【问题标题】:Staggering Angular2 animations within *ngFor在 *ngFor 中交错 Angular2 动画
【发布时间】:2017-02-03 18:30:27
【问题描述】:

我一直在研究 Angular2 文档,似乎没有一种简单的方法可以为动画添加延迟。作为参考,这是我的目标:plunkr using jQuery

我想使用 Angular2 的动画功能,因为这些“条”是在循环中生成的。它们的动画效果很好,但同时进行。我想以 1s 的增量错开它们。这是我的主要组件文件:

import {
  Component,
  Input,
  trigger,
  state,
  style,
  transition,
  animate
} from '@angular/core';


export class Skill {
  skill: string;
  level: number;
}

const SKILLS: Skill[] = [
    { skill: 'x', level: 70 },
    { skill: 'y', level: 100 },
    { skill: 'z', level: 80 }
]

@Component({
  selector: 'app-wrap',
  template: `
    <div *ngFor="let skill of skills; let i = index" class="skill">
      <span class="bar" [style.width.%]="skill.level" [@expandSkill]>&nbsp;</span>
    </div>
  `,
  animations: [
    trigger('expandSkill', [
      state('in', style({ width: 'auto' })),
      transition('void => *', [
        style({ width: '0' }),
        animate('1000ms ease-in-out')
      ])
    ]
  ]
})

export class AppComponent {
  skills = SKILLS;
}

我遇到了这个看起来很相似的other SO question,但它是在几个月前被问到的,在最终发布之前。

【问题讨论】:

    标签: angular angular2-template


    【解决方案1】:

    交错模块仍未准备好。但是有一些 hacky 路径可以实现相同的效果。

    1. 如果您有一个固定大小的列表(或至少在屏幕上)。您可以编写一个交错函数,该函数返回从 0 到 n 的每个状态的延迟动画。交错函数应该返回 AnimationEntryMetadata[] 然后。您应该将 @expandSkill 绑定到循环中的变量 i 。最后,您将为每个对象创建动态的动画。

    2. 您拥有的另一个选项是使用 Renderer 和 ViewChildren,查询子项,并使用 css 样式创建交错动画。渲染器提供了一个 setElementStyle。

    3. 还有第三个选项,有点老套。渲染一个新列表,并通过 setTimeout 填充它。

    我将两者用于一些丰富的动画。当然有更好的方法:)

    【讨论】:

      【解决方案2】:

      在我对动画 DSL 进行了猛烈抨击以使惊人的动画成为一件事之后。我找到了另一种制作动画的方法,它允许惊人!

      诀窍是让一个指令负责使用渲染器和服务来保存动画存储!

      指令重要代码

      this.animation = this.renderer.animate(
        this.element.nativeElement.firstElementChild || this.element.nativeElement,
        this.animService.getAnimation(animationName).startingStyles,
        this.animService.getAnimation(animationName).keyframes,
        this.duration,
        this.delay,
        this.easing
      );
      this.animation.pause();
      this.animation.play();
      

      如何在模板中使用

      <div *ngFor="let str of ['foo','bar','baz']; let i = index"
        anim-aes
        [anim-aes-delay]="i*200"
        [anim-aes-duration]="500"
        [anim-aes-animation]="'fadeIn'"
        [anim-aes-animation-leave]="'fadeOut'"
        [anim-aes-play]="show">
        click {{str}}
      </div>
      

      我用你需要的一切做了一个工作 plunkr!

      plunkr

      【讨论】:

      • 刚刚注意到它不再工作了(可能是 Angular 2 更新改变了一些东西)看看我能做什么!感谢您的反馈
      • 此方法不适用于 Angular 4 :(((( 请参阅此帖子github.com/angular/angular/issues/15294
      【解决方案3】:

      从 Angular 4.2.6 开始,您可以使用 query()、stagger() 和 animateChild() 来错开各种动画。这是一个例子:

      template: `
          <div [@stagger]>
            <div [@fade] *ngFor="let item of items;">{{item}}</div>
          </div>
        `,
      animations: [
          trigger('fade', [
            transition(':enter', [style({opacity: 0}), animate('.6s ease')])
          ]),
          trigger('stagger', [
            transition(':enter', [
              query(':enter', stagger('.3s', [animateChild()]))
            ])
          ])
        ]
      

      Plunker:https://embed.plnkr.co/pQQSZETi7mnToB6lqfID/

      【讨论】:

      • 它有一堆问题,而且对开发者来说不好用......例如,没有办法暂停它!
      猜你喜欢
      • 1970-01-01
      • 2017-07-11
      • 1970-01-01
      • 2017-12-18
      • 2017-07-26
      • 1970-01-01
      • 2017-11-08
      • 1970-01-01
      • 2017-12-10
      相关资源
      最近更新 更多