css 如何改变汉堡包回到原来的状态后,点击移动的链接从扩展的移动菜单?

7jmck4yq  于 2023-01-18  发布在  其他
关注(0)|答案(1)|浏览(142)

我已经知道如何在点击移动链接后关闭扩展的移动的菜单,但不知道如何将汉堡图标返回到菜单栏,我使用的是npm的汉堡React包。
以下是组件:

import React, { useState } from "react";
import "./Navbar.css";
import { Fade as Hamburger } from "hamburger-react";

const Navbar = () => {
  window.addEventListener("scroll", function () {
    const header = document.querySelector(".header");
    header.classList.toggle("active", window.scrollY > 0);
  });
  window.addEventListener("scroll", function () {
    const hamburger = document.querySelector(".hamburger");
    hamburger.classList.toggle("active", window.scrollY > 0);
  });

  const [click, setClick] = useState(false);
  const handleClick = () => setClick(!click);

  const closeMenu = () => setClick(false);

  return (
    <div className="parent">
      <div className="header d__flex justify__content__flex__end pxy__30">
        <ul className={click ? "nav-menu active" : "nav-menu"}>
          <li className="nav__items mx__15">
            <a href="/" onClick={closeMenu}>
              Home
            </a>
          </li>
          <li className="nav__items mx__15">
            <a href="#about" onClick={closeMenu}>
              About
            </a>
          </li>
          <li className="nav__items mx__15">
            <a href="#projects" onClick={closeMenu}>
              Projects
            </a>
          </li>
          <li className="nav__items mx__15">
            <a href="#contact" onClick={closeMenu}>
              Contact
            </a>
          </li>
        </ul>
        <Hamburger
          className="hamburger-react"
          color="white"
          direction="right"
          rounded
          onToggle={handleClick}
        />
      </div>
    </div>
  );
};

export default Navbar;

下面是css:

.parent {
  height: 124px;
}

img {
  padding-top: 5px;
}

.nav__items {
  font-size: 18px;
}

.nav-menu {
  display: flex;
  text-transform: uppercase;
  letter-spacing: 1px;
}

.nav-link {
  color: #fff;
}

ul a {
  display: inline-block;
  position: relative;
  padding: 1.5em 0;
}

ul a:hover {
  color: #f9004d;
}

ul a::after {
  content: "";
  position: absolute;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 0.2em;
  background-color: #f9004d;
  opacity: 0;
  transition: opacity 300ms, transform 300ms;
}

ul a:hover::after,
ul a:focus::after {
  opacity: 1;
  transform: translate3d(0, 0.2em, 0);
}

/* Scale from center */
ul a::after {
  opacity: 1;
  transform: scale(0);
  transform-origin: center;
}

ul a:hover::after,
ul a:focus::after {
  transform: scale(1);
}

.hamburger-react {
  display: none;
  position: fixed;
  /* top: 3.2rem; */
  top: 1rem;
  right: 2rem;
  transition: 0.4s all ease;
  cursor: pointer;
}

.hamburger-react.active {
  position: fixed;
  /* z-index: 1000; */
  top: 1.5rem;
  right: 2rem;
}

.header {
  transition: 0.4s all ease;
}

.header.active {
  position: fixed;
  z-index: 1;
  width: 100%;
  padding: 0px 30px;
  background-color: #010101;
}

@media only screen and (max-width: 960px) {
  .hamburger-react {
    display: block;
    z-index: 2;
  }

  .nav-menu {
    position: fixed;
    right: -100%;
    flex-direction: column;
    justify-content: center;
    background-color: #010101;
    width: 100%;
    height: 100%;
    text-align: center;
    transition: 0.4s;
    top: 0%;
  }

  .nav-menu.active {
    right: 0;
    z-index: 1;
  }

  .nav__items {
    font-size: 1.5rem;
  }
}

我觉得我需要向closeMenu函数添加一些更多的功能,以将汉堡包返回到酒吧,但还没有成功。

2wnc66cl

2wnc66cl1#

假设目标是在closeMenu触发时将汉堡包图标更改回非活动外观,请尝试将toggled={click}设置为Hamburger的 prop 。
click状态似乎是用来控制图标的,并且它的处理程序事件都已设置,因此Hamburger应该只需要设置它就可以控制toggled

<Hamburger
  className="hamburger-react"
  color="white"
  direction="right"
  rounded
  // 👇 Pass the state to control the component
  toggled={click}
  onToggle={handleClick}
/>

相关问题