Skip to content

Commit 72119e0

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
1 parent bbdfbb4 commit 72119e0

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

src/effects.js

Lines changed: 25 additions & 2 deletions
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() {
@@ -452,6 +464,9 @@ jQuery.speed = function( speed, easing, fn ) {
452464

453465
jQuery.fn.extend({
454466
fadeTo: function( speed, to, easing, callback ) {
467+
if ( isDocumentHidden() ) {
468+
return this;
469+
}
455470

456471
// Show any hidden elements after setting opacity to 0
457472
return this.filter( isHidden ).css( "opacity", 0 ).show()
@@ -460,6 +475,10 @@ jQuery.fn.extend({
460475
.end().animate({ opacity: to }, speed, easing, callback );
461476
},
462477
animate: function( prop, speed, easing, callback ) {
478+
if ( isDocumentHidden() ) {
479+
return this;
480+
}
481+
463482
var empty = jQuery.isEmptyObject( prop ),
464483
optall = jQuery.speed( speed, easing, callback ),
465484
doAnimation = function() {
@@ -625,10 +644,14 @@ jQuery.fx.timer = function( timer ) {
625644
};
626645

627646
jQuery.fx.interval = 13;
628-
629647
jQuery.fx.start = function() {
630648
if ( !timerId ) {
631-
timerId = setInterval( jQuery.fx.tick, jQuery.fx.interval );
649+
if ( window.requestAnimationFrame ) {
650+
timerId = true;
651+
window.requestAnimationFrame( raf );
652+
} else {
653+
timerId = setInterval( jQuery.fx.tick, jQuery.fx.interval );
654+
}
632655
}
633656
};
634657

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;
@@ -17,6 +20,7 @@ module("effects", {
1720
jQuery.now = Date.now;
1821
jQuery.fx.stop();
1922
jQuery.fx.interval = this._oldInterval;
23+
window.requestAnimationFrame = oldRaf;
2024
return moduleTeardown.apply( this, arguments );
2125
}
2226
});

0 commit comments

Comments
 (0)