返回顶部

该博客主要介绍了网页返回顶部功能的实现。通过 HTML、CSS 构建页面布局和样式,利用 jQuery 编写脚本。当滚动条滚动距离超过 200 时显示“返回顶部”按钮,点击该按钮页面会在 1000 毫秒内滚动到顶部。

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>返回顶部</title>
    <style>
        *{padding:0;margin:0;}
        .box{width:100%;height:2000px;background:#ccc;}
        .backTop{width:100px;height:100px;background:red;text-align:center;line-height:100px;
            color:#fff;position:fixed;right:10px;cursor:pointer;bottom:100px;display:none;}
        p{height:95px;border-bottom:5px solid #000;text-align:center;}
    </style>
</head>
<body>
    <div class="box">
        <p class="backTop">返回顶部</p>
    </div>
</body>
    <script src="js/jquery.js"></script>
    <script>
        $(function(){
            var h = $(document).height();
            var html ="";
            for(var i=1;i<=h/100;i++){
                html+="<p>"+i+"</p>";
            }
            $(".box").append(html);
            //var winH= $(window).height();
            $(window).scroll(function(){//滚动条滚动的距离》屏幕的高度  显示   ||隐藏
                if($(window).scrollTop()>200){
                    $(".backTop").show();
                }else{
                    $(".backTop").hide();
                }
            })
            $(".backTop").click(function(){//显示 ===》点击事件  页面距离顶部的距离为0
                $(document.documentElement).animate({"scrollTop":0},1000);
                $(document.body).animate({"scrollTop":0},1000);
            })
        })
    </script>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值