export const useScroll = (el, callback) => {
//默认文档对象, delay 距离底部多远提前触发加载,callback执行加载函数
const checkScrollToBottom = (container = document.documentElement, delay = 100) => {
let viewH = container.offsetHeight
let boxH = container.scrollHeight
let scrollTop = container.scrollTop
if (viewH + scrollTop >= boxH - delay) {
return true
}
return false
}
//节流函数
const debounce = (fn, time = 100, context) => {
let timer
return function(...args) {
clearTimeout(timer)
timer = setTimeout(() => {
context = context || this
fn.apply(context, ...args)
}, time)
}
}
return debounce(() => {
// if (!this.list.length) return
if (checkScrollToBottom(el)) {
callback && callback()
}
}, 500)
}