在Vue.js的鉤子函數(shù)created里初始化加載數(shù)據(jù),在鉤子函數(shù)mounted中聲明一個(gè)scroll事件監(jiān)聽,監(jiān)聽屏幕的高度變化,一旦滑動(dòng)到達(dá)屏幕底部就去后臺(tái)請求新的數(shù)據(jù)。
// 監(jiān)聽滾動(dòng)事件
mounted () {
window.addEventListener('scroll', this.handleScroll,true)
},
handleScroll () {
let scrollTop = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop || 0; //滾動(dòng)條的位置
var windowHeitht = document.documentElement.clientHeight; //在頁面上返回內(nèi)容的可視高度
var scrollHeight = document.documentElement.scrollHeight; //返回整個(gè)元素的高度(包括帶滾動(dòng)條的隱蔽的地方)
//是否滾動(dòng)到底部的判斷
if(Math.round(scrollTop) + windowHeitht == scrollHeight){
this.index = this.index + 1;//下一頁
if(this.index <= this.pages){
this.loadDataList()
}
}
},
獲取列表的方法內(nèi)判斷已是最后一頁時(shí),顯示提示“沒有更多了”。
注意容器的css,height:100%;overflow-y:scroll;