View source Improve this doc

# `$animate`
service in module `ngAnimate`

## Description

The `$animate` service provides animation detection support while performing DOM operations (enter, leave and move) as well as during addClass and removeClass operations. When any of these operations are run, the $animate service will examine any JavaScript-defined animations (which are defined by using the $animateProvider provider object) as well as any CSS-defined animations against the CSS classes present on the element once the DOM operation is run.

The `$animate` service is used behind the scenes with pre-existing directives and animation with these directives will work out of the box without any extra configuration.

Requires the `ngAnimate` module to be installed.

Please visit the `ngAnimate` module overview page learn more about how to use animations in your application.

## Dependencies

- `$timeout,`

  $sniffer, $rootElement

## Methods

- ### addClass(element, className, done)

  Triggers a custom animation event based off the className variable and then attaches the className value to the element as a CSS class. Unlike the other animation methods, the animate service will suffix the className value with -add in order to provide the animate service the setup and active CSS classes in order to trigger the animation (this will be skipped if no CSS transitions or keyframes are defined on the -add CSS class).

  Below is a breakdown of each step that occurs during addClass animation:

  Animation Step | What the element class attribute looks like
  --- | ---
  1. $animate.addClass(element, 'super') is called | class=""
  2. $animate runs any JavaScript-defined animations on the element | class=""
  3. the .super-add class is added to the element | class="super-add"
  4. $animate scans the element styles to get the CSS transition/animation duration and delay | class="super-add"
  5. the .super-add-active class is added (this triggers the CSS transition/animation) | class="super-add super-add-active"
  6. $animate waits for X milliseconds for the animation to complete | class="super-add super-add-active"
  7. The animation ends and both CSS classes are removed from the element | class=""
  8. The super class is added to the element | class="super"
  9. The done() callback is fired (if provided) | class="super"

  ##### Parameters

  Param | Type | Details
  --- | --- | ---
  element | jQuery/jqLite element | the element that will be animated
  className | string | the CSS class that will be animated and then attached to the element
  done (optional) | function() | callback function that will be called once the animation is complete

- ### enabled(value)

  Globally enables/disables animations.

  ##### Parameters

  Param | Type | Details
  --- | --- | ---
  value (optional) | boolean | If provided then set the animation on or off.

  ##### Returns

  boolean | Current animation state.

- ### enter(element, parent, after, done)

  Appends the element to the parent element that resides in the document and then runs the enter animation. Once the animation is started, the following CSS classes will be present on the element for the duration of the animation:

  Below is a breakdown of each step that occurs during enter animation:

  Animation Step | What the element class attribute looks like
  --- | ---
  1. $animate.enter(...) is called | class="my-animation"
  2. element is inserted into the parent element or beside the after element | class="my-animation"
  3. $animate runs any JavaScript-defined animations on the element | class="my-animation"
  4. the .ng-enter class is added to the element | class="my-animation ng-enter"
  5. $animate scans the element styles to get the CSS transition/animation duration and delay | class="my-animation ng-enter"
  6. the .ng-enter-active class is added (this triggers the CSS transition/animation) | class="my-animation ng-enter ng-enter-active"
  7. $animate waits for X milliseconds for the animation to complete | class="my-animation ng-enter ng-enter-active"
  8. The animation ends and both CSS classes are removed from the element | class="my-animation"
  9. The done() callback is fired (if provided) | class="my-animation"

  ##### Parameters

  Param | Type | Details
  --- | --- | ---
  element | jQuery/jqLite element | the element that will be the focus of the enter animation
  parent | jQuery/jqLite element | the parent element of the element that will be the focus of the enter animation
  after | jQuery/jqLite element | the sibling element (which is the previous element) of the element that will be the focus of the enter animation
  done (optional) | function() | callback function that will be called once the animation is complete

