在 Angular 上安装 Hamburger CSS

Install Hamburger CSS on Angular

我打算在我的 Angular 项目上安装 Hamburger CSS from Jonathan Suh

我使用 npm i --s hamburgers 成功添加了包。我还将其 CSS 文件添加到我的 angular.json 文件中。

当我尝试使用下面的代码添加汉堡包时,汉堡包正在显示,但无法点击。这是为什么?

<button class="hamburger hamburger--collapse" type="button">
    <span class="hamburger-box">
      <span class="hamburger-inner"></span>
    </span>
  </button>

正如您评论说您使用 CSS 并在文档中写:

.scss source files are available if you use Sass as your CSS precompiler. It’s customizable and modular

Here is alternative with angular and css

Html

<div class="collapse" (click)="ifShow=!ifShow" [ngClass]="{'hide':!ifShow}">
  <span></span>
  <span></span>
  <span></span>
</div>

CSS:

.collapse{
  position: relative;
  cursor: pointer;
}
.collapse span{
    position: absolute;
    width: 40px;
    height: 4px;
    border-radius: 4px;
    background-color: black;
}

.collapse span:nth-child(2) {
    top: 10px;
}
.collapse span:nth-child(3){
    top: -10px;
}

.hide span:nth-child(2) {
   transform:rotate(45deg);
   top:0;
}

.hide span:nth-child(3){
   transform:rotate(-45deg);
   top: 0;
}

.hide span:nth-child(1){
  animation:hideMain 1.5s;
  opacity: 0;
}
.hide span:nth-child(2),.hide span:nth-child(3) {
  animation:hide 1.5s;
}

@keyframes hide{
  0%{
    transform:rotate(0);
  }
  50%{
    top:0;
    transform:rotate(0);
  }
}
@keyframes hideMain{
  49%{
   opacity: 1;
  }
  50%{
  opacity: 0;
  }
}

TS:

 ifShow : boolean = true;

要遵循的步骤,

  1. 安装汉堡包。 npm install hamburgers --save
  2. 导入 styles.scss @import '~hamburgers/_sass/hamburgers/hamburgers.scss';
  3. 在 html 中使用汉堡包 div 并切换 class。 (click)="active=!active" [ngClass]="active ? 'is-active' : ''"