標準的js運動框架

function move(obj,attr,target,funEnd){
    clearInterval(obj.timer);
   obj.timer= setInterval(function(){
    var  currentAttr=0;
 對象,json,函數
function move(obj,json,funEnd){
clearInterval(obj.timer);//清除定時器
obj.timer= setInterval(function(){//同時開好幾個對象定時器
//聲明一個變量,看下JSON中的屬性點是已經達到==目標點是都已經完成
var flag=true;//設置一個初始值假設所有的屬性都已經執行了完了
for(var attr in json){//使用for 循環,引出json中的各個目標點
var currentAttr=0;//聲明一個變量,主要存儲原始點
if(attr=="opacity"){//若是 屬性==opacity,要另行考慮
//爲了確保獲得的數字是整數,因此使用Math.round,後面是轉義,*100是由於opacity的值爲小數
currentAttr=Math.round(parseFloat(getStyle(obj,attr))*100);
}
 
 

 

if(attr=="opacity"){
       currentAttr=Math.round(parseFloat(getStyle(obj,attr))*100);
     }
     
     else {
      currentAttr=parseInt(getStyle(obj,attr));

     }

    
     var speed=(target-currentAttr)/10;   
    

     speed=speed>0?Math.ceil(speed):Math.floor(speed);
      if(target==currentAttr){
        clearInterval(obj.timer);
        if(funEnd)funEnd();
        // if(funEnd){
        //   funEnd();
        // }
      }

     else {
       if(attr=="opacity"){
          // currentAttr+=speed;
           obj.style.opacity=(currentAttr+speed)/100;
          obj.style.filter='alpha(opacity:'+(currentAttr+speed)+')';
       }
       else {
         obj.style[attr]=currentAttr+speed+"px";

       }

    }
     },30)
 }

  
function getStyle(obj,name){

    if(obj.currentStyle){
      return obj.currentStyle[name];
    }
    else {
      return getComputedStyle(obj,false)[name];
    }
  }

    function getClass(oParent,className){
    var ele=oParent.getElementsByTagName("*");
    var arr=[];
       for(var i=0;i<ele.length;i++){
        if(ele[i].className==className){
          arr.push(ele[i]);
        }
       }
       return arr;  

  }
相關文章
相關標籤/搜索