在Angular动画的CSS样式中,添加以下代码:
:host { display: block; }
这会确保在动画期间元素的高度保持不变,从而避免跳动。例如:
@Component({ selector: 'app-component', templateUrl: './app.component.html', styleUrls: ['./app.component.css'], animations: [ trigger('fadeInOut', [ transition(':enter', [ style({opacity: 0}), animate(500, style({opacity: 1})), ]), transition(':leave', [ animate(500, style({opacity: 0})), ]), ]), ], }) export class AppComponent {}
app.component.css:
:host { display: block; }
.fadeInOut { opacity: 1; }