Toggle Class based on scroll React JS

后端 未结 7 1798
醉话见心
醉话见心 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:19

    This is yet another take / my take on hooks approach for on scroll displaying and hiding of a random page element.

    I have been very much inspired from: Dan Abramov's post here.

    You can check a full working example, in this CodeSandbox demo.

    The following is the code for the useScroll custom hook:

    import React, { useState, useEffect } from "react";
    
    export const useScroll = callback => {
      const [scrollDirection, setScrollDirection] = useState(true);
    
      const handleScroll = () => {
        const direction = (() => {
          // if scroll is at top or at bottom return null,
          // so that it would be possible to catch and enforce a special behaviour in such a case.
          if (
            window.pageYOffset === 0 ||
            window.innerHeight + Math.ceil(window.pageYOffset) >=
              document.body.offsetHeight
          )
            return null;
          // otherwise return the direction of the scroll
          return scrollDirection < window.pageYOffset ? "down" : "up";
        })();
    
        callback(direction);
        setScrollDirection(window.pageYOffset);
      };
    
      // adding and cleanning up de event listener
      useEffect(() => {
        window.addEventListener("scroll", handleScroll);
        return () => window.removeEventListener("scroll", handleScroll);
      });
    };
    

    And this hook will be consumed like this:

      useScroll(direction => {
        setScrollDirection(direction);
      });
    

    A full component using this custom hook:

    import React, { useState } from "react";
    import ReactDOM from "react-dom";
    import CustomElement, { useScroll } from "./element";
    import Scrollable from "./scrollable";
    
    function Page() {
      const [scrollDirection, setScrollDirection] = useState(null);
    
      useScroll(direction => {
        setScrollDirection(direction);
      });
    
      return (
        
    {/* a custom element that implements some scroll direction behaviour */} {/* "./element" exports useScroll hook and */} {/* just a lorem ipsum long text */}
    ); } const rootElement = document.getElementById("root"); ReactDOM.render(, rootElement);

    And lastly the code for CustomElement:

    import React, { useState, useEffect } from "react";
    
    export default props => {
      const [elementVisible, setElementVisible] = useState(true);
      const { scrollDirection } = props;
    
      // when scroll direction changes element visibility adapts, but can do anything we want it to do
      // U can use ScrollDirection and implement some page shake effect while scrolling
      useEffect(() => {
        setElementVisible(
          scrollDirection === "down"
            ? false
            : scrollDirection === "up"
            ? true
            : true
        );
      }, [scrollDirection]);
    
      return (
        
    element
    ); };

提交回复
热议问题