Toggle Class based on scroll React JS

后端 未结 7 1797
醉话见心
醉话见心 2021-02-01 06:38

I\'m using bootstrap 4 nav bar and would like to change the background color after ig 400px down scroll down. I was looking at the react docs and found a onScroll but couldn\'t

7条回答
  •  失恋的感觉
    2021-02-01 07:34

     const [scroll, setScroll] = useState(false);
    
     useEffect(() => {
       window.addEventListener("scroll", () => {
         setScroll(window.scrollY > specify_height_you_want_to_change_after_here);
       });
     }, []); 
    

    Then you can change your class or anything according to scroll.

    
    
    

提交回复
热议问题