View source Improve this doc

ngAnimate

The ngAnimate module provides support for JavaScript, CSS3 transition and CSS3 keyframe animation hooks within existing core and custom directives.

Installation

First include angular-animate.js in your HTML:

  1. <script src="angular.js">
  2. <script src="angular-animate.js">

You can download this file from the following places:

e.g. "//ajax.googleapis.com/ajax/libs/angularjs/X.Y.Z/angular-animate.js"

e.g. bower install angular-animate@X.Y.Z

e.g. "//code.angularjs.org/X.Y.Z/angular-animate.js"

where X.Y.Z is the AngularJS version you are running.

Then load the module in your application by adding it as a dependent module:

  1. angular.module('app', ['ngAnimate']);

With that you're ready to get started!

Usage

To see animations in action, all that is required is to define the appropriate CSS classes or to register a JavaScript animation via the myModule.animation() function. The directives that support animation automatically are: ngRepeat, ngInclude, ngIf, ngSwitch, ngShow, ngHide, ngView and ngClass. Custom directives can take advantage of animation by using the $animate service.

Below is a more detailed breakdown of the supported animation events provided by pre-existing ng directives:

Directive Supported Animations
ngRepeat enter, leave and move
ngView enter and leave
ngInclude enter and leave
ngSwitch enter and leave
ngIf enter and leave
ngClass add and remove
ngShow & ngHide add and remove (the ng-hide class value)

You can find out more information about animations upon visiting each directive page.

Below is an example of how to apply animations to a directive that supports animation hooks:

  1. <style type="text/css">
  2. .slide.ng-enter, .slide.ng-leave {
  3. -webkit-transition:0.5s linear all;
  4. transition:0.5s linear all;
  5. }
  6.  
  7. .slide.ng-enter { } /* starting animations for enter */
  8. .slide.ng-enter-active { } /* terminal animations for enter */
  9. .slide.ng-leave { } /* starting animations for leave */
  10. .slide.ng-leave-active { } /* terminal animations for leave */
  11. </style>
  12.  
  13. <!--
  14. the animate service will automatically add .ng-enter and .ng-leave to the element
  15. to trigger the CSS transition/animations
  16. -->
  17. <ANY class="slide" ng-include="..."></ANY>

Keep in mind that if an animation is running, any child elements cannot be animated until the parent element's animation has completed.

CSS-defined Animations

The animate service will automatically apply two CSS classes to the animated element and these two CSS classes are designed to contain the start and end CSS styling. Both CSS transitions and keyframe animations are supported and can be used to play along with this naming structure.

The following code below demonstrates how to perform animations using CSS transitions with Angular:

  1. <style type="text/css">
  2. /*
  3. The animate class is apart of the element and the ng-enter class
  4. is attached to the element once the enter animation event is triggered
  5. */
  6. .reveal-animation.ng-enter {
  7. -webkit-transition: 1s linear all; /* Safari/Chrome */
  8. transition: 1s linear all; /* All other modern browsers and IE10+ */
  9.  
  10. /* The animation preparation code */
  11. opacity: 0;
  12. }
  13.  
  14. /*
  15. Keep in mind that you want to combine both CSS
  16. classes together to avoid any CSS-specificity
  17. conflicts
  18. */
  19. .reveal-animation.ng-enter.ng-enter-active {
  20. /* The animation code itself */
  21. opacity: 1;
  22. }
  23. </style>
  24.  
  25. <div class="view-container">
  26. <div ng-view class="reveal-animation"></div>
  27. </div>

The following code below demonstrates how to perform animations using CSS animations with Angular:

  1. <style type="text/css">
  2. .reveal-animation.ng-enter {
  3. -webkit-animation: enter_sequence 1s linear; /* Safari/Chrome */
  4. animation: enter_sequence 1s linear; /* IE10+ and Future Browsers */
  5. }
  6. @-webkit-keyframes enter_sequence {
  7. from { opacity:0; }
  8. to { opacity:1; }
  9. }
  10. @keyframes enter_sequence {
  11. from { opacity:0; }
  12. to { opacity:1; }
  13. }
  14. </style>
  15.  
  16. <div class="view-container">
  17. <div ng-view class="reveal-animation"></div>
  18. </div>

Both CSS3 animations and transitions can be used together and the animate service will figure out the correct duration and delay timing.

