Animations in AngularJS Applications

AngularJS provides animation for common directives like ngRepeat , ngSwitch, ngInclude,ngClass and ngView. AngularJS also provides animation for custom directives using the $animate service. It provides animated transitions, with help from CSS. Animations in AngularJS are completely based on CSS classes. As long as we have CSS classes attached to an HTML element within our website, we can apply animations to it.

Example:

  1. <html>  
  2.   
  3. <head>  
  4.     <title>Angular JS Example</title>  
  5.     <style>  
  6.         div {  
  7.             transition: width2s, height4s;  
  8.             background-color: red;  
  9.             height: 150px;  
  10.             width: 150%;  
  11.             position: relative;  
  12.             top: 10px;  
  13.             left: 10px;  
  14.         }  
  15.           
  16.         .ng-hide {  
  17.             height: 0;  
  18.             width: 0;  
  19.             background-color: blue;  
  20.             top: -100px;  
  21.             left: 150px;  
  22.         }  
  23.     </style>  
  24.     <scriptsrc="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js">  
  25.         </script>  
  26.         <scriptsrc="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular-animate.js">  
  27.             </script>  
  28. </head>  
  29.   
  30. <bodyng-app="ngAnimate">  
  31.     <h2>AngularJS Animation</h2>  
  32.     <h1>Hide this DIV: <inputtype="checkbox"ng-model="myCheck"></h1>  
  33.     <divng-hide="myCheck">  
  34.         <pre><h2>During the Animation color of Div  
  35. Change from red to blue  
  36. </h2></pre>  
  37.     </div>  
  38.  </body>  
  39.   
  40. </html>  
Output:

effect

After animation starts the following effect will occur.

animation

Above is a quick example of ng-hide directive that enables the animation.

How implement Animation in Application

Firstly, you need to include the AngularJS Animation in your application. You can refer to the following link:
  1. <scriptsrc="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular-animate.js"></script>  
Then you either refer the “ngAnimate” module in your application.
  1. <body ng-app="ngAnimate">  
Or your application contain a name, you must include the ngAnimate module as a dependency within your application.
  1. <body ng-app="myApp">  
  2.    <script>  
  3.       var app = angular.module('myApp', ['ngAnimate']);  
  4. </script>  
How ngAnimate Work

We know that animation in AngularJS is completely based on CSS classes. If we have a CSS class attached to an HTML element within our website, we can apply animations to it. The ngAnimate module is used for adds and removes classes for HTML elements. The ngAnimate module does not animate our HTML elements, but when ngAnimate notice certain events, like hide or show of an HTML element, it add or remove the CSS classes.

The following directives are used for adding or removing the classes: ng-show, ng-hide, ng-view, ng-class, ng-if, ng-include, ng-switch, ng-repeat. There are some other directives that are used for animation like ng-leave, ng-enter, ng-move.

Example:

  1. <html>  
  2.   
  3. <head>  
  4.     <title>Angular JS Example</title>  
  5.     <style>  
  6.         .add-class-add,  
  7.         .css-class-remove {  
  8.             -webkit-transition: allcubic-bezier(0.150, 0.260, 0.410, 0.760)0.2s;  
  9.             -moz-transition: allcubic-bezier(0.250, 0.460, 0.450, 0.940)0.3s;  
  10.             -o-transition: allcubic-bezier(0.150, 0.360, 0.450, 0.940)0.2s;  
  11.             transition: allcubic-bezier(0.250, 0.460, 0.550, 0.940)0.5s;  
  12.         }  
  13.           
  14.         .add-class,  
  15.         .add-class-add.css-class-add-active {  
  16.             color: red;  
  17.             font-size: 3em;  
  18.         }  
  19.           
  20.         .add-class:hover {  
  21.             -webkit-transform: rotate(360deg);  
  22.             background: #9351A6;  
  23.             border-radius: 50%;  
  24.             -webkit-transition: all0.5sease-in;  
  25.             -moz-transition: all0.5sease-in-out;  
  26.             -o-transition: all0.5sease-out;  
  27.             transition: all0.5sease-in-out;  
  28.         }  
  29.           
  30.         .css-class-remove.css-class-remove-active {  
  31.             font-size: 1.0em;  
  32.             color: black;  
  33.         }  
  34.           
  35.         .rotate {}  
  36.     </style>  
  37.     <scriptsrc="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js">  
  38.         </script>  
  39.         <scriptsrc="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular-animate.js">  
  40.             </script>  
  41. </head>  
  42.   
  43. <bodyng-app="ngAnimate">  
  44.     <h2>AngularJS Animation</h2>  
  45.   
  46.     <inputtype="button" style="height:60px;width:160px;font-size:30px" value="Apply" ng-click="myclass='add-class'">  
  47.         <inputtype="button" style="height:60px;width:160px;font-size:30px" value="Reset" ng-click="myclass=''">  
  48.             <br>  
  49.             <divng-class="myclass" style="background-color:blue; height:200px; width:400px">Div Will Rotate</div>  
  50.         </body>  
  51.   
  52. </html>  
Output:

Program output

After that click on “Apply” button.

run code

On mouse over event:

Apply

event

In above example we are using “ng-class” directive and on the activation of this directive we are adding “add-class.”  This class contains events like hover and remove.

Types of Animation

AngularJS supports two types of animation: CSS based animation and JavaScript based animation. For both CSS and JS based animations it must be required that both have a matching CSS class that exists both in the registered animation and within the HTML element that the animation will be triggered on.

CSS based Animation

CSS based animation doesn’t require any javaScriptcode, using the CSS class we can create animation that will be picked up by Angular when the underlying directive performs an operation. Class based animations are triggered via ngClass, ngShow, ngHide and other directives.

