转换CSS3在Angular2中不起作用?

时间:2016-08-19 08:57:41

标签: javascript html css css3 angular

我正在尝试添加全屏导航区域,我已经做到了,但转换不起作用,但另一个CSS代码正在工作,我错过了什么?当使用angular2时,我需要在某处添加一些内容吗?

这是我的组件文件夹,如下所示: enter image description here

下面是我的css文件navbar.component.css:

body {
    margin: 0;
    font-family: 'Lato', sans-serif;
}

.overlay {
    height: 100%;
    width: 100%;
    position: fixed;
    z-index: 1;
    top: 0;
    left: 0;
    background-color: rgb(0,0,0);
    background-color: rgba(0,0,0, 0.9);
    overflow-x: hidden;
    transition: 0.5s;
}

.overlay-content {
    position: relative;
    top: 25%;
    width: 100%;
    text-align: center;
    margin-top: 30px;
}

.overlay a {
    padding: 8px;
    text-decoration: none;
    font-size: 36px;
    color: #818181;
    display: block;
    transition: 0.3s;
}

.overlay a:hover, .overlay a:focus {
    color: #f1f1f1;
}

.overlay .closebtn {
    position: absolute;
    top: 20px;
    right: 45px;
    font-size: 60px;
}

@media screen and (max-height: 450px) {
  .overlay a {font-size: 20px}
  .overlay .closebtn {
    font-size: 40px;
    top: 15px;
    right: 35px;
  }
}

下面是我的html文件navbar.component.html:

<div id="myNav" class="overlay" *ngIf="displayNav">
  <a href="javascript:void(0)" class="closebtn" (click)="closeNav()">&times;</a>
  <div class="overlay-content">
    <a href="#">About</a>
    <a href="#">Services</a>
    <a href="#">Clients</a>
    <a href="#">Contact</a>
  </div>
</div>


<h2>Fullscreen Overlay Nav Example</h2>
<p>Click on the element below to open the fullscreen overlay navigation menu.</p>
<p>In this example, the navigation menu will slide in, from left to right:</p>
<span style="font-size:30px;cursor:pointer" (click)="openNav()">&#9776; open</span>

下面是我的打字稿文件navbar.component.ts:

import { Component } from '@angular/core';

@Component({
  moduleId: module.id,
  selector: 'app-navbar',
  templateUrl: 'navbar.component.html',
  styleUrls: ['navbar.component.css']
})
export class NavbarComponent {

  public displayNav: boolean = false;

  constructor() { }

  openNav(){
    this.displayNav = true;
    console.log(this.displayNav);

  }

  closeNav(){
    this.displayNav = false;
    console.log(this.displayNav);
  }
}

当我点击☰打开它只显示带有id =“myNav”的div区域而没有转换时,我已经在class =“overlay”中设置了转换

.overlay {
    height: 100%;
    width: 100%;
    position: fixed;
    z-index: 1;
    top: 0;
    left: 0;
    background-color: rgb(0,0,0);
    background-color: rgba(0,0,0, 0.9);
    overflow-x: hidden;
    transition: 0.5s;
}

但我尝试使用javascript n css进行常规html过渡,它有效,我在这里错过了什么?

1 个答案:

答案 0 :(得分:1)

转换无效,因为*ngIf从DOM中删除了元素。您可以通过几种方式达到预期效果。使用NgClass点击添加和删除有效课程,或使用angular 2's animations,尤其是transition(* <=> void, animation(...))

我建议使用前者,因为应该经常消失并重新出现的元素不应该使用*ngIf

如果我做对了,这是你需要改变代码的唯一方法:

HTML

<div id="myNav" class="overlay" [ngClass]="{active: displayNav}">

CSS

.overlay {
  visibility: hidden;
  opacity: 0
  ...
}

.overlay.active {
  visibility: visible;
  opacity: 1
}