×

Angular 8 Animation

What is Animation?

The animation is a process of drawing, designing, making layouts, and preparation of photographic sequences which are integrated into the multimedia and gaming products.

A reflection of movement created for displaying a series of frames, or pictures. Cartoons on television are one of the examples of animation.

Angular Animation

Animation provides the illusion of motion. HTML elements change styling over time. A well-designed animation can make our application more fun and easier to use, but they are not just cosmetic. Animation can improve our app and user experience in many ways:

  • Without animation, web page transitions can seem dull and fade.
  • Motion dramatically enhances the user experience, so animations give users a chance to detect the application's response to their actions.
  • Good animations directly call the user’s attention to where it is needed.

Typically, animation involves multiple style transformation over time. An HTML element can change color, move, grow, shrink, fade or slide off the page. These changes can happen simultaneously and sequentially. We also control the timing of every transformation.

Angular 8 Animation

Angular's animation system is built on the CSS functionality, that means we can animate any property that the browser considers animatable easily. It includes positions, sizes, transforms, colors, borders, and many more. The W3C maintains a list of animatable features on its CSS transitions page.

Set-up of Animation in Angular 8

  • Previously, we have to import new animation package through the command “npm install –save@angular/animations."
  • Then, add the BrowserAnimationsModule to our imports[] array in AppModule.
  • The module needs to be imported from @angular/platform-browser/animations’ => import { BrowserAnimationsModule } from ‘@angular/platform-browser/animations’( in AppModule).
  • After that, we import trigger, state, style, etc. from @angular/animations instead of @angular/core.

Step 1: Enabling the animations module

Import BrowserAnimationsModule, which introduces the animation capabilities in our angular root application module.

src/aap/app.module.ts

 import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
@NgModule({
imports: [ 
BrowserModule, 
BrowserAnimationsModule 
], 
declarations: [ ], 
bootstrap: [ ] 
})
export class AppModule { } 

Step 2: Importing animation functions into component file

If we plan to use specific animation functions in component files, import those functions from @angular/animations.

src/app/app.component.ts

import { Component, HostBinding } from '@angular/core';
import {
trigger, 
state, 
style, 
animate, 
transition, 
// ... 
} from '@angular/animations'; 

Step 3: Adding the animation metadata property

In the component file, add a metadata properly called animations in the @Component () decorator. We put the trigger which defines an animation within the animations metadata property.

src/app/app.component.ts

@Component({
selector: 'app-root', 
templateUrl: 'app.component.html', 
styleUrls: ['app.component.css'], 
animations: [ 
// animation triggers go here  
] 
}) 

For Example:

Angular 8 Animation 1

Animating a simple transition in Angular

We can specify that a button displays either open or closed based on the user's last action. When the button is in the free state, it's visible and yellow. When it is the closed state, it's transparent and green.

In HTML, these attributes are set using common CSS styles such as color and opacity. In Angular, use the style () functions to specify a set of CSS styles for use with animations.

Angular 8 Animation

Animation state and styles

We can use Angular's state() function to define different states to call at the end of each transition. This function has two arguments: a unique name such as open, closed, and style( ) function.

Use style() function to define a set of style to associate with a given state name.

In open state, the button has a height of 200 pixels, an opacity of 1, and a background color of yellow.

src/app/open-close.component.ts

// ...
state('open', style({
height: '200px', 
opacity: 1, 
backgroundColor: 'yellow' 
})), 

In the closed state, the button has height of 100 pixels, an opacity of 0.5, and a background color of green.

src/app/open-close.component.ts

state('closed', style({
height: '100px', 
opacity: 0.5, 
backgroundColor: 'green' 
})),  

Triggering the animation in Angular

An animation needs a trigger, so that it knows when to start it. The trigger () function collect the states and transitions, and gives the animation a name, so we can attach to the triggering element in HTML template.

The trigger() function describes the property name to watch for the changes. When a change occurs, the trigger commences the action included in its definition.

Angular 8 Animation

Attaching animation to HTML template in Angular

The animation is defined in the metadata of the component which controls the HTML element, which is animated. Put the code that defines our animation under the animations: property within the @Component () decorator.

src/app/open-close.component.ts

@Component(
selector: 'app-open-close',
animations: [
trigger('openClose', [
// ... 
state('open', style({
height: '200px',
opacity: 1,
backgroundColor: 'yellow'
})), 
state('closed', style({
height: '100px',
opacity: 0.5,
backgroundColor: 'green'
})), 
transition('open => closed', [ 
animate('1s') 
]),  
transition('closed => open', [ 
animate('0.5s') 
]),  
]),  
], 
templateUrl: 'open-close.component.html', 
styleUrls: ['open-close.component.css'] 
})
export class OpenCloseComponent {
isOpen = true; 
toggle() {
this.isOpen = !this.isOpen; 
} 
} 

When we have defined an animation trigger for a component, we can attach it to an element in the component's template by wrapping the trigger name in the bracket and introduce it with an @symbol.

When triggerName is the name of the trigger, and expression evaluates to a defined animation state.

<div[@triggerName]="expression">...</div>;

The animation will be executed or triggered when the expression value changes into a new state.

The given code binds the trigger to the value of the is open property.

src/app/open-close.component.html

