美文网首页
返回顶部底部动画

返回顶部底部动画

作者: jiaomeichen | 来源:发表于2017-06-14 14:30 被阅读0次

1、锚点链接

<a href=‘#mao1’>mao1</a>
<a id='mao1'>我是mao1</a>
js:
var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;//网页被卷去的高 
$('a[href^="#"]').click(function() {//其href属性值以 "#"开头的元素
    var the_id = $(this).attr("href");
    $('html, body').animate({
        scrollTop: $(the_id).offset().top +"px"
    }, 'slow');
    return false;
});;

2、去到底部

var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;//网页被卷去的高度
var aHeight = document.documentElement.scrollHeight || document.body.scrollHeight;//网页正文高
 $("#register").on("click",function(){
       $("body,html").animate({scrollTop:aHeight },1000);
 });

3、回到顶部

var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
if(scrollTop >= 500) {
    $("#backTop").css("display", "block")
} else {
    $("#backTop").css("display", "none")
}
$("#backTop").on("click", function() {
    $("body,html").animate({ scrollTop: 0 }, 1000);
})

4、判断浏览器是否滚动到底部

1、判断滚动条到底部,需要用到DOM的三个属性值,即scrollTop、clientHeight、scrollHeight。
     (1)scrollTop为滚动条在Y轴上的滚动距离。
     (2)clientHeight为内容可视区域的高度。
     (3)scrollHeight为内容可视区域的高度加上溢出(滚动)的距离。
从这个三个属性的介绍就可以看出来,滚动条到底部的条件即为scrollTop + clientHeight == scrollHeight。
2、代码:
    $(window).scroll(function() {
             var scrollTop = $(this).scrollTop();
             var windowHeight = $(this).height();
             var scrollHeight = $(document).height();
            console.log(scrollTop+"    "+windowHeight+"  "+scrollHeight)
            if(scrollTop + windowHeight == scrollHeight) {
       alert("你已经到底部啦!");
           }
   });

相关文章

网友评论

      本文标题:返回顶部底部动画

      本文链接:https://www.haomeiwen.com/subject/ieisqxtx.html