methods: {
handleScroll(e) {
const scrollTop = e.target.scrollTop
const scrollWrap = document.getElementById('scroll-wrap')
const offsetTop = scrollWrap.offsetTop
if (scrollTop > offsetTop) { // 滚动值大于偏移值
scrollWrap.style.position = 'fixed' // 鼠标滚动到一定距离实现定位
} else {
scrollWrap.style.position = 'static'
}
}
}
mounted() {
// 滚动监听事件
window.addEventListener('scroll', this.handleScroll, true)
},
beforeDestroy() {
// 离开该页面需要移除监听的事件
window.removeEventListener('scroll', this.scrollToTop, true)
}
vue 鼠标滚动事件处理定位问题
最新推荐文章于 2024-03-20 10:24:40 发布