需求 初始化获得一部分数据 在滚动至底部时 通过传递当前数组的量 进行二次请求 获得完整数据列表
this.$refs.theref.addEventListener('scroll', this.[方法]);
— theref: 获取需要滚动区域的ref
- 因为组件内部 是通过数据渲染出的列表 考虑vue渲染规则 需要在元素渲染后执行scroll监听 否则获取不到对应dom节点
所以需要配合使用 $nextTick()
this.$nextTick(()=>{
this.$refs.theref.addEventListener('scroll', this.[方法]);
}
function throttle (fn, wait) {
let timer = null;
return function (...args) {
let context = this
if (timer) {
clearTimeout(timer);
timer = null;
}
timer = setTimeout(function () {
fn.apply(context, args)
}, wait)
}
}
this.containerScrollFn = this.throttle(this.[方法], 200);
this.$nextTick(()=>{
this.$refs.theref.addEventListener('scroll', this.containerScrollFn);
})
isScrollBottom() {
const container = this.$refs.theref;
var scrollTop = container.scrollTop;
var windowHeight = container.clientHeight;
var scrollHeight = container.scrollHeight;
if(scrollTop+windowHeight == scrollHeight){
this.getBroadcast(this.theNum);
this.isBottom = true; // 滚动至底部
}else{
this.isBottom = false;
}
},