<div [@openClose]="isOpen ? 'open' : 'closed'" class="open-close-container">
<p>The box is now {{ isOpen ? 'Open' : 'Closed' }}!</p> 
</div> 

Animation API core functions in Angular 8

The functional API is provided by the @angular/animations module. This module provides a domain-specific language (DSL) for creating and controlling animations in Angular applications.

So core functions and its related data structures.

Function name Features
trigger( ) This method starts the animation and will be served as a container for all other animation function calls. The HTML template binds to triggerName, and its first argument is used to declare a unique trigger name.
style( ) It defines one or more CSS styles to use in animations. Control the visual appearance of HTML elements during the animation.
state( ) The state creates a named set of CSS styles that should be applied on a successful transition to a state. The state can be referenced by name with other animation functions.
keyframe( ) It allows a sequence change between styles with an animate ( ).
animate( ) It specifies the timing information for a transition.
transition( ) This method defines the animation sequence between two named states. It also uses array syntax.
group( ) It specifies a group of animation steps to run in parallel. The animation continues only when all inner animation steps have completed.
query( ) Use to find or more inner HTML element within the current element.
stagger( ) Staggers the starting time for animation for multiple elements.
sequence( ) It specifies a list of animation steps that run sequentially one by one.
animation( ) It produces a reusable animation which can be invoked form useAnimation().
useAnimate() It activates a reusable animation.
animateChild() Allows animations on child components which run with the same timeframe as the parent.


Related Topics

Dynamic components in Angular 8

Dynamic components in Angular 8 The dynamic component is one of the versatile and core concept introduced in Angular, Component template is not fixed. An application needs to load new elements...

4 minutes read.

History and Versions of Angular 8

Introduction to Angular JS Misko created Angular JS and the first version of Angular also name as "Angular 1". He built a framework to handle the downfalls of HTML. The first...

3 minutes read.

Creating first APP in Angular 8

Firstly, we have to open Git Bash and, then we have to write the following command in it. ng new my -app The project has been created now so that now we have to go...

2 minutes read.

Angular 8 Directives

Angular 8 Directives: Directives are instructions in the DOM (Document Object Model). It specifies how to place our business logic in Angular. The directive is markers on a DOM element...

3 minutes read.

Angular 8 Libraries

Angular libraries are built a solution of general problems like presenting a unified user interface, presenting data, and allowing the data entry. Developers can create standard solutions for particular domains...

3 minutes read.

Angular 8 Components

Angular is used for building mobile and desktop web applications. The component is the basic building block of Angular. It has a selector, template, style, and other properties, and it...

4 minutes read.

Angular 8 changes and new features

The angular version number indicates the level of changes introduced by the release. The use of semantic versioning helps us understand the potential impact of updating to a new version. Angular is the...

5 minutes read.

Angular CLI Commands

All CLI commands of Angular Angular CLI is a command-line interface which is used to initialize, develop, and maintain Angular applications. We can use these Command on command prompt or consequentially by an associated...

8 minutes read.

Angular 8 Tutorial

What is Angular 8 Angular is a Framework of JavaScript used to build web and mobile applications. Angular 8 is a client-side TypeScript based structure which is used to create dynamic...

6 minutes read.

Scalability and Validation Forms in Angular 8

Form Validation Validation is an essential part of managing any set of forms. If we are checking for required fields or querying an external API for a username. Angular 8 provides a set of...

1 minute read.

Angular 8 Module

Angular 8 Module It is a collection of services, directives, controllers, filters, and configuration information. angular.module is used to configure the $injector. The module is a container of the different parts of an application....

6 minutes read.

Angular 8 Observables

Observables provide support for passing messages between publisher and subscription in our application. The observables can deliver the multiple values of any type like literal, messages, depending on the content. It is an...

6 minutes read.

Installation of Angular 8

Installation Guides to Angular 8 step by Step Angular combines declarative templates, dependency injection and, end to end tooling to solve development challenges. Angular empowers developers to build an application that lives on the...

4 minutes read.

Creating form in Angular 8

The angular reactive form is used to handle the user's input. We can use Angular form in the application to authorize the user to log in, to update profile, to enter information,...

2 minutes read.

What is AOT and JIT Compiler in Angular

A program written in a high-level language is translated into machine code or a low-level language by a piece of software called a compiler. HTML templates are the primary building...

3 minutes read.

Routing in Angular 8

Routing Angular Router is a powerful JavaScript router is built and maintained by the Angular core team that can install from the package @angular/router. Routing provides a complete routing library with the possibility of multiple...

4 minutes read.

Express.js app.get() Request Function

Express.js Express is a flexible online application framework for Node.js that offers a strong set of functionality for both web and mobile applications. While preserving the familiar and adored Node.js features,...

4 minutes read.

Angular 8 Forms

Angular forms are used to handle the user's input. We use Angular form in our application to authorize users to log in, to update profile, to enter information, and to perform many other...

3 minutes read.

ngSwitch Directive in Angular 8

Angular 8 ng-Switch Directive The ng-Switch Directive hides and shows the HTML elements depending on an expression. Child elements with the ng-switch-when directive will be displayed if it gets a match; otherwise, the component and...

2 minutes read.

Angular 8 Architecture

Architecture of Angular 8 Angular 8 is a platform and framework which is used to create clients applications in HTML and Typescript. Angular 8 is written in Typescript. Typescript is a...

5 minutes read.