- ### leave(element, done)

  Runs the leave animation operation and, upon completion, removes the element from the DOM. Once the animation is started, the following CSS classes will be added for the duration of the animation:

  Below is a breakdown of each step that occurs during enter animation:

  Animation Step | What the element class attribute looks like
  --- | ---
  1. $animate.leave(...) is called | class="my-animation"
  2. $animate runs any JavaScript-defined animations on the element | class="my-animation"
  3. the .ng-leave class is added to the element | class="my-animation ng-leave"
  4. $animate scans the element styles to get the CSS transition/animation duration and delay | class="my-animation ng-leave"
  5. the .ng-leave-active class is added (this triggers the CSS transition/animation) | class="my-animation ng-leave ng-leave-active
  6. $animate waits for X milliseconds for the animation to complete | class="my-animation ng-leave ng-leave-active
  7. The animation ends and both CSS classes are removed from the element | class="my-animation"
  8. The element is removed from the DOM | ...
  9. The done() callback is fired (if provided) | ...

  ##### Parameters

  Param | Type | Details
  --- | --- | ---
  element | jQuery/jqLite element | the element that will be the focus of the leave animation
  done (optional) | function() | callback function that will be called once the animation is complete

- ### move(element, parent, after, done)

  Fires the move DOM operation. Just before the animation starts, the animate service will either append it into the parent container or add the element directly after the after element if present. Then the move animation will be run. Once the animation is started, the following CSS classes will be added for the duration of the animation:

  Below is a breakdown of each step that occurs during move animation:

  Animation Step | What the element class attribute looks like
  --- | ---
  1. $animate.move(...) is called | class="my-animation"
  2. element is moved into the parent element or beside the after element | class="my-animation"
  3. $animate runs any JavaScript-defined animations on the element | class="my-animation"
  4. the .ng-move class is added to the element | class="my-animation ng-move"
  5. $animate scans the element styles to get the CSS transition/animation duration and delay | class="my-animation ng-move"
  6. the .ng-move-active class is added (this triggers the CSS transition/animation) | class="my-animation ng-move ng-move-active"
  7. $animate waits for X milliseconds for the animation to complete | class="my-animation ng-move ng-move-active"
  8. The animation ends and both CSS classes are removed from the element | class="my-animation"
  9. The done() callback is fired (if provided) | class="my-animation"

  ##### Parameters

  Param | Type | Details
  --- | --- | ---
  element | jQuery/jqLite element | the element that will be the focus of the move animation
  parent | jQuery/jqLite element | the parent element of the element that will be the focus of the move animation
  after | jQuery/jqLite element | the sibling element (which is the previous element) of the element that will be the focus of the move animation
  done (optional) | function() | callback function that will be called once the animation is complete

- ### removeClass(element, className, done)

  Triggers a custom animation event based off the className variable and then removes the CSS class provided by the className value from the element. Unlike the other animation methods, the animate service will suffix the className value with -remove in order to provide the animate service the setup and active CSS classes in order to trigger the animation (this will be skipped if no CSS transitions or keyframes are defined on the -remove CSS class).

  Below is a breakdown of each step that occurs during removeClass animation:

  Animation Step | What the element class attribute looks like
  --- | ---
  1. $animate.removeClass(element, 'super') is called | class="super"
  2. $animate runs any JavaScript-defined animations on the element | class="super"
  3. the .super-remove class is added to the element | class="super super-remove"
  4. $animate scans the element styles to get the CSS transition/animation duration and delay | class="super super-remove"
  5. the .super-remove-active class is added (this triggers the CSS transition/animation) | class="super super-remove super-remove-active"
  6. $animate waits for X milliseconds for the animation to complete | class="super super-remove super-remove-active"
  7. The animation ends and both CSS all three classes are removed from the element | class=""
  8. The done() callback is fired (if provided) | class=""

  ##### Parameters

  Param | Type | Details
  --- | --- | ---
  element | jQuery/jqLite element | the element that will be animated
  className | string | the CSS class that will be animated and then removed from the element
  done (optional) | function() | callback function that will be called once the animation is complete