鼠标滚动到页面底部自动加载数据--阻止多次请求

来源:互联网 发布:网络借贷陷阱 编辑:程序博客网 时间:2024/05/16 07:14

//鼠标滚动到页面最底部加载数据
var documentHeight = [0];
$(window).bind("scroll",function() {
 
if($(document).scrollTop() + $(window).height() > $(document).height()-1){
    documentHeight.
push($(document).height())
   
if(documentHeight[documentHeight.length-1]>documentHeight[documentHeight.length-2]){
      documentHeight[documentHeight.
length-2] = documentHeight[documentHeight.length-2] + documentHeight[documentHeight.length-1];
     
$(".res-more").css("display","none");
     
self.getShareFiles();
   
}
  }
else{
   
$(".res-more").css("display","block");
 
}
})

为window添加一个scroll事件,浏览器每次触发scroll事件时判断是否滚动到了浏览器底部,如果到了底部则加载新数据。关键是计算滚动条是否滚动到了浏览器底部,算法如下:

滚动条卷起来的高度 +窗口高度 >文档的总高度 + 1


两层判断,外层判断是否到达页面底部,内层判断控制只触发一次请求;