首页 > 其他 > 详细

基于RAF的一个小动画框

时间:2016-05-21 01:15:44      阅读:298      评论:0      收藏:0      [点我收藏+]

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

 

基于RAF的一个小动画框

原文:http://www.cnblogs.com/jelly7723/p/5513782.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!