我正在使用React,以下是我用来实现无限滚动功能的代码。
componentDidMount() {
// Flag to check if the content has loaded.
let flag = true;
function infiniteScroll() {
let enterpriseWrap = $('.enterprise-blocks');
let contentHeight = enterpriseWrap.offsetHeight;
let yOffset = window.pageYOffset;
let y = yOffset + window.innerHeight;
console.log('hey');
if(this.props.hasMore) {
if(y >= contentHeight && flag) {
flag = false;
this.props.loadMoreVendors(function() {
flag = true;
});
}
} else {
window.removeEventListener('scroll', infiniteScroll.bind(this));
}
}
window.addEventListener('scroll', infiniteScroll.bind(this));
}
我实际上想在所有项目加载完成后解除滚动事件,但是removeEventListener
不起作用。我做错了什么吗?