簡體   English   中英

ReactJS-檢測何時出現垂直滾動

[英]ReactJS - detect when the vertical scroll appears

當垂直窗口的瀏覽器滾動出現時,我想采取一些措施。 如下圖所示:

https://www.dropbox.com/s/t8th7cp7rcr662a/scroll.jpg?dl=0

以下代碼無效。

class App extends Component {
      constructor(props) {
        super(props)      
        this.onScroll = this.onScroll.bind(this)
      }

      onScroll(){
        window.onscroll = function (Event) {
          alert('the scroll is visible!')
        }
      }

      componentDidMount() {      
        window.addEventListener('onscroll', this.onScroll)
      }

      componentWillUnmount() {         
        window.removeEventListener('onscroll', this.onScroll)
      }

      render() {
        return (
          <div>
            <canvas id="canvas"></canvas>
            <div className="wrapper-all">
              <Coluna1 />
              <Coluna2 />
              <FooterMobile />
            </div>

          </div>
        )
      }
    }


    export default App;

您可以偵聽resize事件,並將document.body.scrollHeightdocument.body.clientHeight進行比較。

componentDidMount() {
    const screenHeight = window.innerHeight;
    const totalHeight = window.body.scrollHeight;

    if(screenHeight < totalHeight) {
        alert('Scroll detected');
    }

}

此代碼很可能會解決您的問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM