在移动端,我们经常会遇到下拉加载到底部就自动加载下一页的需求,那怎么实现呢?这里要分两种情况,如果只是单纯整个页面下拉触底,那可以监控window对象,如下
$(window).scroll(function(){var scrollTop = $(this).scrollTop();var scrollHeight = $(document).height();var windowHeight = $(this).height();console.log("scrollTope:"+scrollTop);//滚动条高度 console.log("scrollHeighte:"+scrollHeight);//文档的总高度 console.log("windowHeighte:"+windowHeight);//可视高度///若文档的总高度 和可视高度+滚动高度相同,就加载if(scrollTop + windowHeight == scrollHeight){alert("到底部的操作");}});
如果是div对象,也就是我们的排行版是弹出来的,可以用
var stop = true;// 监听滚动let box = document.querySelector("#box");box.addEventListener("scroll", function (e) {let scrollTop = e.target.scrollTop;let clientHeight = e.target.clientHeight;let scrollHeight = e.target.scrollHeight;console.log("scrollTop:"+scrollTop);console.log("clientHeight:"+clientHeight);console.log("scrollHeight:"+scrollHeight);if(scrollTop+clientHeight>=scrollHeight-20){if(stop){stop=false;seelctData();}}});function seelctData(){alert("到底了");stop=true;}
都可以实现!
