Staggering Angular2 animations within *ngFor

前端 未结 3 1720
南旧
南旧 2020-12-09 07:10

I\'ve been digging around the Angular2 documentation and there doesn\'t seem to be a simple way to add delays to animations. For reference, here is what I\'m aiming to achie

相关标签:
3条回答
  • 2020-12-09 07:30

    Since Angular 4.2.6 you can use query(), stagger() and animateChild() to stagger all kinds of animations. Here's an example:

    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/

    0 讨论(0)
  • 2020-12-09 07:33

    After hammering my head against the animation DSL to make staggering animations a thing. I found an alternative way of doing animations which allows staggering!

    The trick is to have a directive responsible of the animation using the Renderer and Service to hold your animation store!

    Directive important code

    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();
    

    How to use it in template

    <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>
    

    I made a working plunkr with everything you need!

    plunkr

    0 讨论(0)
  • 2020-12-09 07:36

    The staggering module is still not ready. But there are some hacky paths to archieve the same effect.

    1. If you have a fixed list in size (or at least at screen). You can write a stagger function that returns a delayed animation for every state throught 0 to n. the stagger function should return AnimationEntryMetadata[] Then. you should pass bind the @expandSkill to the variable i on the loop. On the end, you will be creating dynamic, in animations for every object.

    2. Another option you have, is use, the Renderer plus a ViewChildren, query the childs, and create the stagger animation with css styles. The renderer provides a setElementStyle.

    3. There is a thirth option, a little more hacky.. Render a new list, an populate it throught a setTimeout.

    I used the two for some rich animations. Sure there are better aproaches :)

    0 讨论(0)
提交回复
热议问题