项目中,因为数据量过大,为提高页面性能,采用页面滑动 滚动条到底部的时候再进行数据请求分页,这边给大给个核心,结合vue的生命周期用纯javascript写的一个监听函数
第一个我们需要知道几个属性值,判断滚动条是否已经到达底部
滚动条到顶部的位置:scrollTop
当前窗口内容可视区:windowHeight
滚动条内容的总高度:scrollHeight
//这里是触发页面滚动事件函数
window.onscroll = function(){...}
判断到底部的等式为: scrollTop+windowHeight=scrollHeight;
//这个方法可以放在vue的某个要用的生命周期里使用
//如mounted() ...
window.onscroll = function(){//变量scrollTop是滚动条滚动时,距离顶部的距离var scrollTop = document.documentElement.scrollTop||document.body.scrollTop;//变量windowHeight是可视区的高度var windowHeight = document.documentElement.clientHeight || document.body.clientHeight;//变量scrollHeight是滚动条的总高度var scrollHeight = document.documentElement.scrollHeight||document.body.scrollHeight;//滚动条到底部的条件if(scrollTop+windowHeight == scrollHeight){//到了这个就可以进行业务逻辑加载后台数据了console.log("到了底部");// window.onscroll = '' 用于解除绑定} }