这个问题可能是由于动画完成时间太长,导致元素在动画完成之前已经重新布局了。您可以尝试减少动画的持续时间或使用 Angular 的 animateOnLoad 指令,以便在视图准备就绪后才启动动画。
以下是使用 animateOnLoad 指令的示例:
在组件模板中:
在组件中:
import { Component } from '@angular/core'; import { trigger, transition, style, animate } from '@angular/animations';
@Component({ selector: 'app-my-component', templateUrl: './my-component.component.html', styleUrls: ['./my-component.component.css'], animations: [ trigger('myAnimation', [ transition(':enter', [ style({ transform: 'translateY(-50%)', opacity: 0 }), animate('500ms ease', style({ transform: 'translateY(0)', opacity: 1 })) ]) ]) ] }) export class MyComponentComponent { // ...你的代码 }
这里使用了 animateOnLoad 指令,在视图准备好以后才启动动画。同时指定了一个动画,将元素从上方移动并淡入。