What is angular-animate?
The angular-animate package is an AngularJS module that provides support for JavaScript, CSS3 transition, and CSS3 keyframe animation hooks within your AngularJS applications. It allows you to create smooth, complex animations for various elements and states in your application.
What are angular-animate's main functionalities?
CSS-based animations
This feature allows you to define CSS classes for entering and leaving animations. The .ng-enter and .ng-leave classes are used to define the starting state of the animation, while the .ng-enter-active and .ng-leave-active classes define the end state.
/* CSS code */
.fade.ng-enter {
transition: 0.5s linear all;
opacity: 0;
}
.fade.ng-enter-active {
opacity: 1;
}
.fade.ng-leave {
transition: 0.5s linear all;
opacity: 1;
}
.fade.ng-leave-active {
opacity: 0;
}
JavaScript-based animations
This feature allows you to define animations using JavaScript. You can create custom animations for entering and leaving elements by manipulating their CSS properties and using jQuery's animate function.
// JavaScript code
angular.module('myApp', ['ngAnimate'])
.animation('.slide', function() {
return {
enter: function(element, done) {
element.css({
position: 'relative',
left: '-10px',
opacity: 0
});
element.animate({
left: '0px',
opacity: 1
}, 1000, done);
},
leave: function(element, done) {
element.css({
position: 'relative',
left: '0px',
opacity: 1
});
element.animate({
left: '-10px',
opacity: 0
}, 1000, done);
}
};
});
ngAnimate directive
The ngAnimate directive can be used to apply animations to elements within an AngularJS application. In this example, the .fade class is applied to each item in the ng-repeat directive, and the corresponding CSS animations are triggered when items are added or removed.
<div ng-app="myApp" ng-controller="myCtrl">
<div ng-repeat="item in items" class="fade">
{{item}}
</div>
</div>
<script>
angular.module('myApp', ['ngAnimate'])
.controller('myCtrl', function($scope) {
$scope.items = ['Item 1', 'Item 2', 'Item 3'];
});
</script>
Other packages similar to angular-animate
ng2-animate
ng2-animate is an Angular (2+) module that provides a set of reusable animations for Angular applications. It offers a variety of pre-built animations that can be easily applied to elements, making it simpler to add animations without writing custom CSS or JavaScript. Compared to angular-animate, ng2-animate is designed for newer versions of Angular and provides a more modern approach to animations.
react-transition-group
react-transition-group is a popular library for managing animations in React applications. It provides components like Transition, CSSTransition, and TransitionGroup to handle the entering and exiting of elements with animations. While it serves a similar purpose to angular-animate, it is specifically designed for React and offers a different API and set of features tailored to React's component-based architecture.
animejs
animejs is a lightweight JavaScript animation library that works with any JavaScript framework, including AngularJS. It provides a powerful and flexible API for creating complex animations with minimal code. Unlike angular-animate, which is tightly integrated with AngularJS, animejs is a general-purpose animation library that can be used across different frameworks and projects.
packaged angular-animate
This repo is for distribution on npm
and bower
. The source for this module is in the
main AngularJS repo.
Please file issues and pull requests against that repo.
Install
You can install this package either with npm
or with bower
.
npm
npm install angular-animate
Then add ngAnimate
as a dependency for your app:
angular.module('myApp', [require('angular-animate')]);
bower
bower install angular-animate
Then add a <script>
to your index.html
:
<script src="/bower_components/angular-animate/angular-animate.js"></script>
Then add ngAnimate
as a dependency for your app:
angular.module('myApp', ['ngAnimate']);
Documentation
Documentation is available on the
AngularJS docs site.
License
The MIT License
Copyright (c) 2010-2015 Google, Inc. http://angularjs.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
1.4.0-rc.2 rocket-zambonimation (2015-05-12)
Bug Fixes
- $compile: ensure directive names have no leading or trailing whitespace
(bab474aa,
#11397, #11772)
- $httpParamSerializerJQLike: follow jQuery logic for nested params
(2420a0a7,
#11551, #11635)
- jqLite: check for "length" in obj in isArrayLike to prevent iOS8 JIT bug from surfacing
(426a5ac0,
#11508)
- ngAnimate:
- ensure that multiple requests to requestAnimationFrame are buffered
(db20b830,
#11791)
- ensure that an object is always returned even when no animation is set to run
(d5683d21)
- force use of
ng-anchor
instead of a suffixed -anchor
CSS class when triggering anchor animations
(df24410c) - rename
ng-animate-anchor
to ng-anchor
(e6d053de) - ensure that shared CSS classes between anchor nodes are retained
(e0014002,
#11681)
- prohibit usage of the
ng-animate
class with classNameFilter
(1002b80a,
#11431, #11807) - ensure that the temporary CSS classes are applied before detection
(f7e9ff1a,
#11769, #11804)
- ensure that all jqLite elements are deconstructed properly
(64d05180,
#11658)
- ensure animations are not attempted on text nodes
(2aacc2d6,
#11703)
- ensure JS animations recognize $animateCss directly
(0681a540)
- ngClass: add/remove classes which are properties of Object.prototype
(f7b99970,
#11813, #11814)
- ngOptions:
- ngTouch:
- check undefined tagName for SVG event target
(74eb17d7)
- don't prevent click event after a touchmove
(95521876,
#10985)
Features
- $resource: include request context in error message
(266bc652,
#11363)
Breaking Changes
- ngAnimate - $animateCss: due to d5683d21,
The $animateCss service will now always return an
object even if the animation is not set to run. If your code is using
$animateCss then please consider the following code change:
// before
var animator = $animateCss(element, { ... });
if (!animator) {
continueApp();
return;
}
var runner = animator.start();
runner.done(continueApp);
runner.then(continueApp);
// now
var animator = $animateCss(element, { ... });
var runner = animator.start();
runner.done(continueApp);
runner.then(continueApp);
- due to df24410c,
Prior to this fix there were to ways to apply CSS
animation code to an anchor animation. With this fix, the suffixed
CSS -anchor classes are now not used anymore for CSS anchor animations.
Instead just use the ng-anchor
CSS class like so:
<div class="container-animation" ng-if="on">
<div ng-animate-ref="1" class="my-anchor-element"></div>
</div>
<div class="container-animation" ng-if="!on">
<div ng-animate-ref="1" class="my-anchor-element"></div>
</div>
before:
/* before (notice the container-animation CSS class) */
.container-animation-anchor {
transition:0.5s linear all;
}
now:
/* now (just use `ng-anchor` on a class that both the
elements that contain `ng-animate-ref` share) */
.my-anchor-element.ng-anchor {
transition:0.5s linear all;
}
-
due to e6d053de,
if your CSS code made use of the ng-animate-anchor
CSS class for referencing the anchored animation element then your
code must now use ng-anchor
instead.
-
due to 1002b80a,
partially or fully using a regex value containing
ng-animate
as a token is not allowed anymore. Doing so will trigger a
minErr exception to be thrown.
So don't do this:
// only animate elements that contain the `ng-animate` CSS class
$animateProvider.classNameFilter(/ng-animate/);
// or partially contain it
$animateProvider.classNameFilter(/some-class ng-animate another-class/);
but this is OK:
$animateProvider.classNameFilter(/ng-animate-special/);
Although it is unlikely that anyone is using it in this way, this change does change the
behavior of ngOptions
in the following case:
- you are iterating over an array-like object, using the array form of the
ngOptions
syntax
(item.label for item in items
) and that object contains non-numeric property keys.
In this case these properties with non-numeric keys will be ignored.
** Here array-like is defined by the result of a call to this internal function:
https://github.com/angular/angular.js/blob/v1.4.0-rc.1/src/Angular.js#L198-L211 **
To get the desired behavior you need to iterate using the object form of the ngOptions
syntax
(value.label
for (key, value) in items)`).
<a name="v1.4.0-rc.1"></a>