So far, the study cases we have done are without any animation effect, for the past, We often go to use some animation plug-ins or CSS framework (such as: ANIMATE.CSS) to decorate our web page, so it appears vivid, tall, then we can learn, how to angularjs down to realize the cool animation operation, the main use of the command is Nganimate.
As with the previous Ngresource,ngroute, You need to inject nganimate and introduce ng-animate.js to use this service, you want to use nganimate in your angular application to animate functionality, provided the nganimate is included in your application, animation is converted by using CSS/ Animation or JavaScript callbacks. Angular itself various core NG directives will provide animated hooks, animated instructions are ngrepeat, nginclude, Ngif, Ngswitch,ngshow, Nghide, Ngview and Ngclass, Of course, custom commands can also be animated by using the $animate service, whose individual instructions support the animation in the following table:
- ng-Enter class is used primarily for newly added elements and renders to the page, adding a class ng-enter-active
- The Ng-move class is primarily used when the element is moved, and then the class ng-move-active is added
- Ng-leave class is used primarily for deletion, a class ng-leave-active is added after deletion
- Ng-hide,ng-show class is used for judging execution, and there will be several css,ng-hide-add,ng-hide-add-active,ng-hide-remove,ng-hide-remove-active in the corresponding Class that is dynamically added when using the ng-show or ng-hide instruction to manipulate the DOM
- For forms, there are also different validation attributes on previous learning notes, and the resulting class (such as input is invalid with class= "Ng-invalid") to define its display style
It must be understood that: (1) The parent element animation is not finished, the child element animation does not execute, but you can mask ng-animate-children This behavior, plus
(2) When using $http to obtain remote data, it will automatically extend the animation time, application loading, animation not immediately executed!
By examining the elements of Phonecat above (http://angular.github.io/angular-phonecat/step-12/app/#/phones) and observing their changes, it is not difficult to see Angularjs can use the Nganimate module to give different classes at different points in time, and then through the definition of these class CSS, to achieve animation operation! Think still need to give examples to learn, more easy to understand! Mainly in two parts to illustrate, css-defined animations and javascript-defined animations.
css-defined Animations
Remember the previous study note-angularjs (iii) filter the ng-repeat using filter? We will now modify the previous code, change it to filter to retrieve the song, the code is as follows:
<!doctype html>
'Animate-css'> "UTF8"/> <script src="Angular.min.js"></script> <script src="Angular-animate.js"></script>//Using the Nganimate module requires the introduction of Angular-animate.js<script>Angular.module ('Animate-css', ['nganimate'])//inject nganimate so that animate animation effects are automatically applied to the project, so you need to define CSS to change the style . Controller ('Songcontroller', ['$scope', Function ($scope) {$scope. Songs=['Love you for 10,000 years','Happy monkeys.','Beijing welcomes You','laughing and proud of the lake','Practice','Love Business','Qi qi Xiang','love when you're dead.','Beijing Love Story','Star Lighting','Starry Sky','soy milk and fritters','Myth']; }]); </script> <style>
*/* As mentioned above, angular will have different classes at different points in time, it is the use of these classes to make animation, must understand ng-enter,ng-enter-active,ng-leave,ng-leave-active, Ng-move,ng-move-active The order of these classes * /
li{list-Style:none;} body{margin:50px; background-color: #333; color: #ccc; overflow:hidden;} H3{color: #fff;} . Song-list.ng-enter,. Song-list.ng-leave,. Song-list.ng-Move {-webkit-transition:0. 5s linear all; -moz-transition:0. 5s linear all; -o-transition:0. 5s linear all; Transition:0. 5s linear all; }. Song-list.ng-enter,. Song-list.ng-Move {opacity:0; Height:0; Overflow:hidden; }. Song-list.ng-move.ng-move-active,. Song-list.ng-enter.ng-enter-Active {opacity:1; height:120px; }. Song-list.ng-Leave {opacity:1; Overflow:hidden; }. Song-list.ng-leave.ng-leave-Active {opacity:0; Height:0; Padding-top:0; Padding-bottom:0; } </style> "Songcontroller"> <input type="text"Ng-model="Search"> <button type="Submit">Filter</button> <ul> <liclass="song-list"ng-repeat="Song in songs | Filter:search">{{Song}}</li> </ul> </div> </body>
javascript-defined Animations
If you do not want to use CSS3 conversion or CSS3 animations, if you want to provide animations that do not support CSS browser conversion/animation, then you can use JavaScript animation to define the Angularjs module, which is custom animation, to achieve personalized animation effect, First look at how the official website is defined using JavaScript animations:
//!annotate= "YourApp" Your AngularJS module| Replace this or ngmodule with the module, 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 do when the animation are complete returnfunction (cancelled) {//This (optional) function is called when the animation//completes or when the animation is cancelled (the cancelled//flag would be set to true if cancelled). }; }, Leave:function (element, done) {}, Move:function (element, done) {},//animation that can being triggered before the class is addedbeforeaddclass:function (element, className, done) {},//animation that can being triggered after the class is addedaddclass:function (element, className, done) {},//animation that can being triggered before the class is removedbeforeremoveclass:function (element, className, done) {},//animation that can being triggered after the class is removedremoveclass:function (element, className, done) {}};});
It is not difficult to see that you can define not only the Enter (add Element), move (move Element), leave (delete element) and other states, but also can increase addclass, beforeremoveclass, Removeclass and other listening events. So let's modify the demo of the above filter song name:
<!doctype html>'Animate-javascript'> "UTF8"/><script src= "Jquery.js" ></script><script src="Angular.min.js"></script> <script src="Angular-animate.js"></script> <script>varJAV = Angular.module ('Animate-javascript', ['nganimate']); Jav.controller ('Songcontroller', ['$scope', Function ($scope) {$scope. Songs=['Love you for 10,000 years','Happy monkeys.','Beijing welcomes You','laughing and proud of the lake','Practice','Love Business','Qi qi Xiang','love when you're dead.','Beijing Love Story','Star Lighting','Starry Sky','soy milk and fritters','Myth']; }]); Jav.animation (". Repeat-animation", function () { ///after we introduce Angular-animate.js and inject nganimate module, we can use. Animation (Element,function () {...}) To define the animation, here we define a class for the. Repeat-animation Animation return{enter:function (element, done) { //The function format for animation behavior is functions (Element,done) {...}, the element here refers to a J Query object (precondition must introduce jquery.js), done is the end of the callback function varwidth =element.width (); Element.css ({position:'relative', left:-20, opacity:0 }); Element.animate ({left:0, opacity:1}, done); }, Leave:function (element, done) {element.css ({position : 'relative', left:0, opacity:1 }); Element.animate ({left:-Ten, opacity:0}, done); }, Move:function (element, done) {Element.css ({left: "5px", opacity:0.2 }); Element.animate ({left:"0px", opacity:1}, done); } }; }) </script> <style>li{list-Style:none;} body{margin:50px; background-color: #333; color: #ccc; overflow:hidden;} H3{color: #fff;} </style> "Songcontroller"> <input type="text"Ng-model="Search"> <button type="Submit">Filter</button> <ul> <liclass="song-list repeat-animation "ng-repeat="Song in songs | Filter:search">{{Song}}</li> </ul> </div> </body>
This is a general understanding of the ng-animate, there are many missing points (such as $animate services, etc.), and then learned, will be added, then the other instructions of the Custom Animation code demo update to GitHub!
Study notes-angularjs (ix)