zoukankan      html  css  js  c++  java
  • scrollTop如何实现click后页面过渡滚动到顶部

    用JS操作,body元素的scrollTop

    var getTop = document.getElementById("get-top");
    var head = document.getElementById("head");
    getTop.onclick = function () {
        var time = setInterval(function () {
            document.body.scrollTop = document.body.scrollTop - 50;
            if (document.body.scrollTop === 0) {
                clearInterval(time);
            }
        }, 1);
    
    };

    下面不确定,网上找的没实验;

    $(window).scroll(function(){
        if ($(window).scrollTop()>100){
            $("#backTop").fadeIn(1500);
        }
        else
        {
            $("#backTop").fadeOut(1500);
        }
        });
        //当点击跳转链接后,回到页面顶部位置
        $("#backTop").click(function(){
            $('body,html').animate({scrollTop:0},1000);
            return false;
        });
    });

     点击页面回到底部或者指定位置:

    //  $(window).scroll(function () {
    //    var scrollTop = $(this).scrollTop();
    //    var scrollHeight = $(document).height();
    //    var windowHeight = $(this).height();
    //    console.log(scrollTop+','+scrollHeight+','+windowHeight)
    //    if ((scrollTop + windowHeight) / scrollHeight > 0.99) {
    //      $("#callMe").css("display", 'none')
    //    } else {
    //      $("#callMe").css("display", 'block')
    //    }
    //  });
    $(".order").click(function(){
      var height=document.body.offsetHeight;
    //  $(window).scrollTop(height,3000);
      $('html,body').animate({scrollTop: height}, 300)
    })

    滚动到顶部:

    $('.scroll_top').click(function(){$('html,body').animate({scrollTop: '0px'}, 800);});

    滚动到指定位置:

    $('.scroll_a').click(function(){$('html,body').animate({scrollTop:$('.a').offset().top}, 800);});

    完整事例:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>js平滑滚动到顶部、底部、指定地方</title>
    <script type="text/javascript" src="http://cdn.staticfile.org/jquery/2.1.1-rc2/jquery.min.js"></script>
    <style>
     .box{ height:200px; 100%; background:#ccc; margin:10px 0;}
     .location{ position:fixed; right:0; bottom:10px; 20px; background:#FFC; padding:5px; cursor:pointer;color:#003};
    </style>
    </head>
    <body>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box a">产品介绍产品介绍产品介绍产品介绍产品介绍产品介绍产品介绍产品介绍产品介绍产品介绍产品介绍产品介绍产品介绍</div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box bottom"></div>
    <div class="location">
      <p class="scroll_top">返回顶部</p>
      <p class="scroll_a">产品介绍</p>
      <p class="scroll_bottom">滑到底部</p>
    </div>
    <script type="text/javascript">
     jQuery(document).ready(function($){
      $('.scroll_top').click(function(){$('html,body').animate({scrollTop: '0px'}, 800);}); 
      $('.scroll_a').click(function(){$('html,body').animate({scrollTop:$('.a').offset().top}, 800);});
      $('.scroll_bottom').click(function(){$('html,body').animate({scrollTop:$('.bottom').offset().top}, 800);});
     });
    </script>
    </body>
    </html>
  • 相关阅读:
    Python——数据类型之list、tuple
    Python——数据类型初步:Numbers
    Python——初识Python
    Python——开篇之词
    PAT——乙级1028
    PAT——甲级1009:Product of Polynomials;乙级1041:考试座位号;乙级1004:成绩排名
    PAT——甲级1065:A+B and C(64bit) 乙级1010一元多项式求导
    PAT——甲级1046S:shortest Distance
    PAT——甲级1042:Shuffling Mashine
    特征值和特征向量
  • 原文地址:https://www.cnblogs.com/pengchengzhong/p/6027594.html
Copyright © 2011-2022 走看看