html - Angular 7 动画 - 过渡不起作用

标签 html css angular animation

我查看了围绕同一问题的其他问题,但所提供的解决方案均不适合我。

我正在使用 Angular 7.2 和“@angular/animations”:“~7.2.0”。

我有一个要淡入和淡出的 div。所以我给它应用了一个不透明动画。同样的 div,我使用 *ngIf 添加它并从 HTML 中删除它。

问题是,动画样式(不透明度)得到正确应用,但它不能很好地从不透明度 1 > 0 或 0 > 1 过渡。

HTML:

<div *ngIf="divToShow.value !== 'none'" [@openClose]="openState">Dynamic Content Here</div>

动画:

animations: [
    trigger('openClose', [
      state('true', style({
        opacity: '1'
      })),
      state('false', style({
        opacity: '0'
      })),
      transition('true => false', [
        animate('10s')
      ]),
      transition('false => true', [
        animate('10s')
      ]),
    ]),
  ],

setter/getter :

public get openState(): string {
    if (this.divToShow.value !== 'none') {
      return 'true';
    } else {
      return 'false';
    }
  }

divToShow 将在不显示任何 div 时为“none”。当它确实需要显示一个 div 时,它会将 div 名称作为一个字符串。

你能告诉我这里做错了什么吗?

最佳答案

*ngIf="divToShow.value !== 'none'"

以上将删除 <div>当条件为false 时,但如果<div> 不能淡入或淡出不存在。

Angular 为您提供了一个过渡状态,以便在向 DOM 添加元素或从中删除元素时应用动画。

        trigger('openClose', [
            transition(':enter', [
                style({opacity: 0}),
                animate('10s', style({opacity: 1}))
            ]),
            transition(':leave', [
                style({opacity: 1}),
                animate('10s', style({opacity: 0}))
            ])
        ])

参见:https://angular.io/guide/transition-and-triggers#enter-and-leave-aliases

关于html - Angular 7 动画 - 过渡不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59168105/

相关文章:

html - Bootstrap 中的对齐标签和输入

css - <li> 元素之间的空白,显示为 :table-cell

javascript - 如何进行页面/内容转换

Angular:使用 ngFor 的索引会引发错误期望数字类型

html - 位置固定在页面的底 Angular 而不是 div 的底 Angular

html - div 之间的空间从何而来?

Angular 2 - 如何绑定(bind)到 "dynamic"表单字段

angularjs - 单个数组 JSON 对象不会打印 (Ionic 2)

HTML 表 : last column width auto

javascript - 将 CSS 样式指定给 Slickgrid 单元格