前端基于jQuery返回顶部功能代码

前端基于jQuery返回顶部功能代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Back to Top</title>
    <style>
        .content{
            width: 100%;
            height: 3000px;/*模拟html内容设置高度*/
            background-color: black;
        }
        .scroll-up{
            width: 70px;
            height: 40px;
            line-height: 40px;
            background-color: red;
            position: fixed;
            bottom: 50px;
            right: 100px;
            z-index: 100;
            text-align: center;
            border-radius: 4px;
            color: white;
            text-decoration: none;
            display: none; /*默认将按钮隐藏起来*/
        }
        .scroll-up:hover{
            background-color: rgba(25,180,234,.9);
        }
    </style>
</head>
<body>
    <div class="content"></div>
    <a href="" class="scroll-up">Scroll</a>
    <script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
    <script>
        $(function() {
            //当滚动条的位置处于距顶部200像素以下时,跳转链接出现,否则消失 
            $(function() {
                $(window).scroll(function() {
                    if ($(window).scrollTop() > 200) {
                        $(".scroll-up").fadeIn(1000);//使用淡入效果来显示此返回顶部按钮,延时为1000ms,可按需求自己改
                    } else {
                        $(".scroll-up").fadeOut(1000);//使用淡出效果来隐藏此返回顶部按钮,延时为1000ms,可按需求自己改
                    }
                });

                //当点击跳转链接后,回到页面顶部位置 

                $(".scroll-up").click(function() {
                    $('body,html').animate({
                        scrollTop: 0
                    }, 400);//该动画animate返回顶部所用的时间400ms,可按需求自己改
                    return false;
                });
            });
	    });
    </script>
</body>
</html>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值