在 React 中跟踪组件的滚动位置

J. *_*ers 3 scroll position scrollbar reactjs

我有一个组件,里面有一个main带有overflow: auto. 根据滚动高度,我需要渲染不同的元素。如何在 React 中获取元素滚动位置?

基本要点是这样的:

function App() {
  return (
    <div>
      <main
        style={{
          backgroundColor: "red",
          overflow: "auto",
          height: "500px",
          padding: "5px"
        }}
      >
        Please Track Me!
        <div
          style={{ backgroundColor: "blue", color: "white", height: "5000px" }}
        >
          Inner
        </div>
      </main>
    </div>
  );
}
Run Code Online (Sandbox Code Playgroud)

我需要以某种方式跟踪main's (with overflow: auto) 滚动位置。

Gas*_*ira 8

您的主要组件必须有一个引用。https://reactjs.org/docs/refs-and-the-dom.html 此外,您还将在组件中使用一个名为 onScroll 的事件来更新它。看一下这个

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}
        style={{
          border: '1px solid black',
          width: '600px',
          height: '100px',
          overflow: 'scroll',
        }} >
        <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')
);
Run Code Online (Sandbox Code Playgroud)

https://codepen.io/JohnReynolds57/pen/NLNOyO


Mid*_*lur 5

不使用 refs 实际上很容易。下面的例子是打字稿。

元素:

<div onScroll={this.scrollEvent}></div>
Run Code Online (Sandbox Code Playgroud)

方法:

 scrollEvent(e: SyntheticEvent) {
    const target = e.target as HTMLTextAreaElement;
    console.log('Current scroll position:', target.scrollTop);
}
Run Code Online (Sandbox Code Playgroud)

  • 它也在功能组件中工作,也许您还有其他问题?我创建了一支笔来演示:[https://codepen.io/midnightblurz/pen/NWrqaVw](https://codepen.io/midnightblurz/pen/NWrqaVw) (4认同)