AngularJS Animations
AngularJS Animations
AngularJS provides animation effects that can be used with CSS.
To use animations in AngularJS, you need to include the angular-animate.min.js library.
<script src="http://cdn.static.runoob.com/libs/angular.js/1.4.6/angular-animate.min.js"></script>
You also need to use the ngAnimate model in the application:
<body ng-app="ngAnimate">
What is animation?
Animation is a dynamic change effect generated by changing HTML elements.
Example
Hide DIV by checking a checkbox:
<
body
ng-app=
"ngAnimate"Hide DIV:
<
input
type=
"checkbox"
ng-model=
"myCheck"
<
div
ng-hide=
"myCheck"
<
/div
<
/bodyTry it »
![]() |
Animations in the application should not be excessive, but appropriate use of animations can increase the richness of the page and make it easier for users to understand. |
|---|
If our application has already set an application name, you can add ngAnimate directly to the model:
Instance
<
body
ng-app=
"myApp"
<
h1Hide DIV:
<
input
type=
"checkbox"
ng-model=
"myCheck"
<
/h1
<
div
ng-hide=
"myCheck"
<
/div
<
script
varapp = angular.module(
'myApp'
, [
'ngAnimate'
]);
<
/script
Try it »
What does ngAnimate do?
The ngAnimate model can add or remove classes.
The ngAnimate model cannot make HTML elements animate, but ngAnimate monitors events, similar to hiding or showing HTML elements; if an event occurs, ngAnimate will use predefined classes to set the animation of the HTML element.
AngularJS directive for adding/removing classes:
ng-showng-hideng-classng-viewng-includeng-repeatng-ifng-switch
ng-showandng-hideDirectives are used to add or removeng-hidevalues to a class.
Other directives will addng-enterclasses when entering the DOM and addng-leaveattributes when removing from the DOM.
When the position of an HTML element changes,ng-repeatthe directive can also addng-moveclasses.
Additionally, after the animation is complete, the set of classes on the HTML element will be removed. For example:ng-hideThe instruction will add the following classes:
ng-animateng-hide-animateng-hide-add(if the element will be hidden)ng-hide-remove(if the element will be shown)ng-hide-add-active(if the element will be hidden)ng-hide-remove-active(if the element will be shown)
Using CSS animation
We can use CSS transition or CSS animation to create animation effects for HTML elements; you can refer to our CSS Transition Tutorial, CSS Animation Tutorial for this part.
CSS transition
CSS transitions allow us to smoothly change one CSS property value to another:
Example
When a class is set on a DIV element,.ng-hidethe transition takes 0.5 seconds for the height to change from 100px to 0:
<
style
div {
transition: all linear 0.5s;
background-color: lightblue;
height: 100px;
}
.ng-hide {
height: 0;
}
<
/style
Try it »
CSS Animations
CSS animations allow you to smoothly modify CSS property values:
Example
When a class is set on a DIV element,.ng-hidethe animation will execute, smoothly changing the height from 100px to 0:myChange<
<
style
@keyframes myChange {
from {
height: 100px;
} to {
height: 0;
}
}
div {
height: 100px;
background-color: lightblue;
}
div.ng-hide {
animation: 0.5s myChange;
}
<
/style
Try it »
