我正在使用ngAnimate模塊,但是我所有的ng-if
,ng-show
等都受此影響,我想爲某些選定的元素使用ngAnimate。 用於表現和隱藏非常快速的元素中的一些錯誤。爲某些元素禁用nganimate
謝謝。
我正在使用ngAnimate模塊,但是我所有的ng-if
,ng-show
等都受此影響,我想爲某些選定的元素使用ngAnimate。 用於表現和隱藏非常快速的元素中的一些錯誤。爲某些元素禁用nganimate
謝謝。
只需添加到您的CSS。最好如果是最後一條規則:
.no-animate {
-webkit-transition: none !important;
transition: none !important;
}
然後將no-animate
添加到要禁用的元素的類中。例如:
<div class="no-animate"></div>
如果您使用的是sass,則不需要「-webkit-transition:none!important; 」 –
請注意,此答案是錯誤的,因爲它禁用所有動畫,而不僅僅是由Angular處理的動畫。 – stackular
你試過了嗎?讓我看看你的代碼示例。它適用於我和超過六個人批准它 –
有兩種方法可以確定停用動畫在AngularJS如果你有模塊ngAnimate爲您的模塊的依賴關係:
禁用或在$有生服務全局啓用動畫:
$animate.enabled(false);
禁用特定元素的動畫 - 這必須是該角的元素將添加animationstate css類(例如ng-enter,...)!
$animate.enabled(false, theElement);
由於角1.4版本,你應該扭轉參數:
$animate.enabled(theElement, false);
的文檔$animate
:https://docs.angularjs.org/api/ng/service/ $動畫
感謝,我寫了一個指令哪個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);
};
});
如果要啓用特定元素的動畫(而不是禁用它們的特定元素)可以使用$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
函數完全配置。
你節省了我的一天。非常感謝你 – gustavohenke
超級有用! – TrevDev
這是最優雅的解決方案,因爲它需要選擇加入動畫,並且動畫元素通過類名明顯區別於其他元素。 –
我發現,$animate.enabled(false, $element);
將針對使用ng-show
或ng-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;
}
}
要禁用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/
我有從該第一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. */
我做不要使用ngAnimate在我的ng-if
的,所以這個喔ld是我的解決方案:
[ng-if] {
.ng-enter, .ng-leave, .ng-animate {
-webkit-transition: none !important;
transition: none !important;
}
}
只是發佈這個作爲另一個建議!
我知道這是一個延遲的答覆,但在這裏我們MainController使用:
// disable all animations
$animate.enabled(false);
但問題是,當我們關閉所有的動畫,用戶界面,選擇配置爲opacity: 0
。
因此,它需要使用CSS設置不透明度爲1:
.ui-select-choices {
opacity: 1 !important;
}
這將正確設置不透明度和UI選會工作。
添加一些問題的示例代碼。 – cheekybastard
其中一個問題是ngAnimate強制在所有中繼器上顯示「block」:'''ng-hide-add-active,.ng-hide-remove {display:block!important; }''' – Somatik
要問的更好的問題是「如何定義我的CSS以使ngAnimate正確地爲隱藏的元素進行工作,而無需完成完整的動畫循環」。最好的答案來自Chris Barr。 – Eric