Upon DOM mutation, the event class is added first (something like ng-enter), then the browser prepares itself to add the active class (in this case ng-enter-active) which then triggers the animation. The animation module will automatically detect the CSS code to determine when the animation ends. Once the animation is over then both CSS classes will be removed from the DOM. If a browser does not support CSS transitions or CSS animations then the animation will start and end immediately resulting in a DOM element that is at its final state. This final state is when the DOM element has no CSS transition/animation classes applied to it.

CSS Staggering Animations

A Staggering animation is a collection of animations that are issued with a slight delay in between each successive operation resulting in a curtain-like effect. The ngAnimate module, as of 1.2.0, supports staggering animations and the stagger effect can be performed by creating a ng-EVENT-stagger CSS class and attaching that class to the base CSS class used for the animation. The style property expected within the stagger class can either be a transition-delay or an animation-delay property (or both if your animation contains both transitions and keyframe animations).

  1. .my-animation.ng-enter {
  2. /* standard transition code */
  3. -webkit-transition: 1s linear all;
  4. transition: 1s linear all;
  5. opacity:0;
  6. }
  7. .my-animation.ng-enter-stagger {
  8. /* this will have a 100ms delay between each successive leave animation */
  9. -webkit-transition-delay: 0.1s;
  10. transition-delay: 0.1s;
  11.  
  12. /* in case the stagger doesn't work then these two values
  13. must be set to 0 to avoid an accidental CSS inheritance */
  14. -webkit-transition-duration: 0s;
  15. transition-duration: 0s;
  16. }
  17. .my-animation.ng-enter.ng-enter-active {
  18. /* standard transition styles */
  19. opacity:1;
  20. }

Staggering animations work by default in ngRepeat (so long as the CSS class is defined). Outside of ngRepeat, to use staggering animations on your own, they can be triggered by firing multiple calls to the same event on $animate. However, the restrictions surrounding this are that each of the elements must have the same CSS className value as well as the same parent element. A stagger operation will also be reset if more than 10ms has passed after the last animation has been fired.

The following code will issue the ng-leave-stagger event on the element provided:

  1. var kids = parent.children();
  2.  
  3. $animate.leave(kids[0]); //stagger index=0
  4. $animate.leave(kids[1]); //stagger index=1
  5. $animate.leave(kids[2]); //stagger index=2
  6. $animate.leave(kids[3]); //stagger index=3
  7. $animate.leave(kids[4]); //stagger index=4
  8.  
  9. $timeout(function() {
  10. //stagger has reset itself
  11. $animate.leave(kids[5]); //stagger index=0
  12. $animate.leave(kids[6]); //stagger index=1
  13. }, 100, false);

Stagger animations are currently only supported within CSS-defined animations.

JavaScript-defined Animations

In the event that you do not want to use CSS3 transitions or CSS3 animations or if you wish to offer animations on browsers that do not yet support CSS transitions/animations, then you can make use of JavaScript animations defined inside of your AngularJS module.

  1. var ngModule = angular.module('
    YourApp
    ', []);
  2. ngModule.animation('.my-crazy-animation', function() {
  3. return {
  4. enter: function(element, done) {
  5. //run the animation here and call done when the animation is complete
  6. return function(cancelled) {
  7. //this (optional) function will be called when the animation
  8. //completes or when the animation is cancelled (the cancelled
  9. //flag will be set to true if cancelled).
  10. }
  11. }
  12. leave: function(element, done) { },
  13. move: function(element, done) { },
  14.  
  15. //animation that can be triggered before the class is added
  16. beforeAddClass: function(element, className, done) { },
  17.  
  18. //animation that can be triggered after the class is added
  19. addClass: function(element, className, done) { },
  20.  
  21. //animation that can be triggered before the class is removed
  22. beforeRemoveClass: function(element, className, done) { },
  23.  
  24. //animation that can be triggered after the class is removed
  25. removeClass: function(element, className, done) { }
  26. }
  27. });

JavaScript-defined animations are created with a CSS-like class selector and a collection of events which are set to run a javascript callback function. When an animation is triggered, $animate will look for a matching animation which fits the element's CSS class attribute value and then run the matching animation event function (if found). In other words, if the CSS classes present on the animated element match any of the JavaScript animations then the callback function will be executed. It should be also noted that only simple, single class selectors are allowed (compound class selectors are not supported).

Within a JavaScript animation, an object containing various event callback animation functions is expected to be returned. As explained above, these callbacks are triggered based on the animation event. Therefore if an enter animation is run, and the JavaScript animation is found, then the enter callback will handle that animation (in addition to the CSS keyframe animation or transition code that is defined via a stylesheet).