angularjs - ng-animate 脉冲背景颜色通过添加类,然后删除类

标签 angularjs ng-animate

我正在制作一个简单的动画,它会在数组更改时使背景颜色脉动。

笨蛋:http://plnkr.co/edit/27K6B6vHa4ayuPbgRSP3?p=preview

我的指令:

app.directive('animateOnChange', ['$animate', function($animate) {
  return function(scope, elem, attr) {
    scope.$watchCollection(attr.animateOnChange, function() {
      $animate.addClass(elem, 'on').then(function() {
        $animate.removeClass(elem, 'on');
      });
    });
  };
}]);

似乎 addClassremoveClass 同时运行,on 类永远不会被删除。它淡入淡出,但不会淡出。

最佳答案

将其包装在 $timeout 回调中以强制执行两个不同的摘要:

app.directive('animateOnChange', ['$animate', '$timeout', function($animate, $timeout) {
  return function(scope, elem, attr) {
    scope.$watchCollection(attr.animateOnChange, function() {
      console.log('items changed');
      $animate.addClass(elem, 'on').then(function() {
        $timeout(function(){
          $animate.removeClass(elem, 'on');
        }, 0);
      });
    });
  };
}]);

http://plnkr.co/edit/cWciPY4zJ8lSr31CECMS?p=preview

有很多方法可以做到这一点,但这可能是最简单的。

关于angularjs - ng-animate 脉冲背景颜色通过添加类,然后删除类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27465076/

相关文章:

javascript - AngularJs POST 方法到 springboot api

angularjs - 当 stateparams 更改时,ng-if 上的 ng-animate 会执行 "enter",但不会执行 "leave"

css - 如何在 ng-show 显示/显示元素之前添加延迟

php - 如何访问从 PHP 中的 HTTP POST 请求获取的数组对象的属性?

css - Angular Material 按钮禁用样式更改

angularjs 链式淡入/淡出过渡

angularjs - 在 angular 1.2.x 中动画 ng-repeat ng-move

javascript - 卡片翻转过渡在 IE 中无法正常工作

javascript - 为数组中的数字扩展 Angular 过滤器

ajax - AngularJs - JSON 帖子