监听页面滚动触发事件,页面停止滚动触发事件
核心代码
data() {
return {
timer: null,
isTipVisible: true
}
}
methods: {
handleScrollStart() {
this.timer && clearTimeout(this.timer)
this.timer = setTimeout(() => {
this.handleScrollEnd()
}, 500)
this.isTipVisible = false
},
handleScrollEnd() {
this.isTipVisible = true
clearTimeout(this.timer)
}
},
mounted() {
window.addEventListener('scroll', this.handleScrollStart)
},
// 记得销毁
beforeDestroy() {
window.removeEventListener('scroll', this.handleScrollStart)
}