问题:在移动端或者PC端,都会有下拉或者鼠标想下滑列表,再次加载第二页的数据,以及更多页的数据,从而实现列表数据的懒加载
解决:可以监听 滚动条的在视口的高度,达到某种程度上,从而实现需要进行的操作
上代码:
mounted() {
// 监听
window.addEventListener('scroll', this.onScroll)
},
destroyed() {
// 销毁监听
window.removeEventListener('scroll', this.onScroll)
},
methods:{
onScroll(){
// 滚动条在Y轴上的滚动距离
const scrollTop = document.body.scrollTop || document.documentElement.scrollTop || window.pageYOffset
// 浏览器视口的高度
const clientHeight = document.body.clientHeight || document.documentElement.clientHeight
// 文档的总高度
const scrollHeight = Math.max(document.body.scrollHeight, document.documentElement.scrollHeight)
if (scrollTop + clientHeight >= scrollHeight) {
this.pageIndex++
this.getSpeechList()
}
}
}