ReactJS 监听页面滚动事件

需要使用监听页面的滚动事件,折腾了挺久终于实现了,写出来跟大家分享一下。希望有更好的做法也能留言给我,谢谢��

export class Example extends Component{
   scrollHandler = this.handleScroll.bind(this);
    componentDidMount() {
      window.addEventListener('scroll', this.scrollHandler);
    }
     _handleScroll(scrollTop) {
         console.log(scrollTop)
         //滚动条距离页面的高度
        }
    }
    handleScroll(event) {
        let scrollTop = event.srcElement.body.scrollTop;
        this._handleScroll(scrollTop);
    }
}

ReactJS 监听页面滚动事件_第1张图片

你可能感兴趣的:(react)