2014-01-21 52 views
85

我正在使用ngAnimate模塊,但是我所有的ng-ifng-show等都受此影響,我想爲某些選定的元素使用ngAnimate。 用於表現和隱藏非常快速的元素中的一些錯誤。爲某些元素禁用nganimate

謝謝。

+1

添加一些問題的示例代碼。 – cheekybastard

+0

其中一個問題是ngAnimate強制在所有中繼器上顯示「block」:'''ng-hide-add-active,.ng-hide-remove {display:block!important; }''' – Somatik

+0

要問的更好的問題是「如何定義我的CSS以使ngAnimate正確地爲隱藏的元素進行工作,而無需完成完整的動畫循環」。最好的答案來自Chris Barr。 – Eric

回答

45

只需添加到您的CSS。最好如果是最後一條規則:

.no-animate { 
    -webkit-transition: none !important; 
    transition: none !important; 
} 

然後將no-animate添加到要禁用的元素的類中。例如:

<div class="no-animate"></div> 
+2

如果您使用的是sass,則不需要「-webkit-transition:none!important; 」 –

+8

請注意,此答案是錯誤的,因爲它禁用所有動畫,而不僅僅是由Angular處理的動畫。 – stackular

+0

你試過了嗎?讓我看看你的代碼示例。它適用於我和超過六個人批准它 –

75

有兩種方法可以確定停用動畫在AngularJS如果你有模塊ngAnimate爲您的模塊的依賴關係:

  1. 禁用或在$有生服務全局啓用動畫:

    $animate.enabled(false); 
    
  2. 禁用特定元素的動畫 - 這必須是該角的元素將添加animationstate css類(例如ng-enter,...)!

    $animate.enabled(false, theElement); 
    

由於角1.4版本,你應該扭轉參數:

$animate.enabled(theElement, false); 

的文檔$animatehttps://docs.angularjs.org/api/ng/service/ $動畫

+2

這不適用於預期的特定元素。如果我全局關閉動畫,然後在一個特定元素上啓用它,則不起作用。 –

+1

應該刪除或編輯此答案以指定它實際上在哪個版本的Angular上工作。對於1.2.10,它絕對不適用於有選擇地爲某些元素啓用動畫,這是AFAICT問題的全部要點。 – Trindaz

+0

有誰知道如果選項2是在1.2.25版本上工作? – khorvat

43

感謝,我寫了一個指令哪個y您可以同時在元件上放置

的CoffeeScript:

myApp.directive "disableAnimate", ($animate) -> 
    (scope, element) -> 
    $animate.enabled(false, element) 

的JavaScript:

myApp.directive("disableAnimate", function ($animate) { 
    return function (scope, element) { 
     $animate.enabled(false, element); 
    }; 
}); 
+0

這是一個非常漂亮的方法,它並不總是清楚如何訪問元素對象,並與其他一些方法恐怕我會混淆我的控制器與JavaScript硬參考一個或幾個元素定義在一個模板。這感覺就像一個偉大的關注分離,榮譽! – Mattygabe

+0

向此指令添加'scope:false'是個好主意。 – fracz

+2

參數順序在$ animate.enabled中反轉,如果有人想知道爲什麼這會禁用所有的ng動畫。如果您使用'$ animate.enabled(element,false);' – fearwig

96

如果要啓用特定元素的動畫(而不是禁用它們的特定元素)可以使用$animateProvider來配置具有特定類名稱(或正則表達式)的元素進行動畫處理。

下面的代碼將使對於具有angular-animate類元素的動畫:

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

下面是示例標記包括所述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> 

Plunker例如借用和改性從this blog只有第一個過濾器有動畫(由於有angular-animate類)。

請注意,我使用的是angular-animate作爲示例,它可以使用.classNameFilter函數完全配置。

+5

你節省了我的一天。非常感謝你 – gustavohenke

+0

超級有用! – TrevDev

+0

這是最優雅的解決方案,因爲它需要選擇加入動畫,並且動畫元素通過類名明顯區別於其他元素。 –

17

我發現,$animate.enabled(false, $element);將針對使用ng-showng-hide元素的工作將無法正常工作對於使用ng-if出於某種原因元素!我最終使用的解決方案是在CSS中完成,我從this thread on 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; 
} 

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; 
    } 
} 
+0

我的情況是,加載ngAnimate,像.loading.ng-hide這樣的類將保持在屏幕上,直到完整的動畫循環完成。這是最乾淨的答案,因爲它基本上只是將正確的配置提供給ngAnimate並正常使用它。這是錯誤配置,然後禁用它。所以爲你+1,希望我能給予更多,甚至在這裏得分。 – Eric

+0

這是最棘手的答案。用CSS做這件事比用js搞亂更好。 – manas

+0

請看@Blowsie的答案,那就是處理這個更通用和正確的方法 – edrian

35

要禁用NG-動畫的某些元素,使用CSS類,這是繼角動畫模式,您可以配置NG-動畫使用正則表達式來測試這個類。

配置

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

使用

只需將ng-animate-disabled類添加到要通過NG-動畫被忽略的任何元素。


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

+4

我認爲這是過濾ngannimate行爲最正確的方法! – edrian

+2

是的,這應該被接受的答案。 – Xyroid

+2

這個答案值得滾動。 –

2

我有從該第一li使用ng-hide="$first"隱藏列表。使用ng-enter結果li在消失前顯示半秒鐘。

基於克里斯·巴爾的解決方案,我的代碼現在看起來像這樣:

HTML

<ol> 
    <li ng-repeat="item in items" 
     ng-hide="$first" 
     ng-class="{'no-animate' : $first}"> 
    </li> 
</ol> 

CSS

.no-animate.ng-enter, 
.no-animate.ng-leave, 
.no-animate.ng-animate { 
     transition: none !important; /* disable transitions */ 
     animation: none 0s !important; /* disable keyframe animations */ 
} 

li.ng-enter { 
    opacity: 0; 
    transition: opacity 0.3s ease-out; 
} 
li.ng-enter-active { 
    opacity: 1; 
} 

/* I use Autoprefixer. If you don't, please include vendor prefixes. */ 
3

我做要使用ngAnimate在我的ng-if的,所以這個喔ld是我的解決方案:

[ng-if] { 
    .ng-enter, .ng-leave, .ng-animate { 
    -webkit-transition: none !important; 
    transition: none !important; 
    } 
} 

只是發佈這個作爲另一個建議!

0

我知道這是一個延遲的答覆,但在這裏我們MainController使用:

// disable all animations 
$animate.enabled(false); 

但問題是,當我們關閉所有的動畫,用戶界面,選擇配置爲opacity: 0

因此,它需要使用CSS設置不透明度爲1:

.ui-select-choices { 
    opacity: 1 !important; 
} 

這將正確設置不透明度和UI選會工作。