Skip to content

Commit 06a4540

Browse files
committed
Effects: Reintroduce use of requestAnimationFrame
Same as before, just use don't use prefixes, since they pretty match useless now and use page visibility API to determine if animation should start. Also null the requestAnimationFrame attribute in window for tests since sinon does not provide fake method for it. Fixes #15147 Ref 72119e0
1 parent 13040b6 commit 06a4540

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

src/effects.js

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,18 @@ var
7171
} ]
7272
};
7373

74+
function raf() {
75+
if ( timerId ) {
76+
window.requestAnimationFrame( raf );
77+
jQuery.fx.tick();
78+
}
79+
}
80+
81+
// Will get false negative for old browsers which is okay
82+
function isDocumentHidden() {
83+
return "hidden" in document && document.hidden;
84+
}
85+
7486
// Animations created synchronously will run synchronously
7587
function createFxNow() {
7688
setTimeout(function() {
@@ -460,6 +472,9 @@ jQuery.speed = function( speed, easing, fn ) {
460472

461473
jQuery.fn.extend({
462474
fadeTo: function( speed, to, easing, callback ) {
475+
if ( isDocumentHidden() ) {
476+
return this;
477+
}
463478

464479
// show any hidden elements after setting opacity to 0
465480
return this.filter( isHidden ).css( "opacity", 0 ).show()
@@ -468,6 +483,10 @@ jQuery.fn.extend({
468483
.end().animate({ opacity: to }, speed, easing, callback );
469484
},
470485
animate: function( prop, speed, easing, callback ) {
486+
if ( isDocumentHidden() ) {
487+
return this;
488+
}
489+
471490
var empty = jQuery.isEmptyObject( prop ),
472491
optall = jQuery.speed( speed, easing, callback ),
473492
doAnimation = function() {
@@ -636,7 +655,12 @@ jQuery.fx.interval = 13;
636655

637656
jQuery.fx.start = function() {
638657
if ( !timerId ) {
639-
timerId = setInterval( jQuery.fx.tick, jQuery.fx.interval );
658+
if ( window.requestAnimationFrame ) {
659+
timerId = true;
660+
window.requestAnimationFrame( raf );
661+
} else {
662+
timerId = setInterval( jQuery.fx.tick, jQuery.fx.interval );
663+
}
640664
}
641665
};
642666

test/unit/effects.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@ if ( !jQuery.fx ) {
55
return;
66
}
77

8+
var oldRaf = window.requestAnimationFrame;
9+
810
module("effects", {
911
setup: function() {
12+
window.requestAnimationFrame = null;
1013
this.clock = sinon.useFakeTimers( 505877050 );
1114
this._oldInterval = jQuery.fx.interval;
1215
jQuery.fx.interval = 10;
@@ -15,6 +18,7 @@ module("effects", {
1518
this.clock.restore();
1619
jQuery.fx.stop();
1720
jQuery.fx.interval = this._oldInterval;
21+
window.requestAnimationFrame = oldRaf;
1822
return moduleTeardown.apply( this, arguments );
1923
}
2024
});

0 commit comments

Comments
 (0)