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); } } }) }