Implementing animations in Angular 2 can enhance the user experience.
To see a live demonstration, visit this link
app.component.ts
@Component({
selector: 'my-app',
animations: [
trigger('displayName', [
state('in', style({ opacity: 1, transform: 'translateY(0)' })),
state('out', style({ opacity: 0, display: "none", transform: 'translateY(100%)' })),
transition('in => out', [
style({ transform: 'translateY(0)', opacity: 1 }),
animate('0.5s', style({ transform: 'translateY(100%)', opacity: 0 }))
]),
transition('out => in', [
style({ transform: 'translateY(100%)', opacity: 0 }),
animate('0.5s ease-out', style({ transform: 'translateY(0)', opacity: 1 }))
])
])
]
})
export class App {
name:string;
currentIndex: number;
students: [] = [
{
name: "Alice",
animationState: 'in'
},
{
name: "Bob",
animationState: 'out'
},
{
name: "Charlie",
animationState: 'out'
}
];
constructor() {
this.name = 'Angular Animations';
this.currentIndex = 0;
}
ngOnInit() {
setInterval((function(){
console.log(this.currentIndex);
this.students[this.currentIndex].animationState = 'out';
this.currentIndex++;
if(this.currentIndex >= this.students.length) {
this.currentIndex = 0;
}
this.students[this.currentIndex].animationState = 'in';
}).bind(this), 4000);
}
}
html
<div>
<h2>Greetings, {{name}}!</h2>
</div>
<div *ngFor="let student of students" [@displayName]="student.animationState">
Hello, {{student.name}}
</div>