Open In App

Angular10 Trigger Animation

In this article, we are going to see what is trigger in Angular 10 and how to use it.

The trigger in Angular10 is used to create an animation trigger containing state and transition of the animation.



Syntax:

animate(name | definitions)

NgModule: Module used by trigger is:



 

Approach: 

Parameters:

Return Value:

Example:




import { LOCALE_ID, NgModule } 
from '@angular/core';
  
import { BrowserModule }
from '@angular/platform-browser';
import {BrowserAnimationsModule} 
from '@angular/platform-browser/animations';
import { AppRoutingModule }
from './app-routing.module';
import { AppComponent }
from './app.component';
  
@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
    BrowserAnimationsModule
  ],
  providers: [
      { provide: LOCALE_ID, useValue: 'en-GB' },
  ],
  bootstrap: [AppComponent]
})
export class AppModule { }




import { 
  // Trigger is imported here
  trigger, 
  state, 
  style, 
  transition, 
  animate } from '@angular/animations';
import { Component } from '@angular/core';
  
@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ],
  animations: [
  
    // Trigger is used here
    trigger('geek',[
      state('green', style({
        'background-color': 'green',
        transform: 'translateX(0)'
      })),
      state('blu', style({
        'background-color': '#49eb34',
        transform: 'translateX(0)'
      })),
      transition('green => blu',animate(1200)),
      transition('blu => green',animate(1000))
    ])
  ]
})
export class AppComponent  {
  state = 'green';
  anim(){
    this.state == 'green' ?
    this.state = 'blu' : this.state = 'green';
  }
}




<h1>GeeksforGeeks</h1>
<button (click)='anim()'>Animate</button>
<br>
<br>
<div 
  style="width: 150px; height: 100px; border-radius: 5px;"
  [@geek]='state'>
</div>

Output:

Reference: https://angular.io/api/animations/trigger


Article Tags :