码迷,mamicode.com
首页 > 其他好文 > 详细

检测页面滚动到底部

时间:2016-09-27 17:48:43      阅读:115      评论:0      收藏:0      [点我收藏+]

标签:

原理是:

滚动高度 + 页面高度 = 页面滚动总高度

代码如下:

//文档的总高度
function getScrollHeight(){
  var scrollHeight = 0, bodyScrollHeight = 0, documentScrollHeight = 0;
  if(document.body){
    bodyScrollHeight = document.body.scrollHeight;
  }
  if(document.documentElement){
    documentScrollHeight = document.documentElement.scrollHeight;
  }
  scrollHeight = (bodyScrollHeight - documentScrollHeight > 0) ? bodyScrollHeight : documentScrollHeight;
  return scrollHeight;
}
//浏览器视口的高度
function getWindowHeight(){
  var windowHeight = 0;
  if(document.compatMode == "CSS1Compat"){
    windowHeight = document.documentElement.clientHeight;
  }else{
    windowHeight = document.body.clientHeight;
  }
  return windowHeight;
}
$(window).on(‘scroll‘, function(){
    if ($(this).scrollTop() + getWindowHeight() == getScrollHeight()) {
        $(‘#cardLoader‘).show();
        $(window).off(‘scroll‘);
    }
});

 

检测页面滚动到底部

标签:

原文地址:http://www.cnblogs.com/lazaphy/p/5913581.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!