1.给页面绑定滚动事件
document.addEventListener('scroll', throttle(this.scroll, 300), true)
2.写事件的回调函数
/ 滚动事件-分页获取数据
scroll() {
let scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
if (scrollTop > this.scrollTop) this.scrollTop = scrollTop;
let clientHeight = document.documentElement.clientHeight || document.body.clientHeight;
let scrollHeight = document.documentElement.scrollHeight || document.body.scrollHeight;
if (scrollHeight - (this.scrollTop+clientHeight) < clientHeight && !this.isShow) {
this.debGet(this);
}
},
debGet: debounce((that) => {
that.currPage = that.currPage + 1;
that.getShopsList()
}, 300),
3.防抖,节流函数-(网上抄的,仅供学习)
export function throttle(fun, delay) {
let last, deferTimer
return function (args) {
let that = this
let _args = arguments
let now = +new Date()
if (last && now < last + delay) {
clearTimeout(deferTimer)
deferTimer = setTimeout(function () {
last = now
fun.apply(that, _args)
}, delay)
} else {
last = now
fun.apply(that, _args)
}
}
}
export function debounce(fun, delay) {
return args => {
clearTimeout(fun.id)
fun.id = setTimeout(() => {
fun.call(this, args)
}, delay)
}
}