//封装函数,获取样式,解决offset全部属性下的BUGfunction getStyle(obj,attr){ if(obj.currentStyle){ return obj.currentStyle[attr]; }else{ return getComputedStyle(obj,false)[attr]; }}//封装动画框架function startMove(obj,json,fn){ //(对象,改变的属性,目标值,回调函数) clearInterval(obj.timer); obj.timer = setInterval(function(){ for(var attr in json){ //JSON实现同时运动 //取到当前值 var icur = 0; var flag = true;//假设 if(attr == "opacity"){ icur = Math.round(parseFloat(getStyle(obj,attr))*100);//处理透明度 }else{ var icur = parseInt(getStyle(obj,attr)); } //计算速度 var speed = (json[attr] - icur)/10; speed = speed>0?Math.ceil(speed):Math.floor(speed); //是否所有的运动都达到目标值 if(json[attr]!=icur){ flag = false; } if(attr == "opacity"){ icur += speed; obj.style.opacity = icur/100; obj.style.filter = 'alpha(opacity:'+icur+')'; }else{ obj.style[attr] = icur + speed +"px"; } } if(flag){ clearInterval(obj.timer); if(fn){ fn(); } } },50)}
添加回答
举报
0/150
提交
取消