Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular animation: first fadeout, then fadeIn

I have the following HTML

<div @fadeInOut *ngIf="state">State is true</div>
<div @fadeInOut *ngIf="!state">State is false</div>

<button (click)="state = !state">Toggle</button>

And I want to animate the :leave and :enter transition. Very trivial I would say

I can do this with the following animation

 trigger("fadeInOut", [
      transition(":enter", [
         style({ opacity: 0 }),
         animate(1000, style({ opacity: 1 }))
      ]),
      transition(":leave", [animate(1000, style({ opacity: 0 }))])
 ])

But the problem is that as soon as the transition begins both elements are visible. What I want is that first the :leave goes away, and then the :enter appears. What is the correct way to achieve this?

like image 985
Jeanluca Scaljeri Avatar asked Aug 31 '25 17:08

Jeanluca Scaljeri


1 Answers

You can achieve it by adding delay for the :enter animation equal to the length of the :leave animation

trigger("fadeInOut", [
  transition(":enter", [
    style({ opacity: 0 }),
    animate("1000ms 1000ms", style({ opacity: 1 }))
  ]),
  transition(":leave", [animate(1000, style({ opacity: 0 }))])
])

Reference: https://angular.io/api/animations/animate

like image 58
T. Sunil Rao Avatar answered Sep 02 '25 12:09

T. Sunil Rao