0
0
Angularframework~10 mins

Route transition animations in Angular - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Route transition animations
User triggers route change
Angular Router starts transition
Animation trigger activates
Old component animates out
New component animates in
Transition completes, new route displayed
Shows how Angular triggers animations during route changes, animating old and new components smoothly.
Execution Sample
Angular
import { trigger, transition, style, animate, query } from '@angular/animations';

export const fadeAnimation = trigger('routeAnimations', [
  transition('* <=> *', [
    style({ position: 'relative' }),
    query(':enter, :leave', [
      style({
        position: 'absolute',
        top: 0,
        left: 0,
        width: '100%',
        height: '100%'
      })
    ], { optional: true }),
    query(':enter', [
      style({ opacity: 0 })
    ], { optional: true }),
    query(':leave', [
      animate('300ms ease-in-out', style({ opacity: 0 }))
    ], { optional: true }),
    query(':enter', [
      animate('300ms ease-in-out', style({ opacity: 1 }))
    ], { optional: true })
  ])
]);
Defines a simple fade animation triggered on any route change.
Execution Table
StepTriggerAnimation StateActionResult
1User clicks link to new routeNo animationRouter starts route changeOld component visible
2Route change detectedAnimation trigger firesOld component animates out (fade out)Old component fades out
3Old component animation endsPrepare new componentNew component loads hiddenNew component ready but invisible
4New component animation startsAnimate inNew component animates in (fade in)New component becomes visible
5Animation completesAnimation endsTransition finishesNew route fully displayed
6IdleNo animationWaiting for next route changeStable UI
💡 Animation completes and new route is fully displayed, waiting for next user action.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
oldComponentVisibilityvisiblefading outhiddenhiddenhidden
newComponentVisibilityhiddenhiddenhiddenfading invisible
animationStatenonerunning fade outpreparing fade inrunning fade innone
Key Moments - 3 Insights
Why does the old component animate out before the new one animates in?
Because Angular runs the 'leave' animation first to smoothly remove the old view before showing the new one, as shown in execution_table steps 2 and 3.
What triggers the animation to start on route change?
The Angular Router detects the route change and triggers the animation defined by the 'routeAnimations' trigger, as seen in step 2 of the execution_table.
Why is the new component initially hidden before animating in?
It is hidden to avoid flicker and to allow the fade-in animation to run smoothly, as shown in step 3 where the new component is ready but invisible.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the animationState at step 4?
Arunning fade out
Bpreparing fade in
Crunning fade in
Dnone
💡 Hint
Check the 'Animation State' column at step 4 in the execution_table.
At which step does the old component become hidden?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Look at 'oldComponentVisibility' in variable_tracker after each step.
If the animation duration changes from 300ms to 600ms, what changes in the execution_table?
ASteps remain same but animation actions take longer
BAnimation trigger fires later
CNew component loads earlier
DOld component does not animate out
💡 Hint
Duration affects timing but not the order of steps in execution_table.
Concept Snapshot
Angular route transition animations:
- Use trigger() with transition() to define animations
- Animate old component out before new component in
- Use '* <=> *' to animate all route changes
- Animations run on route change events
- Helps smooth UI changes between pages
Full Transcript
When a user changes routes in Angular, the router detects this and triggers animations defined by the developer. First, the old component animates out, usually fading or sliding away. Then the new component loads hidden and animates in, creating a smooth transition effect. This process involves animation triggers that listen for route changes and run animations in sequence. Variables track visibility and animation states to manage the UI during the transition. This approach improves user experience by avoiding abrupt content changes.