Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 5 Stagger Animation - How to do Reverse Order

While working with Angular CDK and developing a custom component, I am trying to implement stagger animation with ngIf and ngFor. The animation is a sequence of simple fade in.

The following simplified HTML:

<button (click)="visible = !visible">Toggle</button>
<div class="parent" @parentAnimation *ngIf="visible">
  <p class="child">Child 1</p>
  <p class="child">Child 2</p>
  <p class="child">Child 3</p>
</div>

And Component:

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ],
  animations: [
        trigger('parentAnimation', [
            transition('void => *', [
                query('.child', style({opacity: 0})),
                query('.child', stagger('500ms', [
                    animate('100ms .1s ease-out', style({opacity: 1}))
                ]))
            ]),
            transition('* => void', [
                query('.child', style({opacity: 1})),
                query('.child', stagger('500ms', [
                    animate('100ms .1s ease-out', style({opacity: 0}))
                ]))
            ])
        ])
    ]
})
export class AppComponent  {
   visible = false;
}

StackBlitz - https://stackblitz.com/edit/angular-5dj532

As can be seen in the link above, the issue is when hiding the elements, the order needs to be reversed (LIFO).

Looking at the stagger and query documentation, I could not find a built-in way to reverse the order.

Is there any proper way to implement this using angular animations?

like image 673
dev7 Avatar asked Jan 19 '18 05:01

dev7


People also ask

What is the use of Animatechild () function in angular?

Each time an animation is triggered in Angular, the parent animation has priority and any child animations are blocked. In order for a child animation to run, the parent animation must query each of the elements containing child animations, and run them using this function.

Which function applies cascading delay after each animation?

stagger() applies a cascading delay to animations for multiple elements. group() runs multiple animation steps in parallel. sequence() runs animation steps one after another.

What is stagger animation?

A staggered animation consists of sequential or overlapping animations. To create a staggered animation, use multiple Animation objects. One AnimationController controls all of the Animation s. Each Animation object specifies the animation during an Interval . For each property being animated, create a Tween .


1 Answers

Use negative timing on the second stagger:

....
query('.child', stagger('-500ms', [....
...

Demo

like image 178
Vega Avatar answered Oct 09 '22 17:10

Vega