Detecting when user scrolls to bottom of div with React js

后端 未结 9 2158
悲哀的现实
悲哀的现实 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 22:01

    We can also detect div's scroll end by using ref.

    import React, { Component } from 'react';
    import {withRouter} from 'react-router-dom';
    import styles from 'style.scss';
    
    class Gallery extends Component{ 
    
      paneDidMount = (node) => {    
        if(node) {      
          node.addEventListener("scroll", this.handleScroll.bind(this));      
        }
      }
    
      handleScroll = (event) => {    
        var node = event.target;
        const bottom = node.scrollHeight - node.scrollTop === node.clientHeight;
        if (bottom) {      
          console.log("BOTTOM REACHED:",bottom); 
        }    
      }
    
      render() {
        var that = this;        
        return(
    ...
    ); } } export default withRouter(Gallery);

提交回复
热议问题