jquery写点击导航跟随效果

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.js"></script>
    <style>
        *{
            padding: 0;
            margin: 0;
        }
        ul{
            display: flex;
            margin: 100px;
            margin-left: 200px;
            position: relative;
        }
        li{
            list-style: none;
            
        }
        a{
            text-decoration: none;
            width: 120px;
            height: 40px;
            background: #f1f1f1;
            text-align: center;
            line-height: 40px;
            display: block;
        }
        div{
            bottom: 0;
            width: 120px;
            border-bottom: 3px solid red;
            position: absolute;
        }
        .red{
            color: red
        }
        li a:hover{
            color: red;
        }
    </style>
</head>
<body>
    <ul>
        <div class="box"></div>    <!-- t添加一个div块 -->
        <li><a href="aa:;" class="red">首页</a></li>
        <li><a href="aa:;">关于</a></li>
        <li><a href="aa:;">我们</a></li>
        <li><a href="aa:;">他们</a></li>
        <li><a href="aa:;">电话</a></li>
        <li><a href="aa:;">其他</a></li>
    </ul>
</body>
<script>
    $("ul li").click(function(){
        $(this).find("a").attr("class","red").parent().siblings().find("a").removeAttr("class","red")  //颜色变化
        console.log($(this).index()-1)
        $(".box").stop().animate({"left":($(this).index()-1)*$(this).width()},1000)             //红色线的div块移动
    })

</script>
</html>

猜你喜欢

转载自blog.csdn.net/namechenfl/article/details/81533288