兼容各个浏览器的动画循环代码

浏览:1594 发布日期:2013/08/15 分类:功能实现 关键字: html5
兼容各个浏览器的动画循环代码。使用html5的requestAnimationframe API
window.requestNextAnimationframe =
(function () {
var originalWebkitRequestAnimationframe = undefined,
wrapper = undefined,
callback = undefined,
geckoVersion = 0,
userAgent = navigator.userAgent,
index = 0,
self = this;

// Workaround for Chrome 10 bug where Chrome
// does not pass the time to the animation function

if (window.webkitRequestAnimationframe) {
// Define the wrapper

wrapper = function (time) {
if (time === undefined) {
time = +new Date();
}
self.callback(time);
};

// Make the switch

originalWebkitRequestAnimationframe = window.webkitRequestAnimationframe;

window.webkitRequestAnimationframe = function (callback, element) {
self.callback = callback;

// Browser calls the wrapper and wrapper calls the callback

originalWebkitRequestAnimationframe(wrapper, element);
}
}

// Workaround for Gecko 2.0, which has a bug in
// mozRequestAnimationframe() that restricts animations
// to 30-40 fps.

if (window.mozRequestAnimationframe) {
// Check the Gecko version. Gecko is used by browsers
// other than Firefox. Gecko 2.0 corresponds to
// Firefox 4.0.

index = userAgent.indexOf('rv:');

if (userAgent.indexOf('Gecko') != -1) {
geckoVersion = userAgent.substr(index + 3, 3);

if (geckoVersion === '2.0') {
// Forces the return statement to fall through
// to the setTimeout() function.

window.mozRequestAnimationframe = undefined;
}
}
}

return window.requestAnimationframe ||
window.webkitRequestAnimationframe ||
window.mozRequestAnimationframe ||
window.oRequestAnimationframe ||
window.msRequestAnimationframe ||

function (callback, element) {
var start,
finish;

window.setTimeout( function () {
start = +new Date();
callback(start);
finish = +new Date();

self.timeout = 1000 / 60 - (finish - start);

}, self.timeout);
};
}
)
();
评论( 相关
后面还有条评论,点击查看>>