View source Improve this doc

# ``

## ngAnimate

`ngAnimate` is an optional module that provides CSS and JavaScript animation hooks.

# Installation

First include `angular-animate.js` in your HTML:

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

You can also find this file on the Google CDN, Bower (as `angular-animate`), and on code.angularjs.org.

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

        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 $animation service. The directives that support animation automatically are: `ngRepeat`, `ngInclude`, `ngSwitch`, `ngShow`, `ngHide` and `ngView`. 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:

    <style type="text/css">
    .slide.ng-enter > div,
    .slide.ng-leave > div {
      -webkit-transition:0.5s linear all;
      -moz-transition:0.5s linear all;
      -o-transition:0.5s linear all;
      transition:0.5s linear all;
    }

    .slide.ng-enter { }        /* starting animations for enter */
    .slide.ng-enter-active { } /* terminal animations for enter */
    .slide.ng-leave { }        /* starting animations for leave */
    .slide.ng-leave-active { } /* terminal animations for leave */
    </style>

    <!--
    the animate service will automatically add .ng-enter and .ng-leave to the element
    to trigger the CSS transition/animations
    -->
    <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:

    <style type="text/css">
    /*
     The animate class is apart of the element and the ng-enter class
     is attached to the element once the enter animation event is triggered
    */
    .reveal-animation.ng-enter {
     -webkit-transition: 1s linear all; /* Safari/Chrome */
     -moz-transition: 1s linear all; /* Firefox */
     -o-transition: 1s linear all; /* Opera */
     transition: 1s linear all; /* IE10+ and Future Browsers */

     /* The animation preparation code */
     opacity: 0;
    }

    /*
     Keep in mind that you want to combine both CSS
     classes together to avoid any CSS-specificity
     conflicts
    */
    .reveal-animation.ng-enter.ng-enter-active {
     /* The animation code itself */
     opacity: 1;
    }
    </style>

    <div class="view-container">
      <div ng-view class="reveal-animation"></div>
    </div>

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

    <style type="text/css">
    .reveal-animation.ng-enter {
      -webkit-animation: enter_sequence 1s linear; /* Safari/Chrome */
      -moz-animation: enter_sequence 1s linear; /* Firefox */
      -o-animation: enter_sequence 1s linear; /* Opera */
      animation: enter_sequence 1s linear; /* IE10+ and Future Browsers */
    }
    @-webkit-keyframes enter_sequence {
      from { opacity:0; }
      to { opacity:1; }
    }
    @-moz-keyframes enter_sequence {
      from { opacity:0; }
      to { opacity:1; }
    }
    @-o-keyframes enter_sequence {
      from { opacity:0; }
      to { opacity:1; }
    }
    @keyframes enter_sequence {
      from { opacity:0; }
      to { opacity:1; }
    }
    </style>

    <div class="view-container">
      <div ng-view class="reveal-animation"></div>
    </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.

## 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.

    var ngModule = angular.module('YourApp', []);
    ngModule.animation('.my-crazy-animation', function() {
      return {
        enter: function(element, done) {
          //run the animation

    return function(element, done) {
            //cancel the animation
          }
        }
        leave: function(element, done) { },
        move: function(element, done) { },
        show: function(element, done) { },
        hide: function(element, done) { },
        addClass: function(element, className, done) { },
        removeClass: function(element, className, done) { },
      }
    });

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 be executed. It should be also noted that only simple class selectors are allowed.

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).