Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 7c605dd

Browse files
matskomhevery
authored andcommitted
fix($animate): skip ngAnimate animations if the provided element already has transitions/durations attached to it
Closes #3587
1 parent ee2f3d2 commit 7c605dd

File tree

4 files changed

+92
-52
lines changed

4 files changed

+92
-52
lines changed

docs/src/templates/css/animations.css

-7
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,6 @@
6464
height:0;
6565
}
6666

67-
.animate-container.animations-off * {
68-
-webkit-transition: none;
69-
-moz-transition: none;
70-
-o-transition: color 0 ease-in; /* opera is special :) */
71-
transition: none;
72-
}
73-
7467
.foldout.ng-enter,
7568
.foldout.ng-hide-add,
7669
.foldout.ng-hide-remove {

src/ng/directive/ngClass.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ function classDirective(name, selector) {
151151
152152
## Animations
153153
154-
Example that demostrates how addition and removal of classes can be animated.
154+
The example below demonstrates how to perform animations using ngClass.
155155
156156
<example animations="true">
157157
<file name="index.html">
@@ -196,6 +196,14 @@ function classDirective(name, selector) {
196196
});
197197
</file>
198198
</example>
199+
200+
201+
## ngClass and pre-existing CSS3 Transitions/Animations
202+
The ngClass directive still supports CSS3 Transitions/Animations even if they do not follow the ngAnimate CSS naming structure.
203+
Therefore, if any CSS3 Transition/Animation styles (outside of ngAnimate) are set on the element, then, if a ngClass animation
204+
is triggered, the ngClass animation will be skipped so that ngAnimate can allow for the pre-existing transition or animation to
205+
take over. This restriction allows for ngClass to still work with standard CSS3 Transitions/Animations that are defined
206+
outside of ngAnimate.
199207
*/
200208
var ngClassDirective = classDirective('', true);
201209

src/ngAnimate/animate.js

+54-44
Original file line numberDiff line numberDiff line change
@@ -267,8 +267,8 @@ angular.module('ngAnimate', ['ng'])
267267
* |----------------------------------------------------------------------------------------------|-----------------------------------------------|
268268
* | 1. $animate.enter(...) is called | class="my-animation" |
269269
* | 2. element is inserted into the parent element or beside the after element | class="my-animation" |
270-
* | 3. the .ng-enter class is added to the element | class="my-animation ng-enter" |
271-
* | 4. $animate runs any JavaScript-defined animations on the element | class="my-animation ng-enter" |
270+
* | 3. $animate runs any JavaScript-defined animations on the element | class="my-animation" |
271+
* | 4. the .ng-enter class is added to the element | class="my-animation ng-enter" |
272272
* | 5. $animate scans the element styles to get the CSS transition/animation duration and delay | class="my-animation ng-enter" |
273273
* | 6. the .ng-enter-active class is added (this triggers the CSS transition/animation) | class="my-animation ng-enter ng-enter-active" |
274274
* | 7. $animate waits for X milliseconds for the animation to complete | class="my-animation ng-enter ng-enter-active" |
@@ -302,8 +302,8 @@ angular.module('ngAnimate', ['ng'])
302302
* | Animation Step | What the element class attribute looks like |
303303
* |----------------------------------------------------------------------------------------------|----------------------------------------------|
304304
* | 1. $animate.leave(...) is called | class="my-animation" |
305-
* | 2. the .ng-leave class is added to the element | class="my-animation ng-leave" |
306-
* | 3. $animate runs any JavaScript-defined animations on the element | class="my-animation ng-leave" |
305+
* | 2. $animate runs any JavaScript-defined animations on the element | class="my-animation" |
306+
* | 3. the .ng-leave class is added to the element | class="my-animation ng-leave" |
307307
* | 4. $animate scans the element styles to get the CSS transition/animation duration and delay | class="my-animation ng-leave" |
308308
* | 5. the .ng-leave-active class is added (this triggers the CSS transition/animation) | class="my-animation ng-leave ng-leave-active |
309309
* | 6. $animate waits for X milliseconds for the animation to complete | class="my-animation ng-leave ng-leave-active |
@@ -337,8 +337,8 @@ angular.module('ngAnimate', ['ng'])
337337
* |----------------------------------------------------------------------------------------------|---------------------------------------------|
338338
* | 1. $animate.move(...) is called | class="my-animation" |
339339
* | 2. element is moved into the parent element or beside the after element | class="my-animation" |
340-
* | 3. the .ng-move class is added to the element | class="my-animation ng-move" |
341-
* | 4. $animate runs any JavaScript-defined animations on the element | class="my-animation ng-move" |
340+
* | 3. $animate runs any JavaScript-defined animations on the element | class="my-animation" |
341+
* | 4. the .ng-move class is added to the element | class="my-animation ng-move" |
342342
* | 5. $animate scans the element styles to get the CSS transition/animation duration and delay | class="my-animation ng-move" |
343343
* | 6. the .ng-move-active class is added (this triggers the CSS transition/animation) | class="my-animation ng-move ng-move-active" |
344344
* | 7. $animate waits for X milliseconds for the animation to complete | class="my-animation ng-move ng-move-active" |
@@ -373,8 +373,8 @@ angular.module('ngAnimate', ['ng'])
373373
* | Animation Step | What the element class attribute looks like |
374374
* |------------------------------------------------------------------------------------------------|---------------------------------------------|
375375
* | 1. $animate.addClass(element, 'super') is called | class="" |
376-
* | 2. the .super-add class is added to the element | class="super-add" |
377-
* | 3. $animate runs any JavaScript-defined animations on the element | class="super-add" |
376+
* | 2. $animate runs any JavaScript-defined animations on the element | class="" |
377+
* | 3. the .super-add class is added to the element | class="super-add" |
378378
* | 4. $animate scans the element styles to get the CSS transition/animation duration and delay | class="super-add" |
379379
* | 5. the .super-add-active class is added (this triggers the CSS transition/animation) | class="super-add super-add-active" |
380380
* | 6. $animate waits for X milliseconds for the animation to complete | class="super-add super-add-active" |
@@ -408,8 +408,8 @@ angular.module('ngAnimate', ['ng'])
408408
* | Animation Step | What the element class attribute looks like |
409409
* |-----------------------------------------------------------------------------------------------|-------------------------------------------------|
410410
* | 1. $animate.removeClass(element, 'super') is called | class="super" |
411-
* | 2. the .super-remove class is added to the element | class="super super-remove" |
412-
* | 3. $animate runs any JavaScript-defined animations on the element | class="super super-remove" |
411+
* | 2. $animate runs any JavaScript-defined animations on the element | class="super" |
412+
* | 3. the .super-remove class is added to the element | class="super super-remove" |
413413
* | 4. $animate scans the element styles to get the CSS transition/animation duration and delay | class="super super-remove" |
414414
* | 5. the .super-remove-active class is added (this triggers the CSS transition/animation) | class="super super-remove super-remove-active" |
415415
* | 6. $animate waits for X milliseconds for the animation to complete | class="super super-remove super-remove-active" |
@@ -494,23 +494,14 @@ angular.module('ngAnimate', ['ng'])
494494
done:done
495495
});
496496

