为了账号安全,请及时绑定邮箱和手机立即绑定

回到顶部按钮

标签:
JavaScript

滚动条滚动到下一屏出现回到顶部按钮,点击回到顶部按钮滚动条回到顶部。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
    <head>
        <title>Javascript 回到顶部效果</title>
        <style>
            #btn {
                width:40px;
                height:40px;
                position:fixed;
                left:50%; 
                display:none;
                margin-left:610px;
                bottom:30px; 
                background:url(http://img1.sycdn.imooc.com//535e0dc100010e9c00400080.jpg) no-repeat left top;
            }
            #btn:hover {    
                background:url(http://img1.sycdn.imooc.com//535e0dc100010e9c00400080.jpg) no-repeat 0 -39px;
            }
            .box {
                width:1190px; 
                margin:0 auto; 
            }
    </style>
    </head>
    <body>
        <div class="box">
            <img class="lazyload" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC" data-original="http://img1.sycdn.imooc.com//535e0ce800015b7511902787.jpg" />
        </div>
        <a href="javascript:;" id="btn" title="回到顶部"></a>
        <script class="lazyload" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC" data-original="http://libs.baidu.com/jquery/1.9.0/jquery.js" type="text/javascript"></script>
        <script>
            window.onload=function(){
                var btn=document.getElementById("btn");
                var scrollTop=0;
                var timer=null;
                
                window.onscroll=function(){
                    scrollTop=document.documentElement.scrollTop||document.body.scrollTop;
                    //可视区域
                    var clientHeight=document.documentElement.clientHeight||document.body.clientHeight;
                    if(scrollTop-clientHeight>0){
                        document.getElementById('btn').style.display="block";
                    }else{
                        btn.style.display="none";
                    }
                }
                btn.onclick=function(){
                    newScrollTop=document.documentElement.scrollTop||document.body.scrollTop;
                    timer=setInterval(function(){
                        if(scrollTop==0){
                            clearInterval(timer);                            
                        }
                        document.documentElement.scrollTop=document.body.scrollTop=0;
                    },30)
                }
            }
        </script>
    </body>
</html>


12/21

点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消