禁用某些元素的nganimate

drt*_*bal 92 angularjs ng-animate

我使用的是ngAnimate模块,但我所有的ng-if,ng-show等等,都受到了影响,我想利用ngAnimate对一些选定的元素.对于性能和一些显示和隐藏非常快速的元素的错误.

谢谢.

Glo*_*opy 104

如果要为特定元素启用动画(而不是为特定元素禁用它们),可以使用$ animateProvider配置具有特定类名(或正则表达式)的元素进行动画处理.

下面的代码将为具有angular-animate该类的元素启用动画:

var myApp = angular.module("MyApp", ["ngAnimate"]);
myApp.config(function($animateProvider) {
  $animateProvider.classNameFilter(/angular-animate/);
})
Run Code Online (Sandbox Code Playgroud)

以下是包含angular-animate启用动画的类的示例标记:

<div ng-init="items=[1,2,3,4,5,6,7,8,9]">
  <input placeholder="Filter with animations." ng-model="f" /> 
  <div class="my-repeat-animation angular-animate" ng-repeat="item in items | filter:f track by item" >
    {{item}}
  </div>
</div>
Run Code Online (Sandbox Code Playgroud)

这个博客借用和修改的Plunker示例,其中只有第一个过滤器具有动画(由于具有angular-animate该类).

请注意,我使用的angular-animate是一个例子,它可以使用该.classNameFilter功能完全配置.

  • 使用`/ ^(?:(?!ng-animate-disabled).)*$ /`regex用`ng-animate-disabled`类禁用annimation. (9认同)
  • 你救了我的一天.非常感谢你 (5认同)
  • 这应该是公认的解决方案.工作完美,节省了我的一天! (2认同)

mic*_*ael 80

如果您将模块ngAnimate作为模块的依赖项,有两种方法可以在AngularJS中取消动画:

  1. 在$ animate服务上全局禁用或启用动画:

    $animate.enabled(false);
    
    Run Code Online (Sandbox Code Playgroud)
  2. 禁用特定元素的动画 - 这必须是该角度的元素将添加animationstate css类(例如ng-enter,...)!

    $animate.enabled(false, theElement);
    
    Run Code Online (Sandbox Code Playgroud)

从Angular 1.4版本开始,您应该反转参数:

$animate.enabled(theElement, false);
Run Code Online (Sandbox Code Playgroud)

文档$animate.

  • 这不符合预期的特定元素.如果我全局关闭动画然后在一个特定元素上启用动画,则它不起作用. (2认同)

Dav*_*eye 53

只需将其添加到您的CSS中即可.最好是它是最后一条规则:

.no-animate {
   -webkit-transition: none !important;
   transition: none !important;
}
Run Code Online (Sandbox Code Playgroud)

然后添加no-animate到要禁用的元素类.例:

<div class="no-animate"></div>
Run Code Online (Sandbox Code Playgroud)

  • 请注意,这个答案是错误的,因为它会禁用所有动画,而不仅仅是那些由Angular处理的动画. (14认同)
  • 如果您正在使用sass,则不需要"-webkit-transition:none!important;" (2认同)
  • 我添加了一个`.no-animate,.no-animate-children*'规则来涵盖孩子们等等 (2认同)

use*_*709 44

谢谢,我写了一个你可以放在元素上的指令

CoffeeScript的:

myApp.directive "disableAnimate", ($animate) ->
  (scope, element) ->
    $animate.enabled(false, element)
Run Code Online (Sandbox Code Playgroud)

JavaScript的:

myApp.directive("disableAnimate", function ($animate) {
    return function (scope, element) {
        $animate.enabled(false, element);
    };
});
Run Code Online (Sandbox Code Playgroud)

  • $ animate.enabled中的参数顺序相反,如果有人想知道为什么这会禁用所有动画.如果你使用`$ animate.enabled(element,false);`就像魅力一样 (2认同)

Blo*_*sie 44

要使用遵循Angular动画范例的CSS类禁用某些元素的ng-animate,您可以配置ng-animate以使用regex测试该类.

配置

    var myApp = angular.module("MyApp", ["ngAnimate"]);
    myApp.config(function($animateProvider) {
        $animateProvider.classNameFilter(/^(?:(?!ng-animate-disabled).)*$/);
    })
Run Code Online (Sandbox Code Playgroud)

用法

只需将ng-animate-disabled类添加到ng-animate要忽略的任何元素即可.


信用 http://davidchin.me/blog/disable-nganimate-for-selected-elements/

  • 我认为这是过滤ngannimate行为的最正确方法! (5认同)
  • 这个答案值得滚动. (4认同)
  • 是的,这应该是接受的答案. (3认同)

CBa*_*arr 19

我发现,$animate.enabled(false, $element);将针对使用元素的工作ng-showng-hide,但它不会工作对于使用元素ng-if出于某种原因!我最终使用的解决方案是在CSS中完成所有操作,我从GitHub上的这个线程中学到了这一点.

CSS

/* Use this for transitions */
.disable-animations.ng-enter,
.disable-animations.ng-leave,
.disable-animations.ng-animate {
  -webkit-transition: none !important;
  transition: none !important;
}

/* Use this for keyframe animations */
.disable-animations.ng-animate {
  -webkit-animation: none 0s;
  animation: none 0s;
}
Run Code Online (Sandbox Code Playgroud)

SCSS

.disable-animations {
  // Use this for transitions
  &.ng-enter,
  &.ng-leave,
  &.ng-animate {
    -webkit-transition: none !important;
    transition: none !important;
  }
  // Use this for keyframe animations
  &.ng-animate {
    -webkit-animation: none 0s;
    animation: none 0s;
  }
}
Run Code Online (Sandbox Code Playgroud)