Home  >  Article  >  Web Front-end  >  How to implement ReactJS to listen to page scroll events

How to implement ReactJS to listen to page scroll events

一个新手
一个新手Original
2017-09-27 09:46:305814browse

You need to use the scroll event of the monitoring page. After struggling for a long time, I finally realized it. I wrote it and shared it with everyone. I hope you can leave me a message if you have a better way to do it, thank you��

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);
    }
}

How to implement ReactJS to listen to page scroll events

The above is the detailed content of How to implement ReactJS to listen to page scroll events. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn