angular - 动画兄弟元素
问题描述
在我的应用程序中,我有 2 个兄弟元素。一个元素被隐藏,我可以通过按钮切换它的可见性。当这个隐藏的元素变得可见时,我添加了一个动画。问题是,兄弟元素没有动画。它只是跳到它的新位置。知道如何解决这个问题吗?请参阅stackblitz示例。
app.component.ts
import { Component } from '@angular/core';
import { animate, style, transition, trigger } from "@angular/animations";
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css',],
animations: [
trigger(
"enterAnimation", [
transition(":enter", [
style({transform: "translateY(-100%)", opacity: 0}),
animate("500ms", style({transform: "translateY(0)", opacity: 1}))
]),
transition(":leave", [
style({transform: "translateY(0)", opacity: 1}),
animate("500ms", style({transform: "translateY(-100%)", opacity: 0}))
])
]
)
],
})
export class AppComponent {
visible: boolean = false;
toggle(): void {
this.visible = !this.visible;
}
}
app.component.html
<div class="box1" *ngIf="visible" [@enterAnimation]></div>
<div class="box2"></div>
<button (click)="toggle()">Toggle</button>
解决方案
而不是使用变换使用高度来获得这种效果。演示
trigger(
"enterAnimation", [
transition(":enter", [
style({height: "0px", opacity: 0}),
animate("500ms", style({height: "50px", opacity: 1}))
]),
transition(":leave", [
style({height: "50px", opacity: 1}),
animate("500ms", style({height: "0px", opacity: 0}))
])
]
)
推荐阅读
- go - 如何在 go-kit 中使用 zap logger?
- reporting-services - 从开发人员导出修改后的报告以进行测试。现在报告抱怨缺少参数值
- snowflake-cloud-data-platform - 在 Snowflake 中灵活命名表
- flutter - 单个小部件中的多个 API 请求
- javascript - 如何防止移动键盘(android)弄乱布局?
- java - 如何第一次运行 FreePBX?
- c# - 如何在单个 NuGet 包中提供多个“dotnet new”模板?
- java - 使用来自自定义 JR 数据源的数据创建图表
- html - 防止谷歌浏览器打破双引号并将括号打开到新行?
- jquery - jQuery:仅在您第一次进入网站时(或在硬刷新时)运行介绍类