How to use AngularJS animation module to add animation effects to your app

share link

by vsasikalabe dot icon Updated: May 8, 2023

technology logo
technology logo

Solution Kit Solution Kit  

In AngularJS, we can create animation effects that support CSS-based animations. We can also do many style transformations over time. Using an HTML element, we can move the content, change color, grow or shrink, fade, or slide off the page. These animations can occur concurrently. You can control the timing of each change. The animation library utilizes the animation property like adding and removing classes. It uses the Web animations API for CSS keyframes if we don't support the Web animations API. 


The different animation properties in angular animations: 

  • Positions 
  • Size 
  • Transforms 
  • Colors 
  • borders 


The App animations depend on the CSS web transition functionality. We can style any method using CSS. We also apply the angular animations with more control on the developer side. We can implement the angular animations using CSS transition. Using the Angular CLI Angular makes very easy animations. The code for route animation and regular html animation or components are the same. Based on the use of keyframes or transitions, we can use either or listen to the animation's end event. We are using the ngAnimate module to create animation effects in AngularJS. The wildcard and void are default states, so we cannot use them within our trigger. The parent animation has more priority on every animation trigger than the children. We have control of the element's state. Without any bindings, we can add animation triggers to the element. 


Well-designed animations can make your application more entertaining and simpler to use. Animations can improve your application with user experience in several ways: 

  • Without animations, the web page appearance is abrupt and jarring. 
  • Motion greatly increases the user experience. Animation also provides the illusion of motion on a page. 

Good animations call the user's attention to where we need it. 


Import BrowserModule in the root AppModule. It provides services for launching and running a browser application. NgModule does the following: 

  • It declares which components and directives belong to the module. 
  • Makes some components and directives public. 
  • It provides services that other application components can use. So other modules' templates can use everywhere in the code. 

We use jQuery fadeTo() functionality to animate the 'App Loading'. It messages before the root component loads. We can import the trigger from angular animation modules. This method encloses a single-element animation inside the animations array. The state () method describes the final state of the animation. It applies CSS properties to the target element. By invoking style(), styles get passed into its argument the desired styles as an object. 


Here is an example of using the AngularJS animation module to add animation effects to your app: 

Fig: Preview of the output that you will get on running this code from your IDE.

Code

<!DOCTYPE html>
<html>

  <head>
    <base href="." />
    <title>angular playground</title>
    <link rel="stylesheet" href="style.css" />
    <script src="https://unpkg.com/core-js@2.4.1/client/shim.min.js"></script>
    <script src="https://unpkg.com/zone.js/dist/zone.js"></script>
    <script src="https://unpkg.com/zone.js/dist/long-stack-trace-zone.js"></script>
    <script src="https://unpkg.com/reflect-metadata@0.1.3/Reflect.js"></script>
    <script src="https://unpkg.com/systemjs@0.19.31/dist/system.js"></script>
    <script src="config.js"></script>
    <script>
      System.import('app')
        .catch(console.error.bind(console));
    </script>

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

  </head>

  <body>
    <style>
      #animate {
        width: 100%;
        height: 250px;
        position: absolute;
        background-color: red;
      }
    </style>

    <my-app>
        <div id="animate"><h1>App loading, please wait.</h1></div>
    </my-app>

    <script>
      $( "#animate" ).fadeTo( 750 , 0.1, function() {
          // Animation complete.
        });
   </script>

  </body>

</html>

import {Component, NgModule, Input, VERSION} from '@angular/core'
import {BrowserModule} from '@angular/platform-browser'
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';

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

@Component({
  selector: 'my-app',
  template: `
    <div [@flyInOut]="'out'">
      <h2>Hello {{name}}</h2>
    </div>
  `,
  animations: [
    trigger('flyInOut', [
      state('out', style({opacity: 0, transform: 'translateX(0)'})),
      transition('void => *', [
        style({
          opacity: 1,
          //transform: 'translateX(-100%)'
        }),
        animate('1s ease-in')
      ]),
      transition('* => void', [
        animate('0.2s 10 ease-out', style({
          opacity: 0,
          transform: 'translateX(100%)'
        }))
      ])
    ])
  ]
})
export class App {
  name:string;
  constructor() {
    this.name = `Angular! v${VERSION.full}`
  }
}

