運動函數封裝(js)

// 運動函數css

function starMove(obj,json,fnEnd){json

  clearInterval(obj.timer);函數

  obj.timer  = setInterval(function(){ci

    var bStop = true;get

    for(var name in json){it

      var iTarget = json[name]; // 目標點io

      if(name = 'opacity'){function

          var cur = parseInt(parseFloat(getStyle(obj,name))*100);im

      }else{樣式

          var cur = parseInt(getStyle(obj,name));

      }

 

      var speed = (iTarget-cur)/8;

      speed = speed>0?Math.ceil(speed):Math.floor(speed);// 每30毫秒運動的距離,數值越小運動越慢

      if(name = 'opacity'){

          obj.style.opacity = (cur+speed)/100;

      }else{

          obj.style[name]= cur+speed+'px';

      }

      if(cur != iTarget){

        bStop = false;

      }

    }

    if(bStop){

      clearInterval(obj.timer);

      if(fnEnd){

        fnEnd();

      }

    }

  },30);

}

 

// 獲取css樣式

function getStyle(obj,attr){

  if(obj.currentStyle){

    return obj.currentStyle[attr];

  }else{

    return obj.getComputedStyle(obj)[attr];

  }

}

相關文章
相關標籤/搜索