Unlimited Plugins, WordPress themes, videos & courses! Unlimited asset downloads! From \$16.50/m

# How to Use Animation in Angular 6

Difficulty:BeginnerLength:ShortLanguages:

In this tutorial, you'll learn about the importance of animations in improving the user experience. I'll show you how to incorporate animations in Angular 6 applications with the help of animation components and Bootstrap.

## The Role of Animations in User Design

Most web users are visual creatures and therefore respond to visual objects. This means that as a designer, you have to find a way to incorporate animations in your designs.

Animation can prove to be a useful tool when it comes to user interaction with your website or app. However, this doesn't mean that when you use animation, users will be attracted to your site. You have to strike a balance with the number of animations used at a particular place and time.

Let's look at some scenarios in which animation can prove useful in user design.

• Providing Feedback: Animation can be used to acknowledge that a particular action has been received by the system. For example, when a user enters the wrong information, you can use the shake animation to let them know they need to enter their credentials again.
• Demonstration: Animation can also be used to demonstrate how certain aspects of applications work. This is done by providing visual hints which serve to teach users how the different UI elements operate.
• Transitions: Transition animation helps the user to keep their focus on the right area. When incorporating transition animations, keep in mind that the process should be smooth and effortless.

## Animation States and Transitions

Before we get started on adding animations into our Angular application, let's first look at the different animation states and transitions available in Angular.

These are the main states in Angular animations:

• void state: an element which is not in the view is represented by the void state
• wildcard (*) state: this represents any state

We will look at how these fit in an actual app when we start coding.

In order for an animation to change from one state to another, it needs to transition. The transition event has its own properties, which include:

• Duration: this property defines the life of an animation. This means from the start of the animation to the time it finishes.
• Delay: this property is used to define the time between the start of the actual transition and the time it is triggered.
• Easing: this property controls changes in the speed of an animation. An animation can start out slow and accelerate as it progresses and vice versa.

## Setting Up an Angular 6 Project

We will start by initializing a new Angular application as follows:

The next thing will be to enable animations by importing the BrowserAnimationsModule in app.module.ts as shown below.

Any animation functions that will be used are then imported from @angular/animations in the component file as follows:

#### Define the Animations

Finally, you define all your animations in the components decorator as shown.

The animation property is usually an array that contains one or more triggers as defined above. Each trigger, in addition, has a state and transition functions.

A simple animation property would look like this:

In the above animation, the name of the trigger is changeBtnColor, and we have two states: state1 and state2. Our element will be yellow in state1, and our animation will change its size and color when transitioning to state2.

#### Hook an Element to an Animation Trigger

Let's now hook our element to the trigger in the template as shown below.

Now go back to the component and define the function that listens to the button click.

Below are the before and after transformations of the animation.

Now that we have mastered the basics of Angular animation, let's go a little more in-depth with another example. In the next example, we will add animation to list items.

#### Animating List Items

Now open app.component.ts and update it as follows:

Here, we create an empty list and create a function for adding elements to the list and another for removing elements from the list.

Next, we will add the elements that will be applied to the animations. Update the template as follows:

We will start adding animations in the component. Open app.component.ts and add the following animations in the component decorator.

Here we have a trigger called AnimateList which has the :enter and :leave aliases.  enter represents the transition from void to * (void => *), while :leave represents the transition from * to void (* => void). During the :enter transition, the animation will wait for 600ms and run for 0.2s. The leave transition will wait for 0.1s and run with no delay.

We now need to update the template to use the animation we have defined above. The list items in app.component.html should look like this:

The final demo of the application should run as shown below.

## Conclusion

This tutorial has covered all that is necessary to get started with your own animations in Angular. For more information, head over to the animations guide and explore the full power of animations in Angular.