Skip to content

Instantly share code, notes, and snippets.

@350d

350d/jquery.raf.js

Created Oct 29, 2014
Embed
What would you like to do?
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
You can’t perform that action at this time.