Example:
  1. <html>  
  2.   
  3. <head>  
  4.     <title>Angular JS Example</title>  
  5.     <style>  
  6.         .animate.ng-enter {  
  7.             transition: 0.5slinearall;  
  8.             opacity: 0;  
  9.         }  
  10.         /*The finishing CSS styles for the enter animation*/  
  11.           
  12.         .animate.ng-enter.ng-enter-active {  
  13.             opacity: 1;  
  14.         }  
  15.           
  16.         .animate:hover {  
  17.             width: 300px;  
  18.             height: 300px;  
  19.             -webkit-transform: rotate(180deg);  
  20.             /*Safari*/  
  21.             transform: rotate(90deg);  
  22.             transition-property: width;  
  23.             transition-duration: 5s;  
  24.             transition-delay: 2s;  
  25.         }  
  26.     </style>  
  27.     <scriptsrc="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js">  
  28.         </script>  
  29.         <scriptsrc="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular-animate.js">  
  30.             </script>  
  31. </head>  
  32.   
  33. <bodyng-app="ngAnimate">  
  34.     <divstyle="height:200px ;width:400px;font-size:24px; background-color:darkorange" ng-if="bool" class="animate">  
  35.         This is a example of css based animation  
  36.         </div>  
  37.         <buttonng-click="bool=true">Fade Me In !</button>  
  38.             <buttonng-click="bool=false">Fade me Out !</button>  
  39.         </body>  
  40.   
  41. </html>  
Output:

On page load style of page:

On page load style

After clicking on “Fade Me In!” button style of page.

Fade Me In

On mouse over:

On mouse over

JavaScript Based Animation

Animation in AngularJS can also be applied using JavaScript. This approach is similar to CSS based animation, we are required to register the animation on the module using the module.animate function.

Example:
  1. <html>  
  2.   
  3. <head>  
  4.     <title>Angular JS Example</title>  
  5.   
  6.     <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js">  
  7.         </script>  
  8.         <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular-animate.js">  
  9.             </script>  
  10.             <script>  
  11.                 var myApp = angular.module('mainModule', ['ngAnimate']);  
  12.                 myApp.animation('.repeat-item', function() {  
  13.                     return {  
  14.                         enter: function(element, done) {  
  15.                             runTheAnimation(element, done);  
  16.                             return function(cancelled) {  
  17.                                 if (cancelled) {  
  18.                                     stopTheAnimation();  
  19.                                 } else {  
  20.                                     completeTheAnimation();  
  21.                                 }  
  22.                             }  
  23.                         },  
  24.   
  25.                         addClass: function(element, className, done) {  
  26.                             if (className == 'hide') {  
  27.                                 runTheHideAnimation(element, done);  
  28.                             } else {  
  29.                                 runTheAnimation(element, done);  
  30.                             }  
  31.   
  32.                             return functiononEnd(element, done) {};  
  33.                         },  
  34.   
  35.                         removeClass: function(element, className, done) {  
  36.                             if (className == 'hide') {  
  37.                                 runTheShowAnimation(element, done);  
  38.                             } else {  
  39.                                 runTheAnimation(element, done);  
  40.                             }  
  41.   
  42.                             return functiononEnd(element, done) {};  
  43.                         },  
  44.   
  45.   
  46.                     };  
  47.                 });  
  48.             </script>  
  49.             <style>  
  50.                 .repeat-item.ng-enter,  
  51.                 .repeat-item.ng-leave {  
  52.                     -webkit-transition: 0.5slinearall;  
  53.                     transition: 0.5slinearall;  
  54.                     background-color: red  
  55.                 }  
  56.                   
  57.                 .repeat-item.ng-enter,  
  58.                 .repeat-item.ng-leave.ng-leave-active {  
  59.                     opacity: 0.3;  
  60.                     background-color: blue;  
  61.                 }  
  62.                   
  63.                 .repeat-item.ng-leave,  
  64.                 .repeat-item.ng-enter.ng-enter-active {  
  65.                     opacity: 1;  
  66.                 }  
  67.                   
  68.                 .ng-hide {  
  69.                     display: none!important;  
  70.                 }  
  71.                   
  72.                 .my-elm {  
  73.                     -webkit-transition: 0.6slinearall;  
  74.                     transition: 0.4slinearall;  
  75.                     opacity: 1;  
  76.                 }  
  77.                   
  78.                 .ng-hide {  
  79.                     background-color: orange;  
  80.                 }  
  81.                   
  82.                 .my-elm.ng-hide {  
  83.                     opacity: 0.2;  
  84.                 }  
  85.             </style>  
  86. </head>  
  87.   
  88. <body ng-app="mainModule">  
  89.     <div style="margin-left:100px;margin-top:20px; font-size:24px">  
  90.         <h1>Animation Using Javascript</h1>  
  91.         <div ng-init="items=['pankaj','sandeep','rahul','neeraj','sanjeev','divyanshu']">  
  92.             <input placeholder="serach name" ng-model="f" />  
  93.             <div ng-repeat="obj in items | filter:f" class="repeat-item">  
  94.                 {{obj}}  
  95.             </div>  
  96.         </div>  
  97.     </div>  
  98. </body>  
  99.   
  100. </html>  
Output:

At starting of page load.

Output

On key press the following effect will occur,

occur

sir "Pankaj Kumar Choudhary" ka title kya rakhu jo hai wahi

In the above example we are applying animation for “ng-repeat” directive. Animations in AngularJS can also be applied by the combination of CSS and JavaScript or using the $animate for custom directive.
 
Read more articles on AngularJS:

 

X

Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now