Advanced Animations in Angular
Animations enhance the user experience by adding visual feedback to user interactions. Angular provides a powerful animation module that allows you to create advanced animations with ease. This guide covers various advanced animation techniques in Angular.
Setting Up Angular Animations
First, import the Angular animations module in your app module:
// app.module.ts
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
@NgModule({
declarations: [AppComponent],
imports: [BrowserModule, BrowserAnimationsModule],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
Creating Complex Animations
Create complex animations using Angular's animation DSL (domain-specific language):
// app.component.ts
import { Component } from '@angular/core';
import { trigger, state, style, transition, animate, keyframes, query, stagger } from '@angular/animations';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css'],
animations: [
trigger('fadeInOut', [
state('void', style({ opacity: 0 })),
transition(':enter', [
animate(300, style({ opacity: 1 }))
]),
transition(':leave', [
animate(300, style({ opacity: 0 }))
])
]),
trigger('listAnimation', [
transition('* => *', [
query(':enter', [
style({ opacity: 0, transform: 'translateY(-100px)' }),
stagger('50ms', [
animate('500ms ease-out', style({ opacity: 1, transform: 'translateY(0)' }))
])
], { optional: true }),
query(':leave', [
stagger('50ms', [
animate('500ms ease-in', style({ opacity: 0, transform: 'translateY(-100px)' }))
])
], { optional: true })
])
])
]
})
export class AppComponent {
items = ['Item 1', 'Item 2', 'Item 3'];
addItem() {
this.items.push(`Item ${this.items.length + 1}`);
}
removeItem() {
this.items.pop();
}
}
Using Keyframes
Keyframes allow you to define intermediate steps in an animation sequence:
// app.component.ts
trigger('bounce', [
transition('* => *', [
animate('1s', keyframes([
style({ transform: 'translateY(0)', offset: 0 }),
style({ transform: 'translateY(-30px)', offset: 0.5 }),
style({ transform: 'translateY(0)', offset: 1 })
]))
])
])
Animating Routes
Animate route transitions using Angular's built-in router animations:
// app-routing.module.ts
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { Page1Component } from './page1/page1.component';
import { Page2Component } from './page2/page2.component';
const routes: Routes = [
{ path: 'page1', component: Page1Component, data: { animation: 'Page1' } },
{ path: 'page2', component: Page2Component, data: { animation: 'Page2' } }
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }
// app.component.ts
import { Component } from '@angular/core';
import { RouterOutlet } from '@angular/router';
import { trigger, transition, style, query, group, animate } from '@angular/animations';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css'],
animations: [
trigger('routeAnimations', [
transition('Page1 <=> Page2', [
style({ position: 'relative' }),
query(':enter, :leave', [
style({
position: 'absolute',
top: 0,
left: 0,
width: '100%'
})
], { optional: true }),
query(':enter', [
style({ left: '-100%' })
], { optional: true }),
query(':leave', [
style({ left: '0%' })
], { optional: true }),
group([
query(':leave', [
animate('300ms ease-out', style({ left: '100%' }))
], { optional: true }),
query(':enter', [
animate('300ms ease-out', style({ left: '0%' }))
], { optional: true })
])
])
])
]
})
export class AppComponent {
prepareRoute(outlet: RouterOutlet) {
return outlet && outlet.activatedRouteData && outlet.activatedRouteData['animation'];
}
}
Reusable Animations
Create reusable animations for consistency across your application:
// animations.ts
import { trigger, state, style, transition, animate } from '@angular/animations';
export const fadeInOut = trigger('fadeInOut', [
state('void', style({ opacity: 0 })),
transition(':enter', [
animate(300, style({ opacity: 1 }))
]),
transition(':leave', [
animate(300, style({ opacity: 0 }))
])
]);
// app.component.ts
import { Component } from '@angular/core';
import { fadeInOut } from './animations';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css'],
animations: [fadeInOut]
})
export class AppComponent {
// Component logic here
}
Key Points
- Import the Angular animations module in your app module to enable animations.
- Create complex animations using Angular's animation DSL.
- Define intermediate steps in an animation sequence using keyframes.
- Animate route transitions using Angular's built-in router animations.
- Create reusable animations for consistency across your application.
Conclusion
Advanced animations in Angular enhance the user experience by adding visual feedback to user interactions. By setting up Angular animations, creating complex animations, using keyframes, animating route transitions, and creating reusable animations, you can create a dynamic and engaging application. Happy animating!