How to reveal a React component on scroll

后端 未结 4 1598
谎友^
谎友^ 2021-02-04 04:38

I\'ve created a React component for a fixed nav that I would like to remain hidden, until I scroll past a certain point on the page, then slides into view. Medium has a header s

4条回答
  •  野性不改
    2021-02-04 05:12

    In the componentDidMount lifecycle hook, do the same thing as in the jQuery link you have given:

    class Navbar extends React.component {
    
      let delta = 5;
    
      render() {
        return (
          
    ); } componentDidMount() { $(window).scroll(function(event){ var st = $(this).scrollTop(); if(Math.abs(this.state.lastScrollTop - st) <= delta) return; if (st > lastScrollTop){ // downscroll code // $(this.refs.header).css('visibility','hidden').hover () this.setState({ navbarVisible: false }); } else { // upscroll code $(this.refs.header).css('visibility','visible'); this.setState({ navbarVisible: true }); } lastScrollTop = st; }.bind(this)); } }

提交回复
热议问题