切换汉堡菜单

Toogle Hamburger Menu

我在我的项目中实现了这个汉堡菜单。菜单工作正常,但问题是当用户单击导航 Link 时它不会关闭,它只会在仅单击汉堡菜单时关闭。我正在寻找的是 - 用户点击 Hamburger Menu 或 Navigation-link 它应该以相同的方式运行。我试过但没能实现,因为这纯粹是 CSS 驱动的,没有使用脚本。非常感谢任何帮助。

.outer-menu {
    position: relative;
    z-index: 1;
  }
  .outer-menu .checkbox-toggle {
    position: absolute;
    top: 0;
    left: 0;
    z-index: 2;
    cursor: pointer;
    width: 30px;
    height: 30px;
    opacity: 0;
  }
  .outer-menu .checkbox-toggle:checked + .hamburger > div {
    -webkit-transform: rotate(135deg);
            transform: rotate(135deg);
  }
  .outer-menu .checkbox-toggle:checked + .hamburger > div:before,
  .outer-menu .checkbox-toggle:checked + .hamburger > div:after {
    top: 0;
    -webkit-transform: rotate(90deg);
            transform: rotate(90deg);
  }
  .outer-menu .checkbox-toggle:checked + .hamburger > div:after {
    opacity: 0;
  }
  .outer-menu .checkbox-toggle:checked ~ .menu {
    pointer-events: auto;
    visibility: visible;
  }
  .outer-menu .checkbox-toggle:checked ~ .menu > div {
    -webkit-transform: scale(1);
            transform: scale(1);
    -webkit-transition-duration: 0.75s;
            transition-duration: 0.75s;
  }
  .outer-menu .checkbox-toggle:checked ~ .menu > div > div {
    opacity: 1;
    -webkit-transition: opacity 0.4s ease 0.4s;
    transition: opacity 0.4s ease 0.4s;
  }
  .outer-menu .checkbox-toggle:hover + .hamburger {
    box-shadow: inset 0 0 10px rgba(0, 0, 0, 0.1);
  }
  .outer-menu .checkbox-toggle:checked:hover + .hamburger > div {
    -webkit-transform: rotate(225deg);
            transform: rotate(225deg);
  }
  .outer-menu .hamburger {
    position: absolute;
    top: 0;
    left: 0;
    z-index: 1;
    width: 30px;
    height: 30px;
    padding: 1px;
    background: rgba(33, 150, 243, 0.0);
    border-radius: 0 0.12em 0.12em 0;
    cursor: pointer;
    -webkit-transition: box-shadow 0.4s ease;
    transition: box-shadow 0.4s ease;
    -webkit-backface-visibility: hidden;
            backface-visibility: hidden;
    display: -webkit-box;
    display: flex;
    -webkit-box-align: center;
            align-items: center;
    -webkit-box-pack: center;
            justify-content: center;
  }
  .outer-menu .hamburger > div {
    position: relative;
    -webkit-box-flex: 0;
            flex: none;
    width: 100%;
    height: 2px;
    background: #000000;
    -webkit-transition: all 0.4s ease;
    transition: all 0.4s ease;
    display: -webkit-box;
    display: flex;
    -webkit-box-align: center;
            align-items: center;
    -webkit-box-pack: center;
            justify-content: center;
  }
  .outer-menu .hamburger > div:before,
  .outer-menu .hamburger > div:after {
    content: '';
    position: absolute;
    z-index: 1;
    top: -10px;
    left: 0;
    width: 100%;
    height: 2px;
    background: inherit;
    -webkit-transition: all 0.4s ease;
    transition: all 0.4s ease;
  }
  .outer-menu .hamburger > div:after {
    top: 10px;
  }
  .outer-menu .menu {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
    visibility: hidden;
    overflow: hidden;
    -webkit-backface-visibility: hidden;
            backface-visibility: hidden;
    outline: 1px solid transparent;
    display: -webkit-box;
    display: flex;
    -webkit-box-align: center;
            align-items: center;
    -webkit-box-pack: center;
            justify-content: center;
  }
  .outer-menu .menu > div {
    width: 140vw;
    height: 140vw;
    background: $color-white;
    border-radius: 50%;
    -webkit-transition: all 0.4s ease;
    transition: all 0.4s ease;
    -webkit-box-flex: 0;
            flex: none;
    -webkit-transform: scale(0);
            transform: scale(0);
    -webkit-backface-visibility: hidden;
            backface-visibility: hidden;
    overflow: hidden;
    display: -webkit-box;
    display: flex;
    -webkit-box-align: center;
            align-items: center;
    -webkit-box-pack: center;
            justify-content: center;
  }
  .outer-menu .menu > div > div {
    text-align: center;
    max-width: 90vw;
    max-height: 100vh;
    opacity: 0;
    -webkit-transition: opacity 0.4s ease;
    transition: opacity 0.4s ease;
    overflow-y: auto;
    -webkit-box-flex: 0;
            flex: none;
    display: -webkit-box;
    display: flex;
    -webkit-box-align: center;
            align-items: center;
    -webkit-box-pack: center;
            justify-content: center;
  }
  .outer-menu .menu > div > div > ul {
    list-style: none;
    padding: 0 1em;
    margin: 0;
    display: block;
    max-height: 100vh;
  }
  .outer-menu .menu > div > div > ul > li {
    padding: 0;
    margin-bottom: 2em;
    font-size: 2em;
    display: block;
  }
  .outer-menu .menu > div > div > ul > li > a {
    position: relative;
    display: inline;
    cursor: pointer;
    -webkit-transition: color 0.4s ease;
    transition: color 0.4s ease;
    text-transform: uppercase;
  }
  .outer-menu .menu > div > div > ul > li > a:hover {
    color: #e5e5e5;
  }
  .outer-menu .menu > div > div > ul > li > a:hover:after {
    width: 100%;
  }
  .outer-menu .menu > div > div > ul > li > a:after {
    content: '';
    position: absolute;
    z-index: 1;
    bottom: -0.15em;
    left: 0;
    width: 0;
    height: 2px;
    background: #333;
    -webkit-transition: width 0.4s ease;
    transition: width 0.4s ease;
  }
  
