Hey everyone,
I’ve just starting using Angular and went to do a few animations. I had a bit of trouble trying to find anywhere that mentioned what each class does. This is the demo we’ll be making, it’s pretty basic but hopefully enough to get you started: JSFiddle Demo
Dummy Page
To start with, just create a new page with the following. Note that none of the stuff below applies to the animation itself, this is just so that we’ve got something to work with.
Test Animation//ajax.googleapis.com/ajax/libs/angularjs/1.1.5/angular.min.js //Initialisation stuff, note that you don't need any of this for animations, just using it so there's something to see function PeopleController($scope){ $scope.name = ""; $scope.people = [ { name: "Santa Clause"}, { name: "Easter Bunny"}, { name: "Tooth Fairy"}, { name: "Chris Owens"}, { name: "Phillip Farnsworth"}, { name: "Clark Kent"}, { name: "Lana Lang"} ]; $scope.removePerson = function(row){ $scope.people.splice(row, 1); } $scope.addPerson = function(){ $scope.people.push({name: $scope.name}); $scope.name = ""; } }Add{{person.name}}
Adding the ng-animate Directive
The first step to adding the transitions is to add the ng-animate directive:
{{person.name}}
Classes for Adding a New Element
Now we’re going to add the styles that are used when a new element is added to the page. To start with, you’ll want to create .person-enter. This is the class that will be animated from. In this instance we want new elements to start off invisible so we’ll set the opacity to 0.
.person-enter{ -webkit-transition: 1s linear all; -moz-transition: 1s linear all; -ms-transition: 1s linear all; -o-transition: 1s linear all; transition: 1s linear all; opacity: 0; }
Next we’ll want to add the styles for .person-enter.person-enter-active. These will be the styles that we want to animate to. Because we’re going from invisible to visible we’ll adjust the opacity:
.person-enter.person-enter-active{ opacity: 1; }
Classes for Removing an Element
Now we’ll add the class to animate from. This will the opposite of what we used when adding an element – start visible, go to invisble:
.person-leave{ -webkit-transition: 1s linear all; -moz-transition: 1s linear all; -ms-transition: 1s linear all; -o-transition: 1s linear all; transition: 1s linear all; opacity: 1; }
And finally the class to animate to when an element is removed:
.person-leave.person-leave-active{ opacity: 0; }
All Together Now
Finally we can put the whole thing together. The people boxes should now fade out and in whenever an element is added or removed. You can see it in action here: JSFiddle Demo
Test Animation .person{ padding: 20px; width: 50px; height: 50px; background-color: #E8FCE4; border: 1px solid #C0E2BB; float: left; margin: 5px; text-align: center; box-shadow: 2px 2px 2px #DAD3D3; text-shadow: 0px -1px 1px #FFF; cursor: pointer; } /* Note: you can condense these, however they've been split to make the explanation a little easier to follow; */ /* This is the class that will be animated *FROM* when a new person is added */ .person-enter{ -webkit-transition: 1s linear all; -moz-transition: 1s linear all; -ms-transition: 1s linear all; -o-transition: 1s linear all; transition: 1s linear all; opacity: 0; } /* This is the class that will be animated *TO* when a new person is added */ .person-enter.person-enter-active{ opacity: 1; } /* This is the class that will be animated *FROM* when a person is removed */ .person-leave{ -webkit-transition: 1s linear all; -moz-transition: 1s linear all; -ms-transition: 1s linear all; -o-transition: 1s linear all; transition: 1s linear all; opacity: 1; } /* This is the class that will be animated *TO* when a person is removed */ .person-leave.person-leave-active{ opacity: 0; }//ajax.googleapis.com/ajax/libs/angularjs/1.1.5/angular.min.js //Initialisation stuff, note that you don't need any of this for animations, just using it so there's something to see function PeopleController($scope){ $scope.name = ""; $scope.people = [ { name: "Santa Clause"}, { name: "Easter Bunny"}, { name: "Tooth Fairy"}, { name: "Chris Owens"}, { name: "Phillip Farnsworth"}, { name: "Clark Kent"}, { name: "Lana Lang"} ]; $scope.removePerson = function(row){ $scope.people.splice(row, 1); } $scope.addPerson = function(){ $scope.people.push({name: $scope.name}); $scope.name = ""; } }Add{{person.name}}
Problems
If you’re having issues, one of the first things to check is that you’re on the right version of angular. There have been a lot of changes and many of the styles used in one this version don’t seem to work in the older ones. If there’s anything else that’s not working let me know!
Where to From Here?
There’s heaps more that nganimate can do, make sure you checkout http://www.nganimate.org/.
Leave a Reply