当前位置: 移动技术网 > IT编程>开发语言>JavaScript > jQuery实现当拉动滚动条到底部加载数据的方法分析

jQuery实现当拉动滚动条到底部加载数据的方法分析

2019年03月16日  | 移动技术网IT编程  | 我要评论

本文实例讲述了jquery实现当拉动滚动条到底部加载数据的方法。分享给大家供大家参考,具体如下:

滚动条到底部加载数据原理很简单,就是为window或者滚动元素添加一个scroll事件,浏览器每次触发scroll事件时判断是否滚动到了浏览器底部,

如果到了底部则加载新数据。关键是计算滚动条是否滚动到了浏览器底部,算法如下

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

$(window).on('scroll',function(){
  if(scrolltop() + windowheight() >= (documentheight() - 50/*滚动响应区域高度取50px*/)){
    loadmore();
  }
});

获取页面顶部被卷起来的高度函数

//获取页面顶部被卷起来的高度
function scrolltop(){
 return math.max(
 //chrome
 document.body.scrolltop,
 //firefox/ie
 document.documentelement.scrolltop);
}

chrome浏览器和firefox/ie对滚动条是属于body还是html理解不同导致处理不同。

获取页面文档的总高度

//获取页面文档的总高度
function documentheight(){
 //现代浏览器(ie9+和其他浏览器)和ie8的document.body.scrollheight和document.documentelement.scrollheight都可以
 return math.max(document.body.scrollheight,document.documentelement.scrollheight);
}

这个算法和jquery计算文档高度的方法一致。

获取页面浏览器视口的高度

function windowheight(){
 return (document.compatmode == "css1compat")?
 document.documentelement.clientheight:
 document.body.clientheight;
}

这里需要说明的是document.compatmode这个东东。很陌生,一般情况貌似没有遇到过。

document.compatmode有两个取值"backcompat"和"css1compat"。官方解释是backcompat:标准兼容模式关闭。

css1compat:标准兼容模式开启。

ie对盒模型的渲染在 standards mode和quirks mode是有很大差别的,在standards mode下对于盒模型的解释和其他的标准浏览器是一样,

但在quirks mode模式下则有很大差别,而在不声明doctype的情况下,ie默认又是quirks mode。

举个例子说明两种模式之间的差别有多大。

  • 当document.compatmode等于"backcompat"时,浏览器客户区宽度是document.body.clientwidth
  • 当document.compatmode等于css1compat时,浏览器客户区宽度是document.documentelement.clientwidth

还有其他属性类似。这里不说了,但是我们可以预见两种模式导致ie渲染的基石都更改了,可想而知构建出来的建筑物差别当有多大。

所以请为每一个页面声明doctype不仅仅是一个好习惯,而且是一个必要的处理。quirks mode可以进垃圾堆了。

当用户滚动元素中到一个不同的地方时,scroll事件将发送到这个元素。它适用于window对象,但也可滚动框架与css overflow属性设置为scroll的元素。

1、普通的div滚动到底部加载更多的方法

<div id="test">内容</div>

$('#test').scroll(function(event){
  var top = $(this).scrolltop();
  var height = $(this).height();
  var scrollheight = $(this).get(0).scrollheight;
  if(scrollheight <= top+height){
    loadmore();
  }
});

结果实验发现#test的div怎么样也不能接受scroll时间,最后经过查找资料和测试,发现要给div加上overflow和height的属性。

#test{
  overflow:scroll;
  height:655px;
}

2、window滚动事件:

$(window).scroll(function () {
  var scrolltop = $(window).scrolltop();       // 滚动条距离顶部的高度
  //scrollheight,windowheight,scrollheight1三个height相同,都是这个页面的高度
  var scrollheight = $(document).height();     // 当前页面的总高度
  var windowheight = $(window).height();      // 当前可视的页面高度,jquery获取的不是屏幕的高度,而是整个文档的高度
  var scrollheight1 = $("#test").get(0).scrollheight;
  var windowidth = $(window).width();
  var documentwidtht = $(document).width() ;
  var innerheight = window.innerheight; //window的高度,即手机的高度
  var clientheight = document.body.clientheight; //window的高度
  var clientheight1 = document.documentelement.clientheight;//这个是body的整个高度,chrom测试
  if(scrolltop + innerheight >= scrollheight){    // 距离顶部+当前高度 >=文档总高度,即代表滑动到底部
   loadmore();
  }
});

更多关于jquery相关内容感兴趣的读者可查看本站专题:《jquery常见事件用法与技巧总结》、《jquery常用插件及用法总结》、《jquery操作json数据技巧汇总》、《jquery扩展技巧总结》、《jquery常见经典特效汇总》及《jquery选择器用法总结

希望本文所述对大家jquery程序设计有所帮助。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网