绑定在window上 addEventListener 事件需要 第三个参数设置为true,不然事件不起作用
mounted() {
window.addEventListener('scroll',this.handleScroll,true)
}
methods: {
handleScroll(){
let scrollTop = document.body.scrollTop || document.documentElement.scrollTop;
console.log(scrollTop);
}
}
绑定到元素上
mounted() {
document.querySelector("#wrap").addEventListener('scroll',this.handleScroll)
}
methods: {
handleScroll(){
let scrollTop = document.querySelector("#wrap").scrollTop;
console.log(scrollTop);
}
}