jquery返回顶部按钮(随滚动位置显示隐藏)

内容介绍

  使用jQuery做“返回顶部”,按钮随页面滚动位置显示隐藏。

一、代码
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>jquery返回顶部按钮(含显示隐藏)</title>
    <script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"></script>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
        
        html,
        body {
            width: 100%;
            height: 100%;
        }
        
        .cons {
            height: 150vh;
        }
        
        #goTop {
            position: fixed;
            bottom: 30px;
            right: 30px;
            width: 50px;
            height: 50px;
            padding: 3px;
            display: flex;
            justify-content: center;
            align-items: center;
            flex-wrap: wrap;
            border-radius: 5%;
            border: 0.3px solid #999;
            cursor: pointer;
            user-select: none;
        }
    </style>
</head>

<body>
    <div class="cons">
        <div id="goTop">
            <div>返 回</div>
            <div>顶 部</div>
        </div>
    </div>
    <script>
        function init() {
            if ($(window).scrollTop() > 50) {
                $("#goTop").fadeIn(200);
            } else {
                $("#goTop").fadeOut(200);
            }
        }
        init();
        //  返回顶部按钮-显示隐藏
        $(window).scroll(function() {
            init();
        });
        //  点击返回顶部
        $("#goTop").click(function(e) {
            $("body,html").animate({
                scrollTop: 0
            }, 500)
            return false
        });
    </script>
</body>

</html>

标签:返回顶部,jquery,显示隐藏


更多演示案例,查看 案例演示


欢迎评论留言!

  • 5
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 10
    评论
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

人间小美味695

您的鼓励是我创作的动力,感谢!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值