<div class="fixed-top shadow-btm">
  <div class="container-fluid bg-white">
    <div class="container">
      <div class="row">
        <div class="menulink">
          <div class="flex-item">
            <!-- <a title="Menu" class="btn btn-link" (click)="open()"><i class="fa fa-bars"></i></a> -->
            <div class="outer-menu">
              <input class="checkbox-toggle" type="checkbox" />
              <div class="hamburger">
                <div></div>
              </div>
              <div class="menu">
                <div>
                  <div>
                    <ul>
                      <li class="nav-item" [routerLinkActive]="['active']">
                        <a class="nav-link" [routerLink]="['/home']"  title="Home">Home</a>
                      </li>
                      <li class="nav-item" [routerLinkActive]="['active']">
                        <a class="nav-link" [routerLink]="['/about']"  title="Home">About</a>
                      <li class="nav-item" [routerLinkActive]="['active']">
                        <a class="nav-link" [routerLink]="['/contact']"  title="Contact">Contact</a>
                      </li>
                    </ul>
                  </div>
                </div>
              </div>
            </div>
          </div>
          <div class="flex-item" style="display:none;">
            <a class="nav-link" [routerLink]="['/home']" title="XYZ Company">XYZ Company B</a>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>

您需要在链接上添加点击侦听器。然后你只要在你点击它的时候将 checked 设置为 false。

