这可能是由于Angular在动画结束后自动删除过渡样式引起的。要解决这个问题,我们可以使用以下方法:
1.在动画完成后,使用ngClass指定类名来应用动画样式:
@Component({ selector: 'app-example', animations: [ trigger('fadeInOut', [ state('void', style({ opacity: 0 })), transition('void <=> *', animate(1000)) ]) ] }) export class ExampleComponent { public show: boolean = false; public toggle(): void { this.show = !this.show; }
public onAnimationDone(event: AnimationEvent): void { if (event.toState !== 'void') { this.show ? this.element.classList.add('animation-in') : this.element.classList.add('animation-out'); } }
constructor(private readonly element: ElementRef) {} }
2.在动画完成后,使用setTimeOut()来延迟清除动画样式:
@Component({ selector: 'app-example', animations: [ trigger('fadeInOut', [ state('void', style({ opacity: 0 })), transition('void <=> *', animate(1000)) ]) ] }) export class ExampleComponent { public show: boolean = false; public toggle(): void { this.show = !this.show; }
public onAnimationDone(): void { setTimeout(() => { this.element.nativeElement.classList.remove('animation-in'); this.element.nativeElement.classList.remove('animation-out'); }); }
constructor(private readonly element: ElementRef) {} }
上一篇:Angular动画延迟未执行
下一篇:Angular动画与进入和离开