我们这里是放在vue里面的,普通的html也适用
首先定义下面几个方法
//获取滚动条的位置
getScrollTop() {
var scrollTop = 0;
if(document.documentElement && document.documentElement.scrollTop) {
scrollTop = document.documentElement.scrollTop;
} else if(document.body) {
scrollTop = document.body.scrollTop;
}
return scrollTop;
},
//获取当前范围高度
getScrollHeight() {
return Math.max(document.body.scrollHeight, document.documentElement.scrollHeight);
},
//获取可视范围高度
getClientHeight() {
var clientHeight = 0;
if(document.body.clientHeight && document.documentElement.clientHeight) {
clientHeight = Math.min(document.body.clientHeight, document.documentElement.clientHeight);
} else {
clientHeight = Math.max(document.body.clientHeight, document.documentElement.clientHeight);
}
return clientHeight;
}
然后在mouthed里面调用
let that=this;
window.onscroll = function() {
if(that.getScrollTop() + that.getClientHeight() === that.getScrollHeight()) {
//获取最后的id
console.log(that.dynamics[that.dynamics.length-1].Id)
}
}