let navs = document.querySelectorAll(".nav-item");
let checkbox = document.querySelector(".checkbox-toggle");
navs.forEach(nav => {
   nav.addEventListener("click", () => {
      checkbox.checked = false;
   })
});
.outer-menu {
    position: relative;
    z-index: 1;
  }
  .outer-menu .checkbox-toggle {
    position: absolute;
    top: 0;
    left: 0;
    z-index: 2;
    cursor: pointer;
    width: 30px;
    height: 30px;
    opacity: 0;
  }
  .outer-menu .checkbox-toggle:checked + .hamburger > div {
    -webkit-transform: rotate(135deg);
            transform: rotate(135deg);
  }
  .outer-menu .checkbox-toggle:checked + .hamburger > div:before,
  .outer-menu .checkbox-toggle:checked + .hamburger > div:after {
    top: 0;
    -webkit-transform: rotate(90deg);
            transform: rotate(90deg);
  }
  .outer-menu .checkbox-toggle:checked + .hamburger > div:after {
    opacity: 0;
  }
  .outer-menu .checkbox-toggle:checked ~ .menu {
    pointer-events: auto;
    visibility: visible;
  }
  .outer-menu .checkbox-toggle:checked ~ .menu > div {
    -webkit-transform: scale(1);
            transform: scale(1);
    -webkit-transition-duration: 0.75s;
            transition-duration: 0.75s;
  }
  .outer-menu .checkbox-toggle:checked ~ .menu > div > div {
    opacity: 1;
    -webkit-transition: opacity 0.4s ease 0.4s;
    transition: opacity 0.4s ease 0.4s;
  }
  .outer-menu .checkbox-toggle:hover + .hamburger {
    box-shadow: inset 0 0 10px rgba(0, 0, 0, 0.1);
  }
  .outer-menu .checkbox-toggle:checked:hover + .hamburger > div {
    -webkit-transform: rotate(225deg);
            transform: rotate(225deg);
  }
  .outer-menu .hamburger {
    position: absolute;
    top: 0;
    left: 0;
    z-index: 1;
    width: 30px;
    height: 30px;
    padding: 1px;
    background: rgba(33, 150, 243, 0.0);
    border-radius: 0 0.12em 0.12em 0;
    cursor: pointer;
    -webkit-transition: box-shadow 0.4s ease;
    transition: box-shadow 0.4s ease;
    -webkit-backface-visibility: hidden;
            backface-visibility: hidden;
    display: -webkit-box;
    display: flex;
    -webkit-box-align: center;
            align-items: center;
    -webkit-box-pack: center;
            justify-content: center;
  }
  .outer-menu .hamburger > div {
    position: relative;
    -webkit-box-flex: 0;
            flex: none;
    width: 100%;
    height: 2px;
    background: #000000;
    -webkit-transition: all 0.4s ease;
    transition: all 0.4s ease;
    display: -webkit-box;
    display: flex;
    -webkit-box-align: center;
            align-items: center;
    -webkit-box-pack: center;
            justify-content: center;
  }
  .outer-menu .hamburger > div:before,
  .outer-menu .hamburger > div:after {
    content: '';
    position: absolute;
    z-index: 1;
    top: -10px;
    left: 0;
    width: 100%;
    height: 2px;
    background: inherit;
    -webkit-transition: all 0.4s ease;
    transition: all 0.4s ease;
  }
  .outer-menu .hamburger > div:after {
    top: 10px;
  }
  .outer-menu .menu {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
    visibility: hidden;
    overflow: hidden;
    -webkit-backface-visibility: hidden;
            backface-visibility: hidden;
    outline: 1px solid transparent;
    display: -webkit-box;
    display: flex;
    -webkit-box-align: center;
            align-items: center;
    -webkit-box-pack: center;
            justify-content: center;
  }
  .outer-menu .menu > div {
    width: 140vw;
    height: 140vw;
    background: $color-white;
    border-radius: 50%;
    -webkit-transition: all 0.4s ease;
    transition: all 0.4s ease;
    -webkit-box-flex: 0;
            flex: none;
    -webkit-transform: scale(0);
            transform: scale(0);
    -webkit-backface-visibility: hidden;
            backface-visibility: hidden;
    overflow: hidden;
    display: -webkit-box;
    display: flex;
    -webkit-box-align: center;
            align-items: center;
    -webkit-box-pack: center;
            justify-content: center;
  }
  .outer-menu .menu > div > div {
    text-align: center;
    max-width: 90vw;
    max-height: 100vh;
    opacity: 0;
    -webkit-transition: opacity 0.4s ease;
    transition: opacity 0.4s ease;
    overflow-y: auto;
    -webkit-box-flex: 0;
            flex: none;
    display: -webkit-box;
    display: flex;
    -webkit-box-align: center;
            align-items: center;
    -webkit-box-pack: center;
            justify-content: center;
  }
  .outer-menu .menu > div > div > ul {
    list-style: none;
    padding: 0 1em;
    margin: 0;
    display: block;
    max-height: 100vh;
  }
  .outer-menu .menu > div > div > ul > li {
    padding: 0;
    margin-bottom: 2em;
    font-size: 2em;
    display: block;
  }
  .outer-menu .menu > div > div > ul > li > a {
    position: relative;
    display: inline;
    cursor: pointer;
    -webkit-transition: color 0.4s ease;
    transition: color 0.4s ease;
    text-transform: uppercase;
  }
  .outer-menu .menu > div > div > ul > li > a:hover {
    color: #e5e5e5;
  }
  .outer-menu .menu > div > div > ul > li > a:hover:after {
    width: 100%;
  }
  .outer-menu .menu > div > div > ul > li > a:after {
    content: '';
    position: absolute;
    z-index: 1;
    bottom: -0.15em;
    left: 0;
    width: 0;
    height: 2px;
    background: #333;
    -webkit-transition: width 0.4s ease;
    transition: width 0.4s ease;
  }
<div class="fixed-top shadow-btm">
  <div class="container-fluid bg-white">
    <div class="container">
      <div class="row">
        <div class="menulink">
          <div class="flex-item">
            <!-- <a title="Menu" class="btn btn-link" (click)="open()"><i class="fa fa-bars"></i></a> -->
            <div class="outer-menu">
              <input class="checkbox-toggle" type="checkbox" />
              <div class="hamburger">
                <div></div>
              </div>
              <div class="menu">
                <div>
                  <div>
                    <ul>
                      <li class="nav-item" [routerLinkActive]="['active']">
                        <a class="nav-link" [routerLink]="['/home']"  title="Home">Home</a>
                      </li>
                      <li class="nav-item" [routerLinkActive]="['active']">
                        <a class="nav-link" [routerLink]="['/about']"  title="Home">About</a>
                      <li class="nav-item" [routerLinkActive]="['active']">
                        <a class="nav-link" [routerLink]="['/contact']"  title="Contact">Contact</a>
                      </li>
                    </ul>
                  </div>
                </div>
              </div>
            </div>
          </div>
          <div class="flex-item" style="display:none;">
            <a class="nav-link" [routerLink]="['/home']" title="XYZ Company">XYZ Company B</a>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>

