Example of how to implement underline effects using Css and JS

Example of how to implement underline effects using Css and JS

This article mainly describes two kinds of underline dynamic effects. The first one is that when hovering, the X-axis expands from the inside to the outside to achieve the animation effect. The second one is automatic display from left to right, or from right to left! !

The main effect is to use pseudo-class tags, hover, and transition to achieve animation effects.

The x-axis expands from inside to outside

The animation of the horizontal line is realized by using the Bezier curve. The specific code is as follows:

ul {
  display: flex;
  padding: 0;
  margin: 0;
  list-style-type: none;
}
ul:hover li:not(:hover) a {
  opacity: 0.2;
}
ul li {
  position: relative;
  padding: 30px 25px 30px 25px;
  cursor: pointer;
}
ul li::after {
  position: absolute;
  content: "";
  top: 100%;
  left: 0;
  width: 100%;
  height: 2px;
  background: #3498db;
  transform: scaleX(0);
  transition: 0.4s cubic-bezier(0.165, 0.84, 0.44, 1);
}
ul li:hover::after, ul li.active::after {
  transform: scaleX(1);
}

Left and right horizontal underline animation effect

Mainly use js to determine the position of the mouse when it moves away, and display the animation effect by moving it left and right

The js code is as follows:

document.querySelectorAll('a').forEach(elem => {

  elem.onmouseenter =
  elem.onmouseleave = e => {

    const tolerance = 5;

    const left = 0;
    const right = elem.clientWidth;

    let x = e.pageX - elem.offsetLeft;

    if (x - tolerance < left) x = left;
    if (x + tolerance > right) x = right;

    elem.style.setProperty('--x', `${x}px`);

  };

});

CSS uses pseudo-class tags to implement animation effects
The css code is as follows:

a {
  position: relative;
  font-weight: 600;
  text-decoration: none;
  color: rgba(0, 0, 0, 0.4);
  transition: color .3s ease;
}
a::after {
  --scale: 0;
  content: '';
  position: absolute;
  left: 0;
  right: 0;
  top: 100%;
  height: 3px;
  background: #4c81c9;
  -webkit-transform: scaleX(var(--scale));
          transform: scaleX(var(--scale));
  -webkit-transform-origin: var(--x) 50%;
          transform-origin: var(--x) 50%;
  transition: -webkit-transform 0.3s cubic-bezier(0.535, 0.05, 0.355, 1);
  transition: transform 0.3s cubic-bezier(0.535, 0.05, 0.355, 1);
  transition: transform 0.3s cubic-bezier(0.535, 0.05, 0.355, 1), -webkit-transform 0.3s cubic-bezier(0.535, 0.05, 0.355, 1);
}
a:hover {
  color: #4c81c9;
}
a:hover::after {
  --scale: 1;
}

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

<<:  Navicat connection MySQL error description analysis

>>:  Analysis of the pros and cons of fixed, fluid, and flexible web page layouts

Recommend

How to deploy MongoDB container with Docker

Table of contents What is Docker deploy 1. Pull t...

JavaScript implements password box input verification

Sometimes it is necessary to perform simple verif...

Problems with Vue imitating Bibibili's homepage

Engineering Structure The project is divided into...

MySql 8.0.11-Winxp64 (free installation version) configuration tutorial

1. Unzip the zip package to the installation dire...

MySQL 8.0.12 installation and configuration graphic tutorial

Recorded the download and installation tutorial o...

Details after setting the iframe's src to about:blank

After setting the iframe's src to 'about:b...

Interpretation of CocosCreator source code: engine startup and main loop

Table of contents Preface preparation Go! text St...

Solve the problem of MySQL using not in to include null values

Notice! ! ! select * from user where uid not in (...

A detailed introduction to seata docker high availability deployment

Version 1.4.2 Official Documentation dockerhub st...

JavaScript array reduce() method syntax and example analysis

Preface The reduce() method receives a function a...

Tutorial on installing Android Studio on Ubuntu 19 and below

Based on past experience, taking notes after comp...

How to run .sh files in Linux system

There are two ways to run .sh files in Linux syst...

Complete steps to solve 403 forbidden in Nginx

The webpage displays 403 Forbidden Nginx (yum ins...

The complete process of Docker image creation

Table of contents Preface Creation steps Create a...

How to configure nginx to limit the access frequency of the same IP

1. Add the following code to http{} in nginx.conf...