Want to detect when user scrolls to bottom of div with ReactJS? You can use el.getBoundingClientRect().bottom
to check if the bottom has been viewed.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | isBottom(el) { return el.getBoundingClientRect().bottom <= window.innerHeight; } componentDidMount() { document.addEventListener('scroll', this.trackScrolling); } componentWillUnmount() { document.removeEventListener('scroll', this.trackScrolling); } trackScrolling = () => { const wrappedElement = document.getElementById('header'); if (this.isBottom(wrappedElement)) { console.log('header bottom reached'); document.removeEventListener('scroll', this.trackScrolling); } }; |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.