Angular course ยท Module 13: UI and Styling
Animations in Angular
In this lesson4
Animations are like fluid movements in kata - choreography that brings the application to life.
Configuration
// app.config.ts
import { provideAnimationsAsync } from '@angular/platform-browser/animations/async';
export const appConfig: ApplicationConfig = {
providers: [
provideAnimationsAsync() // Lazy loading of animations
]
};Basic Animations
import {
trigger,
transition,
style,
animate,
state,
query,
stagger,
keyframes,
group
} from '@angular/animations';
@Component({
selector: 'app-samurai-list',
animations: [
// Fade in/out
trigger('fadeInOut', [
transition(':enter', [
style({ opacity: 0, transform: 'translateY(-20px)' }),
animate('300ms ease-out', style({ opacity: 1, transform: 'translateY(0)' }))
]),
transition(':leave', [
animate('200ms ease-in', style({ opacity: 0, transform: 'translateY(20px)' }))
])
]),
// Active/inactive state
trigger('activeState', [
state('inactive', style({
transform: 'scale(1)',
opacity: 0.8
})),
state('active', style({
transform: 'scale(1.05)',
opacity: 1,
boxShadow: '0 4px 20px rgba(0,0,0,0.2)'
})),
transition('inactive <=> active', animate('200ms ease-in-out'))
]),
// List with stagger effect
trigger('listAnimation', [
transition('* => *', [
query(':enter', [
style({ opacity: 0, transform: 'translateX(-50px)' }),
stagger('50ms', [
animate('300ms ease-out', style({ opacity: 1, transform: 'translateX(0)' }))
])
], { optional: true })
])
])
],
template: `
<div [@listAnimation]="samuraiList().length">
@for (samurai of samuraiList(); track samurai.id) {
<div
class="samurai-card"
[@fadeInOut]
[@activeState]="samurai.id === selectedId() ? 'active' : 'inactive'"
(click)="select(samurai.id)"
>
{{ samurai.name }}
</div>
}
</div>
`
})
export class SamuraiListComponent {
samuraiList = signal<Samurai[]>([]);
selectedId = signal<number | null>(null);
select(id: number): void {
this.selectedId.set(id);
}
}Keyframe Animations
trigger('attack', [
transition('* => attacking', [
animate('500ms', keyframes([
style({ transform: 'translateX(0)', offset: 0 }),
style({ transform: 'translateX(100px) rotate(45deg)', offset: 0.5 }),
style({ transform: 'translateX(0)', offset: 1 })
]))
])
])Group Animations
trigger('cardFlip', [
transition('front => back', [
group([
animate('300ms', style({ transform: 'rotateY(180deg)' })),
query('.front', animate('300ms', style({ opacity: 0 }))),
query('.back', animate('300ms', style({ opacity: 1 })))
])
])
])Spotted a mistake in this lesson?
Check yourself
Answer the questions from this lesson. Pick an answer to see right away whether it is correct.
1. What does ':enter' represent in an Angular animations transition?
2. What is the stagger() function used for in Angular animations?
These are 2 of 3 questions for this lesson. Solve the rest in the game.
Hands-on tasks in the game
- Horizontal ordering
Arrange on a single line a call to animate() with a duration and an end style.
- Code editor
Complete the fadeIn animation for an appearing element.
- Vertical ordering
Order the lines of an entry animation definition (top-to-bottom order in the editor).
- Click in order
Build a transition between two animation states: transition('inactive <=> active', ...)