vue 滚动监听事件
2021-01-31 本文已影响0人
命运总是颠沛流离
vue 滚动监听事件
mounted() {
window.addEventListener('scroll', this.handleScroll, false)
},
methods: {
handleScroll() {
const oContent = document.querySelectorAll('[scrollspy-content]')
const scrollTop =
document.documentElement.scrollTop || document.body.scrollTop
const curContent = Array.prototype.slice
.call(oContent)
.filter(function(item) {
return utils.getPos(item).top - scrollTop <= 120
})
.slice(-1)[0]
if (curContent) {
clearTimeout(this.timer)
this.timer = setTimeout(() => {
this.curNav = curContent.id
}, 40)
}
this.showSidebar = scrollTop > 900
},
handleClickBtn(target) {
utils.scrollTo(target)
},
},