【问题标题】:How to get scroll position of component?如何获取组件的滚动位置?
【发布时间】:2021-07-02 03:51:01
【问题描述】:

如何获取 ReactJS 组件的滚动位置(可滚动)?我想我必须向我的组件添加一个滚动事件侦听器,但我被困在那里,因为我不知道如何将它添加到组件中 - 只添加到 window.我还可以通过哪个道具访问滚动位置?

【问题讨论】:

    标签: javascript reactjs


    【解决方案1】:

    可以通过 ref 访问滚动条。您需要确保将 css 设置为 overflow: scrolloverflow: auto 以确保您真正获得可滚动的内容。

    class MyScroller extends React.Component {
      scroller = null
      
      componentDidMount = () => {
        if (this.scroller) {
          this.scroller.addEventListener('scroll', this.handleScroll.bind(this), false)
        }
      }
      
      componentWillUnmount = () => {
        if (this.scroller) {
          this.scroller.removeEventListener('scroll', this.handleScroll.bind(this), false)
        }
      }
      
      handleScroll = () => {
        if (this.scroller == null) return
        const { scrollHeight, scrollTop, clientHeight } = this.scroller
        const scroll = scrollHeight - scrollTop - clientHeight
    
        if (scroll > 0) {
          // We are not at the bottom of the scroll content
        }
        else if (scroll == 0){
          // We are at the bottom
        }
      }
    
      render = () => (
        <div ref={rf => this.scroller = rf} className={styles.myScrollingContainer}>
          <MyScrollingStuff />
        </div>
      )
    
    }

    【讨论】:

    【解决方案2】:

    最简单的选项无需参考

    class MyScroller extends React.Component {
    
      handleScroll = (event) => {
        const { scrollHeight, scrollTop, clientHeight } = event.target;
        const scroll = scrollHeight - scrollTop - clientHeight
    
        if (scroll > 0) {
          // We are not at the bottom of the scroll content
        }
        else if (scroll == 0){
          // We are at the bottom
        }
      }
    
      render = () => (
        <div onScroll={this.handleScroll} className={styles.myScrollingContainer}>
          <MyScrollingStuff />
        </div>
      )
    
    }
    

    这种方法更简洁、更高效,因为脚本不需要在真实 DOM 上操作。

    【讨论】:

    • 滚动的值总是-1 有什么问题?
    【解决方案3】:

    你必须在你的 div、span 等上使用 onScroll 函数

    <TableContainer className={classes.container} onScroll={handleScroll}>
    

    handleScroll 是这个函数,如果滚动位置向下则请求你的数据库

    const handleScroll = async event => {
        const { scrollHeight, scrollTop, clientHeight } = event.target
        const scrollPosition = scrollHeight - scrollTop - clientHeight
        if (scrollPosition <= 0) {
          onRequest()
        }
      }
    

    方法 2,您可以使用 useRef 到您的组件

    class ScrollAwareDiv extends React.Component {
      constructor(props) {
        super(props)
        this.myRef = React.createRef()
        this.state = {scrollTop: 0}
      }
      
      onScroll = () => {
        const scrollY = window.scrollY //Don't get confused by what's scrolling - It's not the window
        const scrollTop = this.myRef.current.scrollTop
        console.log(`onScroll, window.scrollY: ${scrollY} myRef.scrollTop: ${scrollTop}`)
        this.setState({
          scrollTop: scrollTop
        })
      }
    
      render() {
        const {
          scrollTop
        } = this.state
        return (
          <div
            ref={this.myRef}
            onScroll={this.onScroll}
             >
            <p>This demonstrates how to get the scrollTop position within a scrollable react component.</p>
            <p>ScrollTop is {scrollTop}</p>
            <p>This demonstrates how to get the scrollTop position within a scrollable react component.</p>
            <p>ScrollTop is {scrollTop}</p>
            <p>This demonstrates how to get the scrollTop position within a scrollable react component.</p>
            <p>ScrollTop is {scrollTop}</p>
            <p>This demonstrates how to get the scrollTop position within a scrollable react component.</p>
            <p>ScrollTop is {scrollTop}</p>
            <p>This demonstrates how to get the scrollTop position within a scrollable react component.</p>
            <p>ScrollTop is {scrollTop}</p>
            <p>This demonstrates how to get the scrollTop position within a scrollable react component.</p>
            <p>ScrollTop is {scrollTop}</p>
          </div>
        )
      }
    }
    
    ReactDOM.render(
      <ScrollAwareDiv />,
      document.getElementById('root')
    );
    

    【讨论】:

      猜你喜欢
      • 2019-12-18
      • 2014-02-23
      • 2012-03-18
      • 1970-01-01
      • 2011-03-25
      • 2013-09-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多