©
Dieses Dokument verwendetPHP-Handbuch für chinesische WebsitesFreigeben
ngAnimate
ThengAnimate
module provides support for JavaScript, CSS3 transition and CSS3 keyframe animation hooks within existing core and custom directives.
To see animations in action, all that is required is to define the appropriate CSS classes or to register a JavaScript animation via the myModule.animation() function. The directives that support animation automatically are:ngRepeat
,ngInclude
,ngIf
,ngSwitch
,ngShow
,ngHide
,ngView
andngClass
. 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 (the CSS class(es) present) |
ngShow & ngHide | add and remove (the ng-hide class value) |
form & ngModel | add and remove (dirty, pristine, valid, invalid & all other validations) |
ngMessages | add and remove (ng-active & ng-inactive) |
ngMessage | enter and leave |
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:
class="slide"ng-include="...">
记住,默认情况下, if an animation is running, any child elements cannot be animated until the parent element's animation has completed. This blocking feature can be overridden by placing theng-animate-children
attribute on a parent container tag.
class="slide-animation"ng-if="on"ng-animate-children>class="fade-animation"ng-if="on">class="explode-animation"ng-if="on">...
When theon
expression value changes and an animation is triggered then each of the elements within will all animate without the block being applied to child elements.
The following code below demonstrates how to perform animations usingCSS transitionswith Angular:
class="view-container">ng-viewclass="reveal-animation">
The following code below demonstrates how to perform animations usingCSS animationswith Angular:
class="view-container">ng-viewclass="reveal-animation">
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 likeng-enter
), then the browser prepares itself to add the active class (in this caseng-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.
Structural transitions (such as enter, leave and move) will always apply a0snone
transition value to force the browser into rendering the styles defined in the setup (.ng-enter, .ng-leave or .ng-move) class. This means that any active transition animations operating on the element will be cut off to make way for the enter, leave or move animation.
Class-based transitions refer to transition animations that are triggered when a CSS class is added to or removed from the element (via$animate.addClass
,$animate.removeClass
,$animate.setClass
, or by directives such asngClass
,ngModel
andform
). They are different when compared to structural animations since theydo not cancel existing animationsnor do theyblock successive transitionsfrom rendering on the same element. This distinction allows formultiple class-based transitionsto be performed on the same element.
In addition to ngAnimate supporting the default (natural) functionality of class-based transition animations, ngAnimate also decorates the element with starting and ending CSS classes to aid the developer in further styling the element throughout the transition animation. Earlier versions of ngAnimate may have caused natural CSS transitions to break and not render properly due to $animate temporarily blocking transitions using0snone
in order to allow the setup CSS class (the-add
or-remove
class) to be applied without triggering an animation. However, as ofversion 1.3, this workaround has been removed with ngAnimate and all non-ngAnimate CSS class transitions are compatible with ngAnimate.
There is, however, one special case when dealing with class-based transitions in ngAnimate. When rendering class-based transitions that make use of the setup and active CSS classes (如:.fade-add
and.fade-add-active
for when.fade
is added) be sure to define the transition valueon the active CSS classand not the setup class.
.fade-add{/* remember to place a 0s transition here to ensure that the styles are applied instantly even if the element already has a transition style */transition:0slinear all;/* starting CSS styles */opacity:1;}.fade-add.fade-add-active{/* this will be the length of the animation */transition:1slinear all;opacity:0;}
The setup CSS class (in this case.fade-add
) also has a transition style property, however, it has a duration of zero. This may not be required, however, incase the browser is unable to render the styling present in this CSS class instantly then it could be that the browser is attempting to perform an unnecessary transition.
This workaround, however, does not apply to standard class-based transitions that are rendered when a CSS class containing a transition is applied to an element:
.fade{/* this works as expected */transition:1slinear all;opacity:0;}
Please keep this in mind when coding the CSS markup that will be used within class-based transitions. Also, try not to mix the two class-based animation flavors together since the CSS code may become overly complex.
A Staggering animation is a collection of animations that are issued with a slight delay in between each successive operation resulting in a curtain-like effect. The ngAnimate module, as of 1.2.0, supports staggering animations and the stagger effect can be performed by creating ang-EVENT-staggerCSS class and attaching that class to the base CSS class used for the animation. The style property expected within the stagger class can either be atransition-delayor ananimation-delayproperty (or both if your animation contains both transitions and keyframe animations).
.my-animation.ng-enter{/* standard transition code */-webkit-transition:1slinear all;transition:1slinear all;opacity:0;}.my-animation.ng-enter-stagger{/* this will have a 100ms delay between each successive leave animation */-webkit-transition-delay:0.1s;transition-delay:0.1s;/* in case the stagger doesn't work then these two values must be set to 0 to avoid an accidental CSS inheritance */-webkit-transition-duration:0s;transition-duration:0s;}.my-animation.ng-enter.ng-enter-active{/* standard transition styles */opacity:1;}
Staggering animations work by default in ngRepeat (so long as the CSS class is defined). Outside of ngRepeat, to use staggering animations on your own, they can be triggered by firing multiple calls to the same event on $animate. However, the restrictions surrounding this are that each of the elements must have the same CSS className value as well as the same parent element. A stagger operation will also be reset if more than 10ms has passed after the last animation has been fired.
The following code will issue theng-leave-staggerevent on the element provided:
varkids=parent.children();$animate.leave(kids[0]);//stagger index=0$animate.leave(kids[1]);//stagger index=1$animate.leave(kids[2]);//stagger index=2$animate.leave(kids[3]);//stagger index=3$animate.leave(kids[4]);//stagger index=4$timeout(Function(){//stagger has reset itself$animate.leave(kids[5]);//stagger index=0$animate.leave(kids[6]);//stagger index=1},100,false);
Stagger animations are currently only supported within CSS-defined animations.
//!annotate="YourApp" Your AngularJS Module|Replace this or ngModule with the module that you used to define your application.varngModule=angular.module('YourApp',['ngAnimate']);ngModule.animation('.my-crazy-animation',Function(){return{enter:Function(element,done){//run the animation here and call done when the animation is completereturnFunction(cancelled){//this (可选) function will be called when the animation//completes or when the animation is cancelled (the cancelled//flag will be set to true if cancelled).};},leave:Function(element,done){},move:Function(element,done){},//animation that can be triggered before the class is addedbeforeAddClass:Function(element,className,done){},//animation that can be triggered after the class is addedaddClass:Function(element,className,done){},//animation that can be triggered before the class is removedbeforeRemoveClass:Function(element,className,done){},//animation that can be triggered after the class is removedremoveClass: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 will be executed. It should be also noted that only simple, single class selectors are allowed (compound class selectors are not supported).
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).
First includeangular-animate.js
in your HTML: