jquery实现仿视觉中国tab栏切换动画效果

 效果为点击每个小li时,下面红线会随着所点击小li的位置,进行来回移动。

且长度适应该小li的宽度,包含padding值。

注:该效果比“视觉中国”实现的动画效果好

代码如下:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>陈不知代码</title>
    <script src="../js/jquery.min.js"></script>
</head>
<style>
    * {
        margin: 0;
        padding: 0;
    }
    
    .hezi {
        width: 100%;
        height: 54px;
        border-bottom: 1px solid #000;
    }
    
    .hezi ul {
        margin: 100px auto;
        width: 1200px;
    }
    
    li {
        list-style: none;
        cursor: pointer;
        float: left;
        color: #666666;
        height: 54px;
        line-height: 60px;
        margin-right: 50px;
        text-align: center;
        padding: 0 20px;
    }
    
    ul {
        position: relative;
    }
    
    .dianji {
        color: #fff;
    }
    
    i {
        width: 50px;
        height: 3px;
        position: absolute;
        left: 12px;
        top: 54px;
        background-color: red;
    }
</style>

<body>
    <div class="hezi">
        <ul>
            <li>推荐</li>
            <li>正在流行</li>
            <li>猜你喜欢</li>
            <i></i>
        </ul>
    </div>
    <script>
        $("ul").on("click", "li", function() {
            /* alert("55"); */
            var ju = $(this).offset().left - 165;
            var kuan = $(this).innerWidth();
            $("i").stop().animate({
                width: kuan,
                left: ju
            }, 300);
        })
    </script>
</body>

</html>

心得:通过获取当前小li的宽度,和左边值返回给i,从而实现动画效果。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值