jquery实现返回顶部、底部功能

实现效果:

页面有返回顶部、底部按钮,返回顶部按钮当页面滚动超过一屏时显示,返回底部按钮当页面到达底部时隐藏;

返回时带有滚动返回效果,可控制返回速度。

<!doctype html>
<html>
    <head>
        <title>返回顶部/底部</title>
        <meta http-equiv='charset' content='utf-8'/>
        <script type='text/javascript' src='jquery-1.7.2.js'></script>
        <script type='text/javascript'>
            //显隐按钮
            function showReposBtn(){
                var clientHeight = $(window).height();
                var scrollTop = $(document).scrollTop();
                var maxScroll = $(document).height() - clientHeight;
                //滚动距离超过可视一屏的距离时显示返回顶部按钮
                if( scrollTop > clientHeight ){
                    $('#retopbtn').show();
                }else{
                    $('#retopbtn').hide();
                }
                //滚动距离到达最底部时隐藏返回底部按钮
                if( scrollTop >= maxScroll ){
                    $('#rebtmbtn').hide();
                }else{
                    $('#rebtmbtn').show();
                }
            }
            
            window.onload = function(){
                //获取文档对象
                $body = (window.opera) ? (document.compatMode == "CSS1Compat" ? $("html") : $("body")) : $("html,body");
                //显示按钮
                showReposBtn();
            }
            
            window.onscroll = function(){
                //滚动时调整按钮显隐
                showReposBtn();
            }
            
            //返回顶部
            function returnTop(){
                $body.animate({scrollTop: 0},400);
            }
            
            //返回底部
            function returnBottom(){
                $body.animate({scrollTop: $(document).height()},400);
            }
        </script>
        <style type='text/css'>
            #retopbtn{
                position:fixed;
                bottom:10px;
                right:10px;
            }
            #rebtmbtn{
                position:fixed;
                top:10px;
                right:10px;
            }
        </style>
    </head>
    <body>
        <button id='rebtmbtn' οnclick='returnBottom()'>底部</button>
        <h1>a</h1>
        <h1>a</h1>
        <h1>a</h1>
        <h1>a</h1>
        <h1>a</h1>
        <h1>a</h1>
        <h1>a</h1>
        <h1>a</h1>
        <h1>a</h1>
        <h1>a</h1>
        <h1>a</h1>
        <h1>a</h1>
        <h1>a</h1>
        <h1>a</h1>
        <h1>a</h1>
        <h1>a</h1>
        <h1>a</h1>
        <h1>a</h1>
        <h1>a</h1>
        <h1>a</h1>
        <h1>a</h1>
        <h1>a</h1>
        <h1>a</h1>
        <h1>a</h1>
        <h1>a</h1>
        <h1>a</h1>
        <h1>a</h1>
        <h1>a</h1>
        <h1>a</h1>
        <h1>a</h1>
        <h1>a</h1>
        <h1>a</h1>
        <h1>a</h1>
        <h1>a</h1>
        <h1>a</h1>
        <h1>a</h1>
        <h1>a</h1>
        <h1>a</h1>
        <h1>a</h1>
        <h1>a</h1>
        <h1>a</h1>
        <button id='retopbtn' οnclick='returnTop()'>顶部</button>
    </body>
</html>


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值