ts:
import { Component } from '@angular/core';
import {animate, state, style, transition, trigger} from '@angular/animations';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ],
animations: [
trigger('fadeInOutAnimation', [
state('out', style({
opacity: 0
})),
state('in', style({
opacity: 1
})),
transition('out=>in', [
animate('5s')
])
])
]
})
export class AppComponent {
name = 'Angular';
currentState = 'out';
changeState() {
this.currentState = this.currentState === 'in' ? 'out' : 'in';
}
}
html:
<div
*ngIf="isMenuOpen"
(click)="changeState()"
[@fadeInOutAnimation]="currentState"
class="shadow-background"
></div>
<button (click)="isMenuOpen = true">Animate</button>
stackblitz: https://stackblitz.com/edit/angular-qzn3es