497-
var baseClassName = className;
498-
if(event == 'addClass') {
499-
className = suffixClasses(className, '-add');
500-
} else if(event == 'removeClass') {
501-
className = suffixClasses(className, '-remove');
502-
}
503-
504-
element.addClass(className);
505-
506497
forEach(animations, function(animation, index) {
507498
var fn = function() {
508499
progress(index);
509500
};
510501

511502
if(animation.start) {
512503
if(event == 'addClass' || event == 'removeClass') {
513-
animation.endFn = animation.start(element, baseClassName, fn);
504+
animation.endFn = animation.start(element, className, fn);
514505
} else {
515506
animation.endFn = animation.start(element, fn);
516507
}
@@ -538,7 +529,6 @@ angular.module('ngAnimate', ['ng'])
538529
function done() {
539530
if(!done.hasBeenRun) {
540531
done.hasBeenRun = true;
541-
element.removeClass(className);
542532
element.removeData(NG_ANIMATE_STATE);
543533
(onComplete || noop)();
544534
}
@@ -549,26 +539,45 @@ angular.module('ngAnimate', ['ng'])
549539
$animateProvider.register('', ['$window','$sniffer', '$timeout', function($window, $sniffer, $timeout) {
550540
var noop = angular.noop;
551541
var forEach = angular.forEach;
542+
543+
//one day all browsers will have these properties
544+
var w3cAnimationProp = 'animation';
545+
var w3cTransitionProp = 'transition';
546+
547+
//but some still use vendor-prefixed styles
548+
var vendorAnimationProp = $sniffer.vendorPrefix + 'Animation';
549+
var vendorTransitionProp = $sniffer.vendorPrefix + 'Transition';
550+
551+
var durationKey = 'Duration',
552+
delayKey = 'Delay',
553+
animationIterationCountKey = 'IterationCount',
554+
ELEMENT_NODE = 1;
555+
552556
function animate(element, className, done) {
553557
if (!($sniffer.transitions || $sniffer.animations)) {
554558
done();
555559
return;
556560
}
561+
else if(['ng-enter','ng-leave','ng-move'].indexOf(className) == -1) {
562+
var existingDuration = 0;
563+
forEach(element, function(element) {
564+
if (element.nodeType == ELEMENT_NODE) {
565+
var elementStyles = $window.getComputedStyle(element) || {};
566+
existingDuration = Math.max(parseMaxTime(elementStyles[w3cTransitionProp + durationKey]),
567+
parseMaxTime(elementStyles[vendorTransitionProp + durationKey]),
568+
existingDuration);
569+
}
570+
});
571+
if(existingDuration > 0) {
572+
done();
573+
return;
574+
}
575+
}
557576

558-
//one day all browsers will have these properties
559-
var w3cAnimationProp = 'animation';
560-
var w3cTransitionProp = 'transition';
561-
562-
//but some still use vendor-prefixed styles
563-
var vendorAnimationProp = $sniffer.vendorPrefix + 'Animation';
564-
var vendorTransitionProp = $sniffer.vendorPrefix + 'Transition';
565-
566-
var durationKey = 'Duration',
567-
delayKey = 'Delay',
568-
animationIterationCountKey = 'IterationCount';
577+
element.addClass(className);
569578

570579
//we want all the styles defined before and after
571-
var duration = 0, ELEMENT_NODE = 1;
580+
var duration = 0;
572581
forEach(element, function(element) {
573582
if (element.nodeType == ELEMENT_NODE) {
574583
var elementStyles = $window.getComputedStyle(element) || {};
@@ -615,6 +624,7 @@ angular.module('ngAnimate', ['ng'])
615624
//there is no need to attach this internally to the
616625
//timeout done method
617626
return function onEnd(cancelled) {
627+
element.removeClass(className);
618628
element.removeClass(activeClassName);
619629

620630
//only when the animation is cancelled is the done()
@@ -626,6 +636,7 @@ angular.module('ngAnimate', ['ng'])
626636
}
627637
}
628638
else {
639+
element.removeClass(className);
629640
done();
630641
}
631642

@@ -656,16 +667,15 @@ angular.module('ngAnimate', ['ng'])
656667
}
657668
};
658669

670+
function suffixClasses(classes, suffix) {
671+
var className = '';
672+
classes = angular.isArray(classes) ? classes : classes.split(/\s+/);
673+
forEach(classes, function(klass, i) {
674+
if(klass && klass.length > 0) {
675+
className += (i > 0 ? ' ' : '') + klass + suffix;
676+
}
677+
});
678+
return className;
679+
}
659680
}]);
660-
661-
function suffixClasses(classes, suffix) {
662-
var className = '';
663-
classes = angular.isArray(classes) ? classes : classes.split(/\s+/);
664-
forEach(classes, function(klass, i) {
665-
if(klass && klass.length > 0) {
666-
className += (i > 0 ? ' ' : '') + klass + suffix;
667-
}
668-
});
669-
return className;
670-
}
671681
}]);

test/ngAnimate/animateSpec.js

+29
Original file line numberDiff line numberDiff line change
@@ -1584,4 +1584,33 @@ describe("ngAnimate", function() {
15841584
});
15851585
});
15861586

1587+
it("should skip ngAnimate animations when any pre-existing CSS transitions are present on the element", function() {
1588+
inject(function($compile, $rootScope, $animate, $timeout, $sniffer) {
1589+
if(!$sniffer.transitions) return;
1590+
1591+
var element = html($compile('<div class="animated parent"></div>')($rootScope));
1592+
var child = html($compile('<div class="animated child"></div>')($rootScope));
1593+
1594+
ss.addRule('.animated', 'transition:1s linear all;' +
1595+
vendorPrefix + 'transition:1s linear all');
1596+
ss.addRule('.super-add', 'transition:2s linear all;' +
1597+
vendorPrefix + 'transition:2s linear all');
1598+
1599+
$rootElement.append(element);
1600+
jqLite(document.body).append($rootElement);
1601+
1602+
$animate.addClass(element, 'super');
1603+
$timeout.flush(0);
1604+
1605+
var empty = true;
1606+
try {
1607+
$timeout.flush();
1608+
empty = false;
1609+
}
1610+
catch(e) {}
1611+
1612+
expect(empty).toBe(true);
1613+
});
1614+
});
1615+
15871616
});

0 commit comments

Comments
 (0)