Improve this Doc View Source $animate
- $animateProvider
- service in module ng
Overview
The $animate service exposes a series of DOM utility methods that provide support for animation hooks. The default behavior is the application of DOM operations, however, when an animation is detected (and animations are enabled), $animate will do the heavy lifting to ensure that animation runs with the triggered DOM operation.
By default $animate doesn't trigger any animations. This is because the ngAnimate module isn't included and only when it is active then the animation hooks that $animate triggers will be functional. Once active then all structural ng- directives will trigger animations as they perform their DOM-related operations (enter, leave and move). Other directives such as ngClass, ngShow, ngHide and ngMessages also provide support for animations.
It is recommended that the$animate service is always used when executing DOM-related procedures within directives.
To learn more about enabling animation support, click here to visit the ngAnimate module page.
Methods
-
on(event, container, callback);
Sets up an event listener to fire whenever the animation event (enter, leave, move, etc...) has fired on the given element or among any of its children. Once the listener is fired, the provided callback is fired with the following params:
$animate.on('enter', container, function callback(element, phase) { // cool we detected an enter animation within the container } );Note: Generally, the events that are fired correspond 1:1 to$animatemethod names, e.g. addClass() will fireaddClass, andngClasswill fireaddClassif classes are added, andremoveClassif classes are removed. However, there are two exceptions:- if both an addClass() and a removeClass() action are performed during the same animation, the event fired will be
setClass. This is true even forngClass. - an animate() call that adds and removes classes will fire the
setClassevent, but if it either removes or adds classes, it will fireanimateinstead.
Parameters
Param Type Details event stringthe animation event that will be captured (e.g. enter, leave, move, addClass, removeClass, etc...)
container DOMElementthe container element that will capture each of the animation events that are fired on itself as well as among its children
callback Functionthe callback function that will be fired when the listener is triggered.
The arguments present in the callback function are:
-
element- The captured DOM element that the animation was fired on. -
phase- The phase of the animation. The two possible phases are start (when the animation starts) and close (when it ends). -
data- an object with these properties:- addClass -
{string|null}- space-separated CSS classes to add to the element - removeClass -
{string|null}- space-separated CSS classes to remove from the element - from -
{Object|null}- CSS properties & values at the beginning of the animation - to -
{Object|null}- CSS properties & values at the end of the animation
- addClass -
Note that the callback does not trigger a scope digest. Wrap your call into a scope.$apply to propagate changes to the scope.
- if both an addClass() and a removeClass() action are performed during the same animation, the event fired will be
-
off(event, [container], [callback]);
Deregisters an event listener based on the event which has been associated with the provided element. This method can be used in three different ways depending on the arguments:
// remove all the animation event listeners listening for `enter` $animate.off('enter'); // remove listeners for all animation events from the container element $animate.off(container); // remove all the animation event listeners listening for `enter` on the given element and its children $animate.off('enter', container); // remove the event listener function provided by `callback` that is set // to listen for `enter` on the given `container` as well as its children $animate.off('enter', container, callback);Parameters
Param Type Details event | container stringDOMElementthe animation event (e.g. enter, leave, move, addClass, removeClass, etc...), or the container element. If it is the element, all other arguments are ignored.
container (optional)DOMElementthe container element the event listener was placed on
callback (optional)Function=the callback function that was registered as the listener
-
pin(element, parentElement);
Associates the provided element with a host parent element to allow the element to be animated even if it exists outside of the DOM structure of the AngularJS application. By doing so, any animation triggered via
$animatecan be issued on the element despite being outside the realm of the application or within another application. Say for example if the application was bootstrapped on an element that is somewhere inside of the<body>tag, but we wanted to allow for an element to be situated as a direct child ofdocument.body, then this can be achieved by pinning the element via$animate.pin(element). Keep in mind that calling$animate.pin(element, parentElement)will not actually insert into the DOM anywhere; it will just create the association.Note that this feature is only active when the
ngAnimatemodule is used.Parameters
Param Type Details element DOMElementthe external element that will be pinned
parentElement DOMElementthe host parent element that will be associated with the external element
-
enabled([element], [enabled]);
Used to get and set whether animations are enabled or not on the entire application or on an element and its children. This function can be called in four ways:
// returns true or false $animate.enabled(); // changes the enabled state for all animations $animate.enabled(false); $animate.enabled(true); // returns true or false if animations are enabled for an element $animate.enabled(element); // changes the enabled state for an element and its children $animate.enabled(element, true); $animate.enabled(element, false);
Parameters
Param Type Details element (optional)DOMElementthe element that will be considered for checking/setting the enabled state
enabled (optional)booleanwhether or not the animations will be enabled for the element
Returns
booleanwhether or not animations are enabled
-
cancel(animationRunner);
Cancels the provided animation and applies the end state of the animation. Note that this does not cancel the underlying operation, e.g. the setting of classes or adding the element to the DOM.
Parameters
Param Type Details animationRunner animationRunnerAn animation runner returned by an $animate function.
Example
-
enter(element, parent, [after], [options]);
Inserts the element into the DOM either after the
afterelement (if provided) or as the first child within theparentelement and then triggers an animation. A promise is returned that will be resolved during the next digest once the animation has completed.Parameters
Param Type Details element DOMElementthe element which will be inserted into the DOM
parent DOMElementthe parent element which will append the element as a child (so long as the after element is not present)
after (optional)DOMElementthe sibling element after which the element will be appended
options (optional)objectan optional collection of options/styles that will be applied to the element. The object can have the following properties:
-
addClass -
{string}- space-separated CSS classes to add to element -
from -
{Object}- CSS properties & values at the beginning of animation. Must have matchingto -
removeClass -
{string}- space-separated CSS classes to remove from element -
to -
{Object}- CSS properties & values at end of animation. Must have matchingfrom
Returns
Runnerthe animation runner
-
addClass -
-
move(element, parent, [after], [options]);
Inserts (moves) the element into its new position in the DOM either after the
afterelement (if provided) or as the first child within theparentelement and then triggers an animation. A promise is returned that will be resolved during the next digest once the animation has completed.Parameters
Param Type Details element DOMElementthe element which will be moved into the new DOM position
parent DOMElementthe parent element which will append the element as a child (so long as the after element is not present)
after (optional)DOMElementthe sibling element after which the element will be appended
options (optional)objectan optional collection of options/styles that will be applied to the element. The object can have the following properties:
-
addClass -
{string}- space-separated CSS classes to add to element -
from -
{Object}- CSS properties & values at the beginning of animation. Must have matchingto -
removeClass -
{string}- space-separated CSS classes to remove from element -
to -
{Object}- CSS properties & values at end of animation. Must have matchingfrom
Returns
Runnerthe animation runner
-
addClass -
-
leave(element, [options]);
Triggers an animation and then removes the element from the DOM. When the function is called a promise is returned that will be resolved during the next digest once the animation has completed.
Parameters
Param Type Details element DOMElementthe element which will be removed from the DOM
options (optional)objectan optional collection of options/styles that will be applied to the element. The object can have the following properties:
-
addClass -
{string}- space-separated CSS classes to add to element -
from -
{Object}- CSS properties & values at the beginning of animation. Must have matchingto -
removeClass -
{string}- space-separated CSS classes to remove from element -
to -
{Object}- CSS properties & values at end of animation. Must have matchingfrom
Returns
Runnerthe animation runner
-
addClass -
-
addClass(element, className, [options]);
Triggers an addClass animation surrounding the addition of the provided CSS class(es). Upon execution, the addClass operation will only be handled after the next digest and it will not trigger an animation if element already contains the CSS class or if the class is removed at a later step. Note that class-based animations are treated differently compared to structural animations (like enter, move and leave) since the CSS classes may be added/removed at different points depending if CSS or JavaScript animations are used.
Parameters
Param Type Details element DOMElementthe element which the CSS classes will be applied to
className stringthe CSS class(es) that will be added (multiple classes are separated via spaces)
options (optional)objectan optional collection of options/styles that will be applied to the element. The object can have the following properties:
-
removeClass -
{string}- space-separated CSS classes to remove from element -
from -
{Object}- CSS properties & values at the beginning of animation. Must have matchingto -
to -
{Object}- CSS properties & values at end of animation. Must have matchingfrom
Returns
RunneranimationRunner the animation runner
-
removeClass -
-
removeClass(element, className, [options]);
Triggers a removeClass animation surrounding the removal of the provided CSS class(es). Upon execution, the removeClass operation will only be handled after the next digest and it will not trigger an animation if element does not contain the CSS class or if the class is added at a later step. Note that class-based animations are treated differently compared to structural animations (like enter, move and leave) since the CSS classes may be added/removed at different points depending if CSS or JavaScript animations are used.
Parameters
Param Type Details element DOMElementthe element which the CSS classes will be applied to
className stringthe CSS class(es) that will be removed (multiple classes are separated via spaces)
options (optional)objectan optional collection of options/styles that will be applied to the element. The object can have the following properties:
-
addClass -
{string}- space-separated CSS classes to add to element -
from -
{Object}- CSS properties & values at the beginning of animation. Must have matchingto -
to -
{Object}- CSS properties & values at end of animation. Must have matchingfrom
Returns
Runnerthe animation runner
-
addClass -
-
setClass(element, add, remove, [options]);
Performs both the addition and removal of a CSS classes on an element and (during the process) triggers an animation surrounding the class addition/removal. Much like
$animate.addClassand$animate.removeClass,setClasswill only evaluate the classes being added/removed once a digest has passed. Note that class-based animations are treated differently compared to structural animations (like enter, move and leave) since the CSS classes may be added/removed at different points depending if CSS or JavaScript animations are used.Parameters
Param Type Details element DOMElementthe element which the CSS classes will be applied to
add stringthe CSS class(es) that will be added (multiple classes are separated via spaces)
remove stringthe CSS class(es) that will be removed (multiple classes are separated via spaces)
options (optional)objectan optional collection of options/styles that will be applied to the element. The object can have the following properties:
-
addClass -
{string}- space-separated CSS classes to add to element -
removeClass -
{string}- space-separated CSS classes to remove from element -
from -
{Object}- CSS properties & values at the beginning of animation. Must have matchingto -
to -
{Object}- CSS properties & values at end of animation. Must have matchingfrom
Returns
Runnerthe animation runner
-
addClass -
-
animate(element, from, to, [className], [options]);
Performs an inline animation on the element which applies the provided to and from CSS styles to the element. If any detected CSS transition, keyframe or JavaScript matches the provided className value, then the animation will take on the provided styles. For example, if a transition animation is set for the given className, then the provided
fromandtostyles will be applied alongside the given transition. If the CSS style provided infromdoes not have a corresponding style into, the style infromis applied immediately, and no animation is run. If a JavaScript animation is detected then the provided styles will be given in as function parameters into theanimatemethod (or as part of theoptionsparameter):ngModule.animation('.my-inline-animation', function() { return { animate : function(element, from, to, done, options) { //animation done(); } } });Parameters
Param Type Details element DOMElementthe element which the CSS styles will be applied to
from objectthe from (starting) CSS styles that will be applied to the element and across the animation.
to objectthe to (destination) CSS styles that will be applied to the element and across the animation.
className (optional)stringan optional CSS class that will be applied to the element for the duration of the animation. If this value is left as empty then a CSS class of
ng-inline-animatewill be applied to the element. (Note that if no animation is detected then this value will not be applied to the element.)options (optional)objectan optional collection of options/styles that will be applied to the element. The object can have the following properties:
-
addClass -
{string}- space-separated CSS classes to add to element -
from -
{Object}- CSS properties & values at the beginning of animation. Must have matchingto -
removeClass -
{string}- space-separated CSS classes to remove from element -
to -
{Object}- CSS properties & values at end of animation. Must have matchingfrom
Returns
Runnerthe animation runner
-
addClass -
© 2010–2018 Google, Inc.
Licensed under the Creative Commons Attribution License 4.0.
https://code.angularjs.org/1.7.8/docs/api/ng/service/$animate