【HTML】完美运动框架

来源:互联网 发布:淘宝欢迎语大全 编辑:程序博客网 时间:2024/06/06 10:20

// 获取非行间样式
function getStyle(obj,styleName)
{
var speed=0;
if(obj.currentStyle){
return obj.currentStyle[styleName];
}else{
return getComputedStyle(obj,null)[styleName];
}
}

function move(obj,json,fn){       clearInterval(obj.timer);     obj.timer=setInterval(function()//开启定时器  {    var stop=true;  //注意一定要放入定时器内,否则stop赋值为false后死循环            for(var attr in json){      var now=0;  //获取当前值    if(attr=="opacity"){                   now=Math.round(parseFloat(getStyle(obj,attr)*100 ) );      }else{                     now=parseInt(getStyle(obj,attr) );       }                       speed=(json[attr]-now)/10;       speed=speed>0?Math.ceil(speed):Math.floor(speed);          if(attr=="opacity"){         obj.style[attr]=(now+speed)/100;         obj.filter="alpha(opacity="+(now+speed)+");";                  }else{         obj.style[attr]=now+speed+"px";        }      if(json[attr]!=now){        stop=false;      }    } //json 循环结束    if(stop){      clearInterval(obj.timer);    }  },30); // 定时器结束  if(fn) fn();} // move 结束
原创粉丝点击