Skip to content

Commit 9dc29a2

Browse files
committed
Effects: Improve raf logic
* Make animation behave as if jQuery.fx.off = true if document is hidden * Use cancelAnimationFrame in jQuery.fx.stop Ref 708764f
1 parent 06a4540 commit 9dc29a2

File tree

2 files changed

+46
-24
lines changed

2 files changed

+46
-24
lines changed

src/effects.js

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,6 @@ function raf() {
7878
}
7979
}
8080

81-
// Will get false negative for old browsers which is okay
82-
function isDocumentHidden() {
83-
return "hidden" in document && document.hidden;
84-
}
85-
8681
// Animations created synchronously will run synchronously
8782
function createFxNow() {
8883
setTimeout(function() {
@@ -446,8 +441,15 @@ jQuery.speed = function( speed, easing, fn ) {
446441
easing: fn && easing || easing && !jQuery.isFunction( easing ) && easing
447442
};
448443

449-
opt.duration = jQuery.fx.off ? 0 : typeof opt.duration === "number" ? opt.duration :
450-
opt.duration in jQuery.fx.speeds ? jQuery.fx.speeds[ opt.duration ] : jQuery.fx.speeds._default;
444+
// Go to the end state if fx are off or if document is hidden
445+
if ( jQuery.fx.off || document.hidden ) {
446+
opt.duration = 0;
447+
448+
} else {
449+
opt.duration = typeof opt.duration === "number" ?
450+
opt.duration : opt.duration in jQuery.fx.speeds ?
451+
jQuery.fx.speeds[ opt.duration ] : jQuery.fx.speeds._default;
452+
}
451453

452454
// normalize opt.queue - true/undefined/null -> "fx"
453455
if ( opt.queue == null || opt.queue === true ) {
@@ -472,21 +474,13 @@ jQuery.speed = function( speed, easing, fn ) {
472474

473475
jQuery.fn.extend({
474476
fadeTo: function( speed, to, easing, callback ) {
475-
if ( isDocumentHidden() ) {
476-
return this;
477-
}
478-
479477
// show any hidden elements after setting opacity to 0
480478
return this.filter( isHidden ).css( "opacity", 0 ).show()
481479

482480
// animate to the value specified
483481
.end().animate({ opacity: to }, speed, easing, callback );
484482
},
485483
animate: function( prop, speed, easing, callback ) {
486-
if ( isDocumentHidden() ) {
487-
return this;
488-
}
489-
490484
var empty = jQuery.isEmptyObject( prop ),
491485
optall = jQuery.speed( speed, easing, callback ),
492486
doAnimation = function() {
@@ -654,18 +648,18 @@ jQuery.fx.timer = function( timer ) {
654648
jQuery.fx.interval = 13;
655649

656650
jQuery.fx.start = function() {
657-
if ( !timerId ) {
658-
if ( window.requestAnimationFrame ) {
659-
timerId = true;
660-
window.requestAnimationFrame( raf );
661-
} else {
662-
timerId = setInterval( jQuery.fx.tick, jQuery.fx.interval );
663-
}
664-
}
651+
timerId = window.requestAnimationFrame ?
652+
window.requestAnimationFrame( raf ) :
653+
setInterval( jQuery.fx.tick, jQuery.fx.interval );
665654
};
666655

667656
jQuery.fx.stop = function() {
668-
clearInterval( timerId );
657+
if ( window.cancelAnimationFrame ) {
658+
window.cancelAnimationFrame( timerId );
659+
} else {
660+
clearInterval( timerId );
661+
}
662+
669663
timerId = null;
670664
};
671665

test/unit/effects.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2239,4 +2239,32 @@ test( "Respect display value on inline elements (#14824)", 2, function() {
22392239
clock.tick( 800 );
22402240
});
22412241

2242+
test( "Animation should go to its end state if document.hidden = true", 1, function() {
2243+
var height;
2244+
if ( Object.defineProperty ) {
2245+
2246+
// Can't rewrite document.hidden property if its host property
2247+
try {
2248+
Object.defineProperty( document, "hidden", {
2249+
get: function() {
2250+
return true;
2251+
}
2252+
});
2253+
} catch ( e ) {}
2254+
} else {
2255+
document.hidden = true;
2256+
}
2257+
2258+
if ( document.hidden ) {
2259+
height = jQuery( "#qunit-fixture" ).animate({ height: 500 } ).height();
2260+
2261+
equal( height, 500, "Animation should happen immediately if document.hidden = true" );
2262+
jQuery( document ).removeProp( "hidden" );
2263+
2264+
} else {
2265+
ok( true, "Can't run the test since we can't reproduce correct environment for it" );
2266+
}
2267+
});
2268+
2269+
22422270
})();

0 commit comments

Comments
 (0)