美文网首页
下划线跟随

下划线跟随

作者: 汀上 | 来源:发表于2019-12-18 14:42 被阅读0次

1.点击跟随 效果图

ef859206-b697-4922-b45b-adf44c6f0ea8.gif
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title></title>
    <style>
        * {
            -webkit-tap-highlight-color: rgba(0, 0, 0, 0);
            -webkit-tap-highlight-color: transparent;
        }

        ul {
            display: flex;
            align-items: center;
            list-style: none;
        }

        ul>li {
            position: relative;
            padding: 6px 0;
            margin: 0 15px;
            font-size:32px;
            color: #666666;
            line-height: 40px;
            flex: 0 0 35px;
            text-align: center;
        }

        .click>i,
        ul>li>i {
            display: block;
            position: absolute;
        }

        .click {
            color: #333333;
            font-weight: bolder;
        }

        .click>i {
            display: block;
            position: absolute;
            width: 50%;
            height: 2px;
            bottom: 0;
            left: 25%;
            background: #AB221E;
            border-radius: 3px;
            animation: tabClick 0.3s 0s linear both;
        }

        @keyframes tabClick {
            0% {
                transform: scaleX(0);
            }

            100% {
                transform: scaleX(1);
            }
        }
    </style>

    <script src="./jquery-3.4.1.min.js" charset="utf-8"></script>
</head>

<body>
    <ul>
        <li class="click">1111<i></i></li>
        <li>22222 <i></i></li>
        <li>333333333333<i></i></li>
        <li>444444444444<i></i></li>
        <li>55555<i></i></li>
    </ul>
</body>
<script type="text/javascript">
  $('li').on('click',function(){
    $(this).addClass('click').siblings().removeClass('click');
  })
</script>
</html>

2.纯css

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>纯CSS导航栏下划线跟随效果</title>
    <style>
        * {
          -webkit-tap-highlight-color: rgba(0,0,0,0);
          -webkit-tap-highlight-color: transparent; 
        }
        ul {
            list-style: none;
        }
        li {
            font-size: 30px;
            padding: 5px 15px;
            float: left;
            cursor: pointer;
            position: relative;
        }
       /* 宽度这里初始值是0,下边的transition在鼠标滑过时动画展开长度,长度在hover里设置 */
        li::before {
            content: '';
            position: absolute;
            top: 0;
            left: 75%;
            width: 0;
            height: 100%;
            border-bottom: 2px solid red;
            transition: 0.3s all linear;
        }
        li:hover {
            color: red;
        }
        /*通过这个控制下划线长度*/
        li:hover::before {
            left: 25%;
            width: 50%; 
        }
        /* 通过这个设置下划线出现的位置,100%就是从最右边滑出*/
        li:hover ~ li::before {
            left: 75%; 
        }
    </style>
</head>
<body>
    <ul>
        <li>1111</li>
        <li>22222</li>
        <li>333333333333</li>
        <li>444444444444</li>
        <li>55555</li>
    </ul>
</body>
</html>

相关文章

网友评论

      本文标题:下划线跟随

      本文链接:https://www.haomeiwen.com/subject/bzkunctx.html