代码之家  ›  专栏  ›  技术社区  ›  Andrés Marino

如何在React中将动画添加到响应Nav

  •  0
  • Andrés Marino  · 技术社区  · 3 年前

    我想在按下按钮打开Nav上的取消菜单时添加动画或过渡。 屏幕较小时会出现该按钮 这是代码

    export const NavBar = () => {
      const [showNav, setShowNav] = useState(false);
      return (
        <>
          <nav className="flex items-center justify-between pl-8 pr-16 fixed w-full border-b-2 h-20 top-0 bg-white/30 backdrop-blur-sm">
            <img         src="https://res.cloudinary.com/dv8nczwtj/image/upload/v1684896617/Logo_jivlnb.png"
              alt="Logo"
              className="logo"
            />
            <div className="md:flex flex-row space-x-5 hidden">
              <a href="#" className="brand">
                Apple
              </a>
              <a href="#" className="brand">
                Samsung
              </a>
              <a href="#" className="brand">
                Xiaomi
              </a>
              <a href="#" className="brand">
                Google
              </a>
            </div>
            <button className="md:hidden" onClick={() => setShowNav(!showNav)}>
              <img
                src="https://res.cloudinary.com/dv8nczwtj/image/upload/v1684859901/menu_wh8ccz.png"
                alt="Menu"
                className="w-6"
              />
            </button>
            <CartWidget />
          </nav>
          {showNav && (
            <div className="flex fixed w-full flex-col justify-center items-center space-y-4 pb-2 border-b-2 border-black md:hidden bg-white/30 top-20 pt-4 backdrop-blur-sm">
              <a href="#" className="brand">
                Apple
              </a>
              <a href="#" className="brand">
                Samsung
              </a>
              <a href="#" className="brand">
                Xiaomi
              </a>
              <a href="#" className="brand">
                Google
              </a>
            </div>
          )}
        </>
      );
    };
    
    

    我试着将过渡添加到海军美孚,但没有成功。 我是新手,所以我需要帮助。 谢谢

    2 回复  |  直到 3 年前
        1
  •  0
  •   RMF    3 年前

    一种方法是首先创建一个名为toggleNav的函数 这样做:

     const toggleNav = () => {
        setShowNav(!showNav);
      };
    

    尽量避免编写内联函数,因为有时会使代码有点混乱(只是一个建议)

    接下来将这个showNav参数添加到下面的代码中

    {showNav && (
            <div className="nav-menu">
              <div className="flex fixed w-full flex-col justify-center items-center space-y-4 pb-2 border-b-2 border-black md:hidden bg-white/30 top-20 pt-4 backdrop-blur-sm">
                <a href="#" className="brand">
                  Apple
                </a>
                <a href="#" className="brand">
                  Samsung
                </a>
                <a href="#" className="brand">
                  Xiaomi
                </a>
                <a href="#" className="brand">
                  Google
                </a>
              </div>
            </div>
          )}
    

    如果你只想在小屏幕上专门应用它,你可以把css文件修改成这样

    @media (max-width: 767px) {
      .nav-menu {
        opacity: 0;
      }
    

    希望它能有所帮助!