javascript-运动框架

来源:互联网 发布:淘宝店铺名字大全女装 编辑:程序博客网 时间:2024/09/21 09:25
function getStyle(obj,attr){
     if(obj.currentStyle){
  return obj.currentStyle[attr];    
  }else{
  return getComputedStyle(obj,false)[attr];    
  }
 }
 function move(obj,json,fn){ 
 var sport=true;
   clearInterval(obj.timer);
 obj.timer=setInterval(function(){
  //取当前的值
  for(var attr in json){
  if(attr=="opacity"){
   var val=Math.round(parseFloat(getStyle(obj,attr))*100);     
  }else{
   var val=parseInt(getStyle(obj,attr));
  }
 //计算速度
  var speed=(json[attr]-val)/8;
   speed=speed>0?Math.ceil(speed):Math.floor(speed);
   //检测停止
    if(val!=json[attr]){             //检测是否所有运动都完成
       sport=false;
     }    
  if(attr=="opacity"){
       obj.style[attr]=(val+speed)/100;
    obj.style.filter="alpha(opacity="+(val+speed)+")";
  }else{
  obj.style[attr]=val+speed+"px";
 }
    if(sport){
     clearInterval(obj.timer);
     if(fn){
            fn();
     }
    }
 }

 },30)
 
 
 }
-----------------------------------------------------------------------
之前的版本
function getStyle(obj,attr){
     if(obj.currentStyle){
  return obj.currentStyle[attr];
 
  }else{
  return getComputedStyle(obj,false)[attr];
 
  }
 }
 
 function move(obj,attr,iTarget,fn){
   clearInterval(obj.timer);
 obj.timer=setInterval(function(){
  //取当前的值
  if(attr=="opacity"){
   var val=Math.round(parseFloat(getStyle(obj,attr))*100);
   
  }else{
   var val=parseInt(getStyle(obj,attr));
  }
 //计算速度
  var speed=(iTarget-val)/8;
   speed=speed>0?Math.ceil(speed):Math.floor(speed);
   //检测停止
 if(val==iTarget){
    clearInterval(obj.timer);
    if(fn){
  fn();
    }
 }else{
   
  if(attr=="opacity"){
       obj.style[attr]=(val+speed)/100;
    obj.style.filter="alpha(opacity="+(val+speed)+")";
 
 
  }else{obj.style[attr]=val+speed+"px";}
 
 }
 },30)
 
 
 }
 

 

0 0
原创粉丝点击