Introduction
Animation is defined as the transition from an initial state to a final state. It is an integral part of any modern web application. Animation not only helps us to create a great UI but it also makes the application interesting and fun to use. A well-structured animation keeps the user glued to the application and enhances the user experience.
Angular allows us to create animations that provide us with a similar native performance as CSS animations. In this article, we will learn how we can create animation using Angular 6.
We will use Visual Studio Code for our demo.
Prerequisites
Install VS Code and Angular CLI.
If you are new to Angular, then refer to my previous article Getting Started With Angular 6.0 to set Angular 6 development environment on your machine.
Source Code
Download the source code from GitHub.
Understanding Angular Animation States
Animation is a transition from one state of the element to another state. Angular defines three different states for an element.
- Void state: A void state represents the state of an element that is not part of the DOM. This state occurs when an element is created but not yet placed in the DOM or the element is removed from the DOM. This state is useful when we want to create animation while adding or removing an element from our DOM. To define this state in our code we use the keyword void.
- The wildcard state: This is also known as the default state of the element. The styles defined for this state apply to the element regardless of its current animation state. To define this state in our code we use the * symbol.
- Custom state: This is the custom state of the element and it needs to be defined explicitly in the code. To define this state in our code we can use any custom name of our choice.
Animation Transition Timing
To show the animation transition from one state to another we define animation transition timing in our application.
Angular provides us with the following three timing properties.
Duration
This property represents the time our animation takes to complete from start (initial state) to finish (final state). We can define the duration of the animation in the following three ways.
- Using an integer value, which represents the time in milliseconds. E.g.- 500
- Using a string value to represent the time in milliseconds. E.g. - '500ms'
- Using a string value to represent the time in seconds. E.g. - '0.5s'
Delay
This property represents the time duration between the animation trigger and the beginning of the actual transition. This property also follows the same syntax as duration. To define the delay, we need to add the delay value after the duration value in a string format – '
Duration Delay'. Delay is an optional property.
e.g.
'0.3s 500ms'. This means the transition will wait for 500ms and then run for 0.3s
Easing
This property represents how the animation accelerates or decelerates during its execution. We can define the easing by adding it as the third variable in the string after duration and delay. If the delay value is not present, then easing will be the second value. This is also an optional property.
E.g.
- '0.3s 500ms ease-in': This means the transition will wait for 500ms and then run for 0.3s (300ms) with ease-in effect.
- '300ms ease-out': This means the transition will run for 300ms (0.3s) with ease-out effect.
Creating the Angular 6 application
Open the command prompt in your machine and execute the following set of commands.
- mkdir ngAnimationDemo
- cd ngAnimationDemo
- ng new animation
These commands will create a directory with the name “ngAnimationDemo” and then create an Angular application with the name “ngAnimation” inside that directory.
Open the animation app using VS code. Now we will create our component.
Navigate to View >> Integrated Terminal. This will open a terminal window in VS Code.
Execute the following command to create the component.
ng g c animationdemo
This will create our component “animation demo” inside the/src/app folder.
To use Angular animation we need to import “BrowserAnimationsModule” which is an animation-specific module in our application. Open the app.module.ts file and include the import definition as shown below.
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
// other import definitions
@NgModule({ imports: [BrowserAnimationsModule // other imports] })
Understanding the Angular Animation Syntax
We will write our animation code inside the component’s metadata. The syntax for animation is as shown below,
@Component({
// other component properties
animations: [
trigger('triggerName', [
state('stateName', style()),
transition('stateChangeExpression', [Animation Steps])
])
]
})
Here we will use a property called “animations”. This property will take an array as input. The array contains one or more “triggers”. Each trigger has a unique name and an implementation. The state and transitions for our animation need to be defined in the trigger implementation.
Each state function has a “stateName” defined to uniquely identify the state and a style function to show the style of an element in that state.
Each transition function has a “stateChangeExpression” defined to show the change of state of an element and the corresponding array of animation steps to show how the transition will take place. We can also include multiple trigger functions inside the animation property as comma-separated values.
These functions trigger, state, and transition are defined in the "@angular/animations" module. Hence, we need to import this module into our component.
To apply animation on an element we need to include the trigger name in the element definition. Include the trigger name followed by the @ symbol in the element tag. Refer to the sample code below,
<div @changeSize></div>
This will apply the trigger “changeSize” to the <div> element.
Let us create a few animations to get a better understanding of the Angular animation concepts.
Change Size Animation
We will create an animation to change the size of a <div> element on a button click.
Open the animationdemo.component.ts file and add the following import definition.
import { trigger, state, style, animate, transition } from '@angular/animations';
Add the following animation property definition in the component metadata.
animations: [
trigger('changeDivSize', [
state('initial', style({
backgroundColor: 'green',
width: '100px',
height: '100px'
})),
state('final', style({
backgroundColor: 'red',
width: '200px',
height: '200px'
})),
transition('initial=>final', animate('1500ms')),
transition('final=>initial', animate('1000ms'))
]),
]
Here we have defined a trigger “changeDivSize” and two state functions inside the trigger. The element will be of green color in the “initial” state and will be of red color with an increased width and height in the “final” state.





Satish Kumar VadlavalliPosted Oct 15, 2018, 10:34 PM
Great work Ankit, happy to see
Kapil Singh KumawatPosted Sep 23, 2018, 1:36 PM
Nice article.