【发布时间】:2019-04-15 20:13:15
【问题描述】:
这是我的组件模板
<nav class="menu" [ngClass]="{ snap: snap }" [@menuBackground]="'normal'">
<div class="container fluid">
<div class="row align-items-center">
<div class="col-2" [@logoZoomOut]="'normal'">
<img *ngIf="true" src="../../../assets/logo.png" class="logo img-fluid" alt="logo" />
</div>
<div
class="links-wrapper text-right d-none d-md-block col-md-8 offset-md-2 col-lg-6 offset-lg-4 col-xl-5 offset-xl-5"
>
<ul>
<app-menu-link [link]="'/'" [text]="'Home'" [first]="true"></app-menu-link>
<app-menu-link [link]="'/services'" [text]="'Services'"></app-menu-link>
<app-menu-link [link]="'/portfolio'" [text]="'Portfolio'"></app-menu-link>
<app-menu-link [link]="'/contact'" [text]="'Contact'" [contact]="true"></app-menu-link>
</ul>
</div>
</div>
</div>
</nav>
这是我的组件代码:
import { Component, OnInit, OnDestroy } from '@angular/core';
import { trigger, state, style, transition, animate } from '@angular/animations';
@Component({
selector: 'app-menu',
templateUrl: './menu.component.html',
styleUrls: ['./menu.component.scss'],
animations: [
trigger('menuBackground', [
state('normal', style({ background: 'rgba(118, 137, 169, 0.22)' })),
transition('void => normal', [style({ background: 'none' }), animate('.5s 2s ease-in')])
]),
trigger('logoZoomOut', [
state('normal', style({ transform: 'scale(1)' })),
transition('void => normal', [style({ transform: 'scale(1.5)' }), animate('.5s ease-in')])
])
]
})
export class MenuComponent implements OnInit, OnDestroy {
public snap = false;
constructor() {}
ngOnInit() {
window.addEventListener('scroll', this.scroll, true);
}
ngOnDestroy() {
window.removeEventListener('scroll', this.scroll, true);
}
scroll = (): void => {
if (window.pageYOffset > 90) {
this.snap = true;
} else {
this.snap = false;
}
};
}
现在我想为导航的背景和徽标大小设置动画。
但问题是,当menuBackground 和logoZoomOut 触发器都设置在nav 和div 上时 - 只有背景动画并且不播放徽标的动画。
当我删除背景动画时 - 徽标动画正确。
那么我怎样才能同时为这两个元素设置动画呢?
我认为它应该独立工作。我也遇到了徽标动画的问题 - 如果没有 *ngIf="true" 上的 img 元素,它就无法工作。也许它有某种相关性?
【问题讨论】:
标签: html angular typescript angular-template angular-animations