您可以使用“手动动画”。典型动画和手动动画的“翻译”并不难
在您定义的组件中
import {style,animate,AnimationBuilder,AnimationFactory,AnimationPlayer} from "@angular/animations";
private player: AnimationPlayer;
constructor(private builder: AnimationBuilder) {}
并创建一个“动画”元素的函数,例如
/*I want "recreate" the animation
transition(':enter', [
style({ opacity: 0 }),
animate('100ms', style({ opacity: 1 })),
]),
transition(':leave', [
animate('100ms', style({ opacity: 0 }))
])
*/
animate(element: any, state:string) {
const myAnimation = state=='enter'?
this.builder.build([
style({ opacity: 0 }),
animate(this.timing, style({ opacity: 1 }))
]):
this.builder.build([
animate(this.timing, style({ opacity: 0 }))
])
;
this.player = myAnimation.create(element);
//you can control when the animation is finish
this.player.onDone(()=>{
this.toogle=!this.toogle;
})
this.player.play();
}
所以你可以拥有,例如
<div #div style="background-color:red;width:5rem;height:5rem"></div>
<button (click)="animate(div,toogle?'enter':'leave');">click</button>
或使用 ViewChild 获取元素并制作动画或调用 ngOnInit 或...