【发布时间】:2014-01-17 03:31:13
【问题描述】:
Paul Irish 有一个名为 requestAnimationFrame for Smart Animating 的帖子。现在保罗是个聪明人——我只是想了解这个想法的应用范围。
他说要做 HTML5 动画 - 你应该像这样使用 requestAnimationFrame shim:
// shim layer with setTimeout fallback
window.requestAnimFrame = (function(){
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
function( callback ){
window.setTimeout(callback, 1000 / 60);
};
})();
// usage:
// instead of setInterval(render, 16) ....
(function animloop(){
requestAnimFrame(animloop);
render();
})();
// place the rAF *before* the render() to assure as close to
// 60fps with the setTimeout fallback.
这是可以理解的。 We can apply this in a JSFiddle like this 结果相当不错。
但是if I rip out the shim layer function,那么在 Chrome 31 和 Firefox 24 中它可以正常工作。
所以如果我查看compatibility for the RequestAnimationFrame function - 看起来浏览器已经拥有这个功能很长时间了。
我的问题是 - 我们为哪些浏览器使用 Paul Irish 的 requestAnimationFrame shim? 你可以把它撕下来,它仍然可以工作,而且看起来浏览器已经使用了很长时间。
【问题讨论】:
-
对于您刚刚链接到的列表中的那些。特别是对于 IE9。
-
切勿使用提供的垫片。它只是延迟了 1000/60 的执行。 raf 的唯一目的是避免过多的 dom 更新。最好使用真正的 raf polyfill,它会为下一帧执行安排回调。
标签: javascript html requestanimationframe