Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 2/4+ Router animation, wait for previous route animation to complete

I have the following animation configured on 2 of my routes.

trigger('routeAnimation', [
        transition(':enter', [
            style({ opacity: 0 }),
            animate('1s', style({ backgroundColor: 1 }))
        ]),
        transition(':leave', [
            style({ backgroundColor: 1 }),
            animate('1s', style({ backgroundColor: 0 }))
        ]),
    ]),

They work properly. Both get executed when switching from route A to B. However they execute simultaneously. I would like for the TO route to wait for the FROM route's exit animation to finish before coming in.

I want A to completely fade out before B comes in.

Is that possible? Maybe with lifecycle hooks or something?

like image 647
Robert Broersma Avatar asked Dec 08 '25 08:12

Robert Broersma


1 Answers

To wait for the leaving route's animation to finish before the entering route's animation begins:

const fadeIn = [
  query(':leave', style({ opacity:1 })),
  query(':enter', style({ opacity:0 })),
  query(':leave', animate('200ms', style({ opacity:0 }))),
  query(':enter', animate('200ms', style({ opacity:1 })))
]

...

trigger('routerAnimations', [
      transition('route1 => route2', fadeIn)
]

The animations will play in sequence.

(Updated after suggesting to delay the entering route by the duration of the leaving route, leading to a verbose solution)

like image 78
SpaceFozzy Avatar answered Dec 09 '25 22:12

SpaceFozzy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!