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

//鼠标滚动到页面最底部加载数据 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

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


经验分享 程序员 微信小程序 职场和发展