zoukankan      html  css  js  c++  java
  • jQuery实现“回到顶部”按钮功能

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>jQuery</title>
        <style>
          .back_top{
              position: fixed;
              background:gray;
              color: #ffffff;
              padding: 5px;
              bottom: 30px;
              right: 30px;
              width: 42px;
              text-align: center;
              cursor: pointer;
          }
        </style>
    </head>
    <body>
    <div>
        <h1>回到顶部</h1>
            <br/>
        <br/>
        <br/>
        <br/>
        <br/>
        <br/>
        <br/>
        <br/>
        <br/>
        <br/>
        <br/>
        <br/>
        <br/>
        <br/>
        <br/>
        <br/>
        <br/>
        <br/>
        <br/>
        <br/>
    
            <div class="back_top">回到顶部</div>
    
    
    
    </div>
    
    <script src="../js/vendor/jquery-3.2.1.min.js"></script>
    <script>
        //不够平滑
        // $(".back_top").click(function(){
        //     $("body,html").scrollTop(0);
        // })
        //设置成平滑滚动
        $(".back_top").click(function(){
            //滚动时 滚动条离上部的距离
            var distance = $("html").scrollTop() + $("body").scrollTop();
            //设置滚动的总时间
            var time=1000;
            //间隔时间
            var intervalTime=50;
            var itemDistance=distance/(time/intervalTime);
            var intervalId = setInterval(function(){
                distance-=itemDistance;
    
                if(distance<=0){
                    clearInterval(intervalId);
                }
                $("html,body").scrollTop(distance-itemDistance);
            },intervalTime);
        })
    
    </script>
    </body>
    </html>
  • 相关阅读:
    [ZROI 9.16模拟赛] Tutorial
    [ZROI 9.15模拟赛] Tutorial
    [USACO 2017 Open Gold] Tutorial
    [USACO 2017 Feb Gold] Tutorial
    [USACO 2017 Jan Gold] Tutorial
    [BZOJ 1208] 宠物收养所
    [BZOJ 1500] 维护序列
    多表查询
    sql查询语句查询顺序
    单表查询
  • 原文地址:https://www.cnblogs.com/lihuijuan/p/9179861.html
Copyright © 2011-2022 走看看