Skip to content

Instantly share code, notes, and snippets.

@350d
Created October 29, 2014 13:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 350d/734033841beaac5c5d92 to your computer and use it in GitHub Desktop.
Save 350d/734033841beaac5c5d92 to your computer and use it in GitHub Desktop.
jQuery RequestAnimationFrame Plugin
(function(jQuery){
'use strict';
if (!Date.now) Date.now = function() { return new Date().getTime(); };
var animating,
vendors = ['moz', 'webkit'],
W = window;
for (var i = 0; i < vendors.length && !W.requestAnimationFrame; ++i) {
var vp = vendors[i];
W.requestAnimationFrame = W[vp+'RequestAnimationFrame'];
W.cancelAnimationFrame = (W[vp+'CancelAnimationFrame'] || W[vp+'CancelRequestAnimationFrame']);
}
function raf() {
if (animating) {
requestAnimationFrame(raf);
jQuery.fx.tick();
}
}
if (!/iP(ad|hone|od).*OS 6/.test(W.navigator.userAgent) && W.requestAnimationFrame && W.cancelAnimationFrame ) {
jQuery.fx.timer = function(timer) {
if (timer() && jQuery.timers.push(timer) && !animating) {
animating = true;
raf();
}
};
jQuery.fx.stop = function() {
animating = false;
};
} else {
var lastTime = 0;
W.requestAnimationFrame = function(callback) {
var now = Date.now(),
nextTime = Math.max(lastTime + 16, now);
return setTimeout(function() { callback(lastTime = nextTime); }, nextTime - now);
};
W.cancelAnimationFrame = clearTimeout;
}
}(jQuery));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment