//主要是区分浏览器的不同,如果浏览器不支持requestNextAnimationFrame()方法,只能使用setTimeout()来代替。这种封装方式称为polyfill method,多平台智能适配方法,主要思想是利用不同平台的优缺点,当不满足时再用替代方法;个合集思想,而不是交集思想。
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) {//chrome10版本的BUG,现在应该已经修复了,此值一直是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 ||//w3c对象
window.webkitRequestAnimationFrame ||//chrome对象
window.mozRequestAnimationFrame ||//firefox对象
window.oRequestAnimationFrame ||
window.msRequestAnimationFrame ||//window对象
function (callback, element) {//为了解决firefox 4.0版本帧速率过慢的bug,在后续版本中已改正
var start,
finish;
window.setTimeout( function () {
start = +new Date();
callback(start);
finish = +new Date();
self.timeout = 1000 / 60 - (finish - start);
}, self.timeout);
};
}
)
();requestAnimationFrame.js---封装动画Frame51
原创
©著作权归作者所有:来自51CTO博客作者生而为人我很遗憾的原创作品,请联系作者获取转载授权,否则将追究法律责任
上一篇:Gradle构建工具中文教程
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
three.js实践-一个自定义几何体拆合动画制作
three.js实践-一个自定义几何体拆合动画制作将多个三角形面片组合一个面,多个面组合一个拆合为几何体的简易动画。
自定义几何体拆合动画 three.js gsap -
性能更好的js动画实现方式——requestAnimationFrame
等等。所以有的
javascript css3 requestAnimationFram 重绘 API -
原生JS封装动画
用过jQuery的都知道,jQuery中有提供了一个动画方法,那就是animate方法。这个函数的关键在于指定动画形式及结果样式属性对象。
动画 css 封装 jquery 其他 -
js 动画滚动到指定位置 requestAnimationFrame.-- react
ent) => () => { const { scrollTop, scrollHeight, clientHe
javascript reactjs Math ico -
Android frame by frame animation动画显示
在看到编写简单的动画的时候,想到了android上也可以做到这一点,只是几个图片
移动开发 java android xml 帧动画 -
requestAnimationFrame实现浏览器动画
告诉浏览器——你希望执行一个动画,并且要求浏览器在下次重绘之前调用指定的回调函数更新动画
动画 javascript 前端 回调函数 重绘
















