Skip to content

Commit 708764f

Browse files
committedJun 15, 2014
Effects: Improve raf logic
* Make animation behave as if jQuery.fx.off = true if document is hidden * Use cancelAnimationFrame in jQuery.fx.stop Closes gh-1578
1 parent 72119e0 commit 708764f

File tree

2 files changed

+46
-21
lines changed

2 files changed

+46
-21
lines changed
 

‎src/effects.js

+18-21
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() {
@@ -438,8 +433,15 @@ jQuery.speed = function( speed, easing, fn ) {
438433
easing: fn && easing || easing && !jQuery.isFunction( easing ) && easing
439434
};
440435

441-
opt.duration = jQuery.fx.off ? 0 : typeof opt.duration === "number" ? opt.duration :
442-
opt.duration in jQuery.fx.speeds ? jQuery.fx.speeds[ opt.duration ] : jQuery.fx.speeds._default;
436+
// Go to the end state if fx are off or if document is hidden
437+
if ( jQuery.fx.off || document.hidden ) {
438+
opt.duration = 0;
439+
440+
} else {
441+
opt.duration = typeof opt.duration === "number" ?
442+
opt.duration : opt.duration in jQuery.fx.speeds ?
443+
jQuery.fx.speeds[ opt.duration ] : jQuery.fx.speeds._default;
444+
}
443445

444446
// Normalize opt.queue - true/undefined/null -> "fx"
445447
if ( opt.queue == null || opt.queue === true ) {
@@ -464,9 +466,6 @@ jQuery.speed = function( speed, easing, fn ) {
464466

465467
jQuery.fn.extend({
466468
fadeTo: function( speed, to, easing, callback ) {
467-
if ( isDocumentHidden() ) {
468-
return this;
469-
}
470469

471470
// Show any hidden elements after setting opacity to 0
472471
return this.filter( isHidden ).css( "opacity", 0 ).show()
@@ -475,10 +474,6 @@ jQuery.fn.extend({
475474
.end().animate({ opacity: to }, speed, easing, callback );
476475
},
477476
animate: function( prop, speed, easing, callback ) {
478-
if ( isDocumentHidden() ) {
479-
return this;
480-
}
481-
482477
var empty = jQuery.isEmptyObject( prop ),
483478
optall = jQuery.speed( speed, easing, callback ),
484479
doAnimation = function() {
@@ -646,17 +641,19 @@ jQuery.fx.timer = function( timer ) {
646641
jQuery.fx.interval = 13;
647642
jQuery.fx.start = function() {
648643
if ( !timerId ) {
649-
if ( window.requestAnimationFrame ) {
650-
timerId = true;
651-
window.requestAnimationFrame( raf );
652-
} else {
653-
timerId = setInterval( jQuery.fx.tick, jQuery.fx.interval );
654-
}
644+
timerId = window.requestAnimationFrame ?
645+
window.requestAnimationFrame( raf ) :
646+
setInterval( jQuery.fx.tick, jQuery.fx.interval );
655647
}
656648
};
657649

658650
jQuery.fx.stop = function() {
659-
clearInterval( timerId );
651+
if ( window.cancelAnimationFrame ) {
652+
window.cancelAnimationFrame( timerId );
653+
} else {
654+
clearInterval( timerId );
655+
}
656+
660657
timerId = null;
661658
};
662659

‎test/unit/effects.js

+28
Original file line numberDiff line numberDiff line change
@@ -2183,4 +2183,32 @@ test( "Respect display value on inline elements (#14824)", 2, function() {
21832183
clock.tick( 800 );
21842184
});
21852185

2186+
test( "Animation should go to its end state if document.hidden = true", 1, function() {
2187+
var height;
2188+
if ( Object.defineProperty ) {
2189+
2190+
// Can't rewrite document.hidden property if its host property
2191+
try {
2192+
Object.defineProperty( document, "hidden", {
2193+
get: function() {
2194+
return true;
2195+
}
2196+
});
2197+
} catch ( e ) {}
2198+
} else {
2199+
document.hidden = true;
2200+
}
2201+
2202+
if ( document.hidden ) {
2203+
height = jQuery( "#qunit-fixture" ).animate({ height: 500 } ).height();
2204+
2205+
equal( height, 500, "Animation should happen immediately if document.hidden = true" );
2206+
jQuery( document ).removeProp( "hidden" );
2207+
2208+
} else {
2209+
ok( true, "Can't run the test since we can't reproduce correct environment for it" );
2210+
}
2211+
});
2212+
2213+
21862214
})();

0 commit comments

Comments
 (0)