@NgModule({
  imports: [ BrowserModule, BrowserAnimationsModule ],
  declarations: [ App ],
  bootstrap: [ App ]
})
export class AppModule {}

Instructions

Follow the steps carefully to get the output easily.

  1. Install Visual Studio Code on your computer.
  2. Open the terminal, and install Angular using this command-'npm install -g @angular/cli'.
  3. Create a new folder using the command-'ng new folder_name'.
  4. If you are getting an error for ng then, add set-ExecutionPolicy RemoteSigned -Scope CurrentUser, Get-ExecutionPolicy, and Get-ExecutionPolicy -list in the terminal one by one.
  5. Now, Create a new folder using the command-'ng new folder_name'.
  6. Choose yes to all the questions and choose SCSS for the stylesheet format.
  7. cd foldername
  8. Click on the snippet topic we will get index.html and app.ts files separately.
  9. Copy the index.html code using the Copy button above and paste it into your index.html file under src.
  10. Create app.ts file in the src folder and copy this code using the Copy button above and paste it into your app.ts file (newly created).
  11. Save these two files and open the terminal, Run the Code using this command-'ng serve' to generate the output in localhost.


I hope you found this helpful.


I found this code snippet by searching for ' animation in index.html in Angular App' in kandi. You can try any such use case!

Environment Tested

I tested this solution in the following versions. Be mindful of changes when working with other versions.

  1. The solution is created and tested in Visual Studio Code 1.74.1.
  2. The solution is tested in Angular CLI - 15.2.2.
  3. angular animations version - 15.2.0.


Using this solution, we are able to use AngularJS animation module to add animation effects to your app with simple steps. This process also facilities an easy way to use, hassle-free method to create a hands-on working version of code which would help us to use AngularJS animation module to add animation effects to your app.

Dependent Libraries

angular-appby angular-app

JavaScript doticonstar image 5888 doticonVersion:Currentdoticon
License: Permissive (MIT)

Reference application for AngularJS

Support
    Quality
      Security
        License
          Reuse

            angular-appby angular-app

            JavaScript doticon star image 5888 doticonVersion:Currentdoticon License: Permissive (MIT)

            Reference application for AngularJS
            Support
              Quality
                Security
                  License
                    Reuse

                      If you do not have the angular that is required to run this code, you can install them by clicking on the above link.

                      You can search for any dependent library on kandi like angular.

                      FAQ: 

                      1. What is the Web Animations API, and how does it relate to Angular Animate? 

                      Web animations API is an animation of DOM elements. We can use it for arranging order and timing changes of Webpage presentations. It has two models.

                      1. Timing model

                      2. Animation model. 

                      Angular animation uses the native animation API. 


                      2. What can we use CSS web transition functionality with Angular Animate for? 

                      We can style any methods or functions, or animations using CSS. The animations of the webpages depend on the CSS web transition functionality. Angular animations are also animated based on the same. But with the added advantage of more control on the developer side. We implement the property of the Angular animations through CSS transitions. 


                      3. In what ways are animations important for Angular applications? 

                      • We can easily communicate ideas quickly. 
                      • It sets animation timings, styles, keyframes, and transitions. 
                      • We use it to animate more complex HTML elements. 
                      • At any component level, we can easily get control of animations. 
                      • We can run the code before or after the animation. 


                      4. Can an animation end event trigger a specific action within an angular application? 

                      We can use animation end events to trigger an action within an angular application. We can perform this based on the usage of keyframes or transitions. 


                      5. What animation states must we consider when creating custom animations with Angular Animate? 

                      • Void state: It determines the state of an element that is not part of the DOM. It happens when we create an element that we don't keep in DOM, or the element takes away from it. While adding or removing from the DOM, this state is very useful to animate. We can use the keyword void to define this state. 
                      • The wildcard state: This is the default state of the element. We will use the * symbol to define this state. We can style the application element regardless of its current animations. 
                      • Custom state: This is the custom state of the application element. We can use any custom name of our choice to define this state. We should define the need explicitly in our code. 


                      6. Can we define the child animations within the same framework as the parent ones? 

                      Yes, defining child animations within the same framework as parent ones is possible. Each time an animation triggers, we prioritize the Parent animation over the children. It is stopping the execution of all child animations. 

                      Support

                      1. For any support on kandi solution kits, please use the chat
                      2. For further learning resources, visit the Open Weaver Community learning page.

                      See similar Kits and Libraries