RAF也便是requestAnimationFrame,以前的动画都是基于setTimeout写的,因此为了性能方面的考虑,开始使用requestAnimationFrame写动画。性能
function animation(obj, data) { if (obj.timer) { window.cancelAnimationFrame(obj.timer) } obj.timer = window.requestAnimationFrame(function () { for (var i in data) { var current=parseInt(obj.style[i]); var target=parseInt(data[i]); var speed=(target-current)/8;
speed=speed>0?Math.ceil(speed):Math.floor(speed); if(current!=target){ obj.style[i]=current+speed+"px"; obj.timer=window.requestAnimationFrame(arguments.callee); } } }) }