解决vue子组件无法监听到浏览器滚动
Sonder
2020-09-29
660字
2分钟
浏览 (3k)
代码如下:
<div class="desc" ref="scrollBox"></div>
mounted() {
window.addEventListener('scroll', this.handleScroll,true);
},
destroyed(){
window.removeEventListener('scroll', this.handleScroll,true);
},
methods: {
handleScroll() {
let scrollTop = this.$refs.scrollBox.getBoundingClientRect().top
console.log(scrollTop)
},
// 另一种方法
handleScroll1() {
let scrollTop = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop;
if (scrollTop > 760) {
console.log(scrollTop)
} else {
console.log(scrollTop)
}
},
}