由于您正在使用 Angular,因此您应该利用它提供的功能。 只需让您的组件有一个 属性 isOpened 并在您单击图标或菜单项时切换它。

参见下面的示例

component.ts 文件


@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ]
})
export class AppComponent  {
  isOpened = false;
  public toggleMenu() {
    this.isOpened = !this.isOpened;
  }
}

component.html 文件

<div [class]="isOpened ? 'opened' :  'closed'" (click)="toggleMenu()"> Menu Icon</div>
<ul *ngIf="isOpened">
    <li (click)="toggleMenu()">
        Menu item here
    </li>
  <li (click)="toggleMenu()">
        Menu item here
    </li>
</ul>

您可以根据需要设置样式。

请参阅 stackblitz 以获取可行的解决方案: https://stackblitz.com/edit/angular-ozeg8g

const hamburgerMenu = document.querySelector(".menu-btn");
const hamburger = document.querySelector(".hamburger");
const menuList = document.querySelector(".menu-list");

hamburgerMenu.onclick = () => {
  hamburger.classList.toggle("active");
  menuList.classList.toggle("active");
  hamburgerMenu.classList.toggle("active");
};
menuList.onclick = function (e) {
  if (e.target.classList !== "menu-btn") {
    hamburger.classList.remove("active");
    menuList.classList.remove("active");
    hamburgerMenu.classList.remove("active");
  }
};
body{
  background-color: black
}

.menu-btn {
  cursor: pointer;
  height: 55px;
  width: 55px;
  position: fixed;
  top: 30px;
  left: 30px;
  display: flex;
  justify-content: center;
  align-items: center;
  cursor: pointer;
  z-index: 15;
}
.menu-btn .hamburger {
  background-color: whitesmoke;
  height: 3px;
  width: 50px;
  transition: 0.5s;
}
.menu-btn .hamburger::before {
  content: "";
  position: absolute;
  width: inherit;
  height: 3px;
  transform: translateY(-8px);
  background-color: whitesmoke;
  transition: 0.5s;
}
.menu-btn .hamburger::after {
  content: "";
  position: absolute;
  width: inherit;
  height: 3px;
  transform: translateY(8px);
  background-color: whitesmoke;
  transition: 0.5s;
}
/* -------------hamburger menu ANIMATION starts here----------------- */
.menu-btn.active {
  background-color: #edc681;
  transition: 0.3s;
}
.menu-btn .hamburger.active {
  background-color: transparent;
  opacity: 1;
  transition: 0.3s;
}
.menu-btn .hamburger.active::before {
  background-color: black;
  transform: translateY(0px) rotate(-45deg);
  transition: 0.5s;
  opacity: 1;
}
.menu-btn .hamburger.active::after {
  background-color: black;
  transform: translateY(0px) rotate(45deg);
  transition: 0.5s;
  opacity: 1;
}
/* -------------hamburger menu ANIMATION ends here----------------- */

.menu-list {
  position: fixed;
  top: 0;
  left: -100vw;
  width: 100vw;
  height: 100vh;
  /* background-color: rgba(237, 198, 129, 1); */
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 10;
  background-color: black
  transition: 0.3s all ease-in-out;
}
.menu-list ul {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: space-evenly;
  flex-direction: column;
  list-style: none;
}
.menu-list ul li a {
  color: #edc681;
  font-size: 250%;
  font-weight: 500;
  text-decoration: none;
  position: relative;
}
.menu-list ul li a:after {
  content: "";
  position: absolute;
  width: 100%;
  transform: scaleX(0);
  height: 2px;
  bottom: 0;
  left: 0;
  background-color: whitesmoke;
  transform-origin: bottom right;
  transition: transform 0.25s ease-out;
}

.menu-list ul li a:hover:after {
  transform: scaleX(1);
  transform-origin: bottom left;
}

.menu-list.active {
  left: 0vw;
}
 <div class="menu-btn">
      <div class="hamburger"></div>
    </div>
    <div class="menu-list">
      <ul class="menu-items">
        <li><a href="#home">Home</a></li>
        <li><a href="#about">About</a></li>
        <li><a href="#skills">Skills</a></li>
        <li><a href="#contact">Contact</a></li>
      </ul>
    </div>