angular 6 动画 *ngIf,过渡无效
angular 6 animation *ngIf, transition not work
该项目展示了两种动画变体。
动画选项 1,触发器('animationOption1')
毫无怨言地工作。
动画选项 2,触发器('animationOption2')
转换在这里不起作用。
Online check this project in StackBlitz.com
app.component.html
<h1>Animation Option 1</h1>
<div (click)="changeDivState()"
[@animationOption1]="clickedDivState"
>Click Me
</div>
<h1>Animation Option 2</h1>
<button (click)="toggleMenu()">Click Me</button>
<ul *ngIf="isMenuOpen"
[@animationOption2]="isMenuOpen ? 'open': 'close'"
>
<li>Menu Item 1</li>
<li>Menu Item 2</li>
<li>Menu Item 3</li>
</ul>
app.component.ts
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css'],
animations: [
trigger('animationOption1', [
state('start', style({
backgroundColor: 'yellow',
width: '150px',
height: '150px'
})),
state('end', style({
backgroundColor: 'green',
width: '300px',
height: '300px'
})),
transition('start => end', animate(1500)),
transition('end => start', animate('800ms 0.5s ease-out'))
]),
trigger('animationOption2', [
state('close', style({
opacity: 0,
backgroundColor: 'yellow'
})),
state('open', style({
opacity: 1,
backgroundColor: 'green'
})),
transition('close <=> open', animate(3000)),
])
]
})
export class AppComponent {
isMenuOpen = false;
clickedDivState = 'start';
changeDivState() {
this.clickedDivState = 'end';
setTimeout(() => {
this.clickedDivState = 'start';
}, 3000);
}
toggleMenu(): void {
this.isMenuOpen = !this.isMenuOpen;
}
}
谷歌搜索没有找到解决方案。
要使其正常工作,您需要删除 <ul>
上的 *ngIf="isMenuOpen"
。 Angular 无法计算 closed
/open
状态之间的转换,因为当 isMenuOpen
为 false
.
时元素根本不存在
这里是一个 StackBlitz 显示的动画,其中删除了 *ngIf
。
或者,您可以利用 entering/leaving 状态与 *ngIf
结合使用。它看起来像这样:
trigger('animationOption2', [
transition(':enter', [
style({ backgroundColor: 'yellow' }),
animate(300)
]),
transition(':leave', [
animate(300, style({ backgroundColor: 'yellow' }))
]),
state('*', style({ backgroundColor: 'green' })),
])
这是一个 StackBlitz 的动作。
该项目展示了两种动画变体。
动画选项 1,触发器('animationOption1')
毫无怨言地工作。
动画选项 2,触发器('animationOption2')
转换在这里不起作用。
Online check this project in StackBlitz.com
app.component.html
<h1>Animation Option 1</h1>
<div (click)="changeDivState()"
[@animationOption1]="clickedDivState"
>Click Me
</div>
<h1>Animation Option 2</h1>
<button (click)="toggleMenu()">Click Me</button>
<ul *ngIf="isMenuOpen"
[@animationOption2]="isMenuOpen ? 'open': 'close'"
>
<li>Menu Item 1</li>
<li>Menu Item 2</li>
<li>Menu Item 3</li>
</ul>
app.component.ts
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css'],
animations: [
trigger('animationOption1', [
state('start', style({
backgroundColor: 'yellow',
width: '150px',
height: '150px'
})),
state('end', style({
backgroundColor: 'green',
width: '300px',
height: '300px'
})),
transition('start => end', animate(1500)),
transition('end => start', animate('800ms 0.5s ease-out'))
]),
trigger('animationOption2', [
state('close', style({
opacity: 0,
backgroundColor: 'yellow'
})),
state('open', style({
opacity: 1,
backgroundColor: 'green'
})),
transition('close <=> open', animate(3000)),
])
]
})
export class AppComponent {
isMenuOpen = false;
clickedDivState = 'start';
changeDivState() {
this.clickedDivState = 'end';
setTimeout(() => {
this.clickedDivState = 'start';
}, 3000);
}
toggleMenu(): void {
this.isMenuOpen = !this.isMenuOpen;
}
}
谷歌搜索没有找到解决方案。
要使其正常工作,您需要删除 <ul>
上的 *ngIf="isMenuOpen"
。 Angular 无法计算 closed
/open
状态之间的转换,因为当 isMenuOpen
为 false
.
这里是一个 StackBlitz 显示的动画,其中删除了 *ngIf
。
或者,您可以利用 entering/leaving 状态与 *ngIf
结合使用。它看起来像这样:
trigger('animationOption2', [
transition(':enter', [
style({ backgroundColor: 'yellow' }),
animate(300)
]),
transition(':leave', [
animate(300, style({ backgroundColor: 'yellow' }))
]),
state('*', style({ backgroundColor: 'green' })),
])
这是一个 StackBlitz 的动作。