【问题标题】:Unable to trigger Angular transition to expand/hide div无法触发 Angular 转换以展开/隐藏 div
【发布时间】:2020-05-09 05:48:45
【问题描述】:

我正在使用 Angular BrowserAnimations,但似乎无法真正触发它。

我的组件动画设置为

animations: [
  trigger('expandHide', [
    state('expanded', style({
      height: 'auto'
    })),
    state('hidden', style({
      height: '20px'
    })),
    transition('* => expanded', [
      animate('1s')
    ]),
    transition('* => hidden', [
      animate('0.25s')
    ])
  ])
]

我想要制作动画的 div 的模板定义是

<div class="container" [@expandHide]="sheet.isExpanded ? 'expanded' : 'hidden'">

是的,sheet.isExpanded&lt;div (click)="sheet.isExpanded = !sheet.isExpanded"&gt; 设置在相邻的div

我预计它需要 1 秒的时间来扩展高度,而 0.25 秒的时间会倒塌。然而,它只是像没有定义任何转换一样立即打开和关闭。我怎样才能让这个动画工作?

【问题讨论】:

    标签: angular angular-animations


    【解决方案1】:
    1. 删除通配符
    '* => expanded' → 'hidden => expanded'
    '* => hidden' → 'expanded => hidden'
    
    1. 有人更改您的sheet.isExpanded 标志吗?

    最简单的解决办法是

    <div
      class="container" 
      [@expandHide]="sheet.isExpanded ? 'expanded' : 'hidden'"
      (click)="sheet.isExpanded=!sheet.isExpanded" ★ this one
    >
    

    看看example

    【讨论】:

    • 是的,sheet.isExpanded 设置在相邻的 div 中,&lt;div (click)="sheet.isExpanded = !sheet.isExpanded"&gt;
    • 更新了答案。尝试删除通配符('* => 扩展'→  '隐藏=> 扩展','* => 隐藏' → '扩展=> 隐藏')
    • 我试过但没有骰子。您的示例有效,但我似乎无法将其转化为我的具体问题。它在 ngFor 和 ngIf 中,所以它一定是与动画无关的问题。谢谢你的帮助。附言...你的名字就是我的名字:)
    • ;) 哦,你可以试试在ngFor里面给div分配id。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-29
    • 1970-01-01
    相关资源
    最近更新 更多