requestAnimationFrame是比setInterval更高效更平滑的动画实现。
兼容性查看:http://caniuse.mojijs.com/Home/Html/item/key/requestanimationframe/index.html
MDN文档:https://developer.mozilla.org/zh-CN/docs/Web/API/Window/requestAnimationFrame
原文链接:https://www.paulirish.com/2011/requestanimationframe-for-smart-animating/
1 // requestAnimationFrame实现浏览器兼容的代码
2 window.requestAnimFrame = (function(){
3 return window.requestAnimationFrame ||
4 window.webkitRequestAnimationFrame ||
5 window.mozRequestAnimationFrame ||
6 function( callback ){
7 window.setTimeout(callback, 1000 / 60);
8 };
9 })();
10
11
12 // 使用方法:
13 // 代替 setInterval(render, 16) ....
14
15 (function animloop(){
16 requestAnimFrame(animloop);
17 render();
18 })();