Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ng-animate : Animation when model changes

I have created a table in which user can increase and decrease the value. See the Fiddle

//sample code as its not allowing me to push the link to JSFiddle with out pasting code     <tr ng-repeat="d in dataSource" ng-animate="'animate'">  // css - as from angular page .animate-enter {     -webkit-transition: 1s linear all; /* Chrome */     transition: 1s linear all;     background-color:Yellow; }  .animate-enter.animate-enter-active {     background-color:Red; } 

I want to do animation when the model updates i.e the table column changes in background color From Red to white in case user changes the value.

So when you click up arrow or down arrow in any perticular column, the background color of that table column changes from Red to white.

I am not able to get my head around it. Any pointers on how to achieve this ?

like image 317
Anand Avatar asked Nov 18 '13 17:11

Anand


People also ask

Which animation strategy would you use to apply multiple styles for a transition?

The keyframes() function in Angular allows you to specify multiple interim styles within a single transition, with an optional offset to define the point in the animation where each style change should occur.

Which animation strategy would you use if there were multiple animations that had to occur at the same time?

A staggered animation consists of sequential or overlapping animations. To create a staggered animation, use multiple Animation objects. One AnimationController controls all of the Animation s.

What is ngAnimate?

The ngAnimate module provides support for CSS-based animations (keyframes and transitions) as well as JavaScript-based animations via callback hooks. Animations are not enabled by default, however, by including ngAnimate the animation hooks are enabled for an AngularJS app.


2 Answers

There are couple of issues in your code:

  1. NEVER do DOM manipulations in the code of controller: $(elem).animate(.. is something you should avoid. Only in directives you can manipulate with DOM element.

  2. In 1.2+ versions of AngularJS you need to reference ngAnimate module.

  3. It is better to do CSS3 animations with fallback to js-based animations.

I propose to write a directive that will track changes and add a class that will trigger the animation and then remove it:

app.directive('animateOnChange', function($animate,$timeout) {   return function(scope, elem, attr) {       scope.$watch(attr.animateOnChange, function(nv,ov) {         if (nv!=ov) {           var c = nv > ov?'change-up':'change';           $animate.addClass(elem,c).then(function() {             $timeout(function() {$animate.removeClass(elem,c);});           });         }       });    }; }); 

Working example: http://plnkr.co/edit/zs495osfSnWSvWBIn3rh?p=preview

like image 101
Valentyn Shybanov Avatar answered Oct 02 '22 15:10

Valentyn Shybanov


This can be solved with a simple directive and CSS3 animations.

HTML

<span animate-on-change="someValue">{{someValue}}</span> 

Directive

myModule.directive('animateOnChange', function($timeout) {   return function(scope, element, attr) {     scope.$watch(attr.animateOnChange, function(nv,ov) {       if (nv!=ov) {         element.addClass('changed');         $timeout(function() {           element.removeClass('changed');         }, 1000); // Could be enhanced to take duration as a parameter       }     });   }; }); 

CSS

[animate-on-change] {   transition: all 1s;   -webkit-transition: all 1s; } [animate-on-change].changed {   background-color: red;   transition: none;   -webkit-transition: none; } 

Fiddle

like image 34
Clay Avatar answered Oct 02 '22 13:10

Clay