vue 中scroll事件不触发问题

在vue项目中需要监听滚动条滚动的位置,结果写了scroll监听事件就是不生效,最后查资料发现是页面有样式设置了over-flow:scroll,去掉之后完美解决.(页面样式中存在over-flow:scroll,over-flow:auto的时候scroll事件监听不起作用,所以排查问题的时候首先需要考虑当前页面样式中是否存在over-flow);

export default {

methods: {

handleScroll() {
var that = this;
//滚动条在y轴上的滚动距离
var scrollTop = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop;
//文档的总高度
var documentScrollHeight = document.documentElement.scrollHeight || document.body.scrollHeight;
//浏览器窗口的高度
var getWindowHeight = document.documentElement.clientHeight || document.body.clientHeight;
if (scrollTop > 10) {
that.isclass = true;
that.iswhite= true;
} else {
console.log('xiaoyu ');
that.isclass = false;
that.iswhite= false;
}

//滚动条距离页面顶部的距离大于一屏时触发该方法

if (scrollTop + getWindowHeight == documentScrollHeight) {

    that.loadMore();
}
}

},

mounted() {   //监听scroll事件
window.addEventListener('scroll', this.handleScroll);
},
destroyed() { //页面离开后销毁,防止切换路由后上一个页面监听scroll滚动事件会在新页面报错问题


window.removeEventListener('scroll', this.handleScroll)
}
}

原文地址:https://www.cnblogs.com/h5it/p/10193822.html