角度动画:动画父元素和子元素

Chr*_*oth 6 javascript animation angular angular-animations

我创建了div.parent一个效果很好的Angular动画元素()。当我向其添加子元素并尝试同时为其设置动画时,其中一个动画最终不会运行(它只是捕捉到新状态)。

Stackblitz: https ://stackblitz.com/edit/angular-2tbwu8

标记:

<div [@theParentAnimation]="state" class="parent">
  <div [@theChildAnimation]="state" class="child">
  </div>
</div>
Run Code Online (Sandbox Code Playgroud)

动画:

trigger( 'theParentAnimation', [
  state( 'down', style( {
    transform: 'translateY( 100% ) translateZ( 0 )',
  } ) ),
  transition( '* <=> *', [
    group( [
      query( ':self', [
        animate( '0.9s cubic-bezier(0.55, 0.31, 0.15, 0.93)' ),
      ] ),
      query( '@theChildAnimation', animateChild() ),
    ] ),
  ] ),
] ),
trigger( 'theChildAnimation', [
  state( 'down', style( {
    transform: 'translateY( 100% ) translateZ( 0 )',
  } ) ),
  transition( '* <=> *', [
    animate( '0.9s cubic-bezier(0.55, 0.31, 0.15, 0.93)' ),
  ] ),
] ),
Run Code Online (Sandbox Code Playgroud)

Mic*_*oye 7

看起来您不需要使用,query( ':self', ...因为您正在使用group()动画将并行运行。您可以在父级动画中更改转场:

transition('* <=> *', [
  group([
    query('@theChildAnimation', animateChild()),
    animate('0.9s cubic-bezier(0.55, 0.31, 0.15, 0.93)'),
  ]),
]),
Run Code Online (Sandbox Code Playgroud)

更新了StackBlitz

  • 谢谢你!这个答案帮助我学习了如何使用 animateChild()。说这些文档缺乏足够的解释是轻描淡写的。 (2认同)