Detecting when user scrolls to bottom of div with React js

后端 未结 9 2208
悲哀的现实
悲哀的现实 2020-11-28 21:17

I have a website with different sections. I am using segment.io to track different actions on the page. How can I detect if a user has scrolled to the bottom of a div? I hav

9条回答
  •  日久生厌
    2020-11-28 21:38

    This answer belongs to Brendan but in functional components

    export default () => {
       const handleScroll = (e) => {
           const bottom = e.target.scrollHeight - e.target.scrollTop === e.target.clientHeight;
           if (bottom) { 
               console.log("bottom")
           }
        }
    
    return (
         
    //overflowing elements here
    ); }

    If the first div is not scrollable it won't work and onScroll didn't work for me in a child element like div after the first div so onScroll should be at the first HTML tag that has an overflow

提交回复
热议问题