Angular — Animations
Setup Animations
import { trigger, state, style, animate, transition } from '@angular/animations';
@Component({
animations: [
trigger('fade', [
transition(':enter', [
style({ opacity: 0 }),
animate('300ms', style({ opacity: 1 }))
]),
transition(':leave', [
animate('300ms', style({ opacity: 0 }))
])
])
]
})
Basic Animations
animations: [
trigger('slide', [
state('in', style({ transform: 'translateX(0)' })),
state('out', style({ transform: 'translateX(-100%)' })),
transition('in => out', animate('300ms ease-out')),
transition('out => in', animate('300ms ease-in'))
])
]
In Template
<div @fade *ngIf="show">Content</div>
<div [@slide]="state">Sliding content</div>
Route Animations
// app.component.ts
animations: [
trigger('routeAnimation', [
transition('* <=> *', [
style({ opacity: 0 }),
animate('0.3s', style({ opacity: 1 }))
])
])
]
Animation Callbacks
<div @fade
(@fade.start)="onStart($event)"
(@fade.done)="onDone($event)">
Content
</div>
Mini Practice
- Create enter/leave animations
- Add state transitions
- Implement route animations
- Use animation callbacks
Up Next
Continue with Guards — route guards.
Related Topics
Frequently Asked Questions about Animations
What is Animations in Angular?
Animations is a fundamental concept in Angular. This lesson explains it step by step with clear examples, making it easy for beginners to understand.
How do I learn Animations?
Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Animations.
Why is Animations important in Angular?
Animations is essential for Angular development. Understanding this concept will help you write better code and solve real-world problems more effectively.