- First import
BrowserAnimationsModule
in yourapp.module.ts
using this lineimport { BrowserAnimationsModule } from '@angular/platform-browser/animations';
. Remeber to put it in theimports
array. - In your
app.component.ts
import the following functions:trigger, state, style, animate, transition
from'@angular/animations'
;
- Create a
animationState
variable inside your component - Add a photo of an adorable face using this line:
<img src="https://api.adorable.io/avatars/120" [@faceAnimation]="animationState">
take a close look at:[@faceAnimation]="animationState"
- Create two buttons that change the
animationState
variable
<button (click)="changeState('round')">Round out</button>
<button (click)="changeState('bye')">Bye</button>
- Finally add this method that allow us to change the state:`
changeState(effect: string): void {
this.animationState = effect;
}
- Add the following property to the component's decorator
animations: [
trigger('faceAnimation', [
state('round', style({ borderRadius: '100%' })),
state('bye', style({ opacity: '0' })),
]),
]
```
2. Cool, right? Now let's add some transitions
```
trigger('faceAnimation', [
state('round', style({ borderRadius: '100%' })),
state('bye', style({ opacity: '0' })),
transition('* => round', [animate(1000, style({ borderRadius: '100%' }))]),
transition('* => bye', [animate(2000, style({ opacity: '0' }))]),
]),
```