🌐 AI搜索 & 代理 主页
Skip to content

Commit 03d873c

Browse files
committed
[WIP] Effects: Use requestAnimationFrame timestamp if available
In some environments that support the requestAnimationFrame timestamp callback parameter using it results in smoother animations. Fixes gh-3143
1 parent ad6a94c commit 03d873c

File tree

2 files changed

+32
-5
lines changed

2 files changed

+32
-5
lines changed

src/effects.js

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,29 @@ var
2727
rfxtypes = /^(?:toggle|show|hide)$/,
2828
rrun = /queueHooks$/;
2929

30-
function raf() {
30+
function raf( timestamp ) {
3131
if ( timerId ) {
3232
window.requestAnimationFrame( raf );
33-
jQuery.fx.tick();
33+
jQuery.fx.tick( timestamp );
3434
}
3535
}
3636

37+
// We need to be using jQuery.now() or performance.now() consistently as they return different
38+
// values: performance.now() counter starts on page load.
39+
// Support: IE <10, Safari <8.0, iOS <9, Android <4.4, Node with jsdom 9.4
40+
function getTimestamp() {
41+
if ( window.performance && typeof window.performance.now === "function" ) {
42+
return window.performance.now();
43+
}
44+
return jQuery.now();
45+
}
46+
3747
// Animations created synchronously will run synchronously
3848
function createFxNow() {
3949
window.setTimeout( function() {
4050
fxNow = undefined;
4151
} );
42-
return ( fxNow = jQuery.now() );
52+
return ( fxNow = getTimestamp() );
4353
}
4454

4555
// Generate parameters to create a standard animation
@@ -626,12 +636,12 @@ jQuery.each( {
626636
} );
627637

628638
jQuery.timers = [];
629-
jQuery.fx.tick = function() {
639+
jQuery.fx.tick = function( timestamp ) {
630640
var timer,
631641
i = 0,
632642
timers = jQuery.timers;
633643

634-
fxNow = jQuery.now();
644+
fxNow = timestamp || getTimestamp();
635645

636646
for ( ; i < timers.length; i++ ) {
637647
timer = timers[ i ];

test/unit/effects.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,32 @@ QUnit.module( "effects", {
1717
this.sandbox = sinon.sandbox.create();
1818
this.clock = this.sandbox.useFakeTimers( 505877050 );
1919
this._oldInterval = jQuery.fx.interval;
20+
this._oldPerformanceNow = window.performance.now;
2021
jQuery.fx.step = {};
2122
jQuery.fx.interval = 10;
23+
24+
// Fake performance.now() returning lower values than Date.now()
25+
// and that its values are fractional.
26+
// Support: IE <10, Safari <8.0, iOS <9, Android <4.4, Node with jsdom 9.4
27+
if ( window.performance && typeof window.performance.now === "function" ) {
28+
window.performance.now = function() {
29+
return Date.now() - 99999.6394;
30+
};
31+
}
32+
2233
jQuery.now = Date.now;
2334
},
2435
teardown: function() {
2536
this.sandbox.restore();
2637
jQuery.now = Date.now;
2738
jQuery.fx.stop();
2839
jQuery.fx.interval = this._oldInterval;
40+
41+
// Support: IE <10, Safari <8.0, iOS <9, Android <4.4, Node with jsdom 9.4
42+
if ( window.performance && typeof window.performance.now === "function" ) {
43+
window.performance.now = this._oldPerformanceNow;
44+
}
45+
2946
window.requestAnimationFrame = oldRaf;
3047
return moduleTeardown.apply( this, arguments );
3148
}

0 commit comments

Comments
 (0)