Skip to content

Commit ceac435

Browse files
committed
fix(ngAnimate): only trigger animations if the document is not hidden
Prior to this fix, ngAnimate would always trigger animations even if the browser tab or browser window is not in view. This issue was important to fix because browsers do not flush calls to requestAnimationFrame when they are not active. This fix ensures that ngAnimate will respect `document.hidden` in order to get around this. Closes angular#12842
1 parent a1f461e commit ceac435

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

src/ngAnimate/animateQueue.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,8 @@ var $$AnimateQueueProvider = ['$animateProvider', function($animateProvider) {
337337
// this is a hard disable of all animations for the application or on
338338
// the element itself, therefore there is no need to continue further
339339
// past this point if not enabled
340-
var skipAnimations = !animationsEnabled || disabledElementsLookup.get(node);
340+
var doc = $document[0];
341+
var skipAnimations = !animationsEnabled || doc.hidden || disabledElementsLookup.get(node);
341342
var existingAnimation = (!skipAnimations && activeAnimationsLookup.get(node)) || {};
342343
var hasExistingAnimation = !!existingAnimation.state;
343344

test/ngAnimate/animateSpec.js

+25
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,31 @@ describe("animations", function() {
148148
expect(copiedOptions).toEqual(initialOptions);
149149
}));
150150

151+
it("should skip animations entirely if the document is not active", function() {
152+
var doc;
153+
154+
module(function($provide) {
155+
doc = jqLite({
156+
body: document.body,
157+
hidden: true
158+
});
159+
$provide.value('$document', doc);
160+
});
161+
162+
inject(function($animate, $rootScope) {
163+
$animate.enter(element, parent);
164+
$rootScope.$digest();
165+
expect(capturedAnimation).toBeFalsy();
166+
expect(element[0].parentNode).toEqual(parent[0]);
167+
168+
doc[0].hidden = false;
169+
170+
$animate.leave(element);
171+
$rootScope.$digest();
172+
expect(capturedAnimation).toBeTruthy();
173+
});
174+
});
175+
151176
it('should animate only the specified CSS className matched within $animateProvider.classNameFilter', function() {
152177
module(function($animateProvider) {
153178
$animateProvider.classNameFilter(/only-allow-this-animation/);

0 commit comments

Comments
 (0)