【问题标题】:AngularJS animation on clickAngularJS 动画点击
【发布时间】:2014-05-03 00:32:57
【问题描述】:

我正在尝试添加一个指令,该指令在单击元素时对其进行动画处理,但我的指令中的代码似乎永远不会触发。

Fiddle

html:

<img class="refresh" animate-spin src="refresh-icon-614x460.png">

js:

myApp.directive('animateSpin', ['$animate', function($animate) {
  return function(element, scope, attrs) {
    element.on('click', function() {
        $animate.addClass(element, 'spin');
    });
  };
}]);

【问题讨论】:

  • 您的示例使用 AngularJS v1.0.5,是您要求的版本吗?
  • @tasseKATT 更新为使用新版本
  • 以后有空再回答。
  • 我想我知道我做错了什么。 (一切)我稍后会发布我自己的解决方案

标签: angularjs angularjs-directive css-animations angularjs-animation


【解决方案1】:

我意识到我使用 $animate 完全错误。这是我想出的解决方案。单击该指令会将自旋类以及自旋添加类添加到元素中。动画将运行,旋转类将被删除,以便下次单击时可以再次添加。

directiveModule.directive('animateSpin', ['$animate',
    function ($animate) {
        return {
            link: function (scope, elem, attrs) {
                elem.on('click', function () {
                    var self = angular.element(this);
                    $animate.addClass(self, 'spin', function () {
                        self.removeClass('spin');
                    });
                });
            }
        };
}]);

CSS:

.spin-add{
   -webkit-animation:spin 4s linear;
    -moz-animation:spin 4s linear;
    animation:spin 4s linear; 
}

【讨论】:

  • @IanB 您不能在不先向作者指出这一点的情况下通过编辑并发布其他代码来更改答案,因为这可能不是他的意图。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-09-07
  • 1970-01-01
  • 2011-09-08
  • 2014-11-15
  • 1970-01-01
  • 2017-05-18
相关资源
最近更新 更多