多物体运动JavaScript函数封装

function startMove(obj, json, func){
  clearInterval(obj.timer);
  obj.timer = setInterval(function(){

    var bStop = true;

    //取出该属性的初值
    for(var attr in json){
      var iCur = 0;
      if(attr == "opacity"){
        iCur = parseFloat(getStyle(obj, attr)) * 100;
      }else{
        iCur = parseInt(getStyle(obj, attr))
      }
      var speed = (json[attr] - iCur) / 8;  //8 缩放系数,缓冲运动效果最好的缩放系数
      speed = speed > 0 ? Math.ceil(speed) : Math.floor(speed);

      //只要有一个值未达到指定值程序就不能继续执行
      if(iCur != json[attr]){
        bStop = false;
      }
      if(attr == "opacity"){
        obj.style.filter = "alpha(opacity: " + (iCur + speed) + ")";
        obj.style.opacity = (iCur + speed) / 100;
      }else{
        obj.style[attr] = iCur + speed + "px";
      }
    }
    if(bStop){
      clearInterval(obj.timer);
      if(func){
        func();
      }
    }
  }, 30);
}

//获取非行间样式
function getStyle(obj, attr){
  if(obj.currentStyle){
    return obj.currentStyle[attr];  //IE9 以下
  }else{
    return getComputedStyle(obj)[attr];  //IE9 及标准浏览器
  }
}

posted @ 2017-10-28 23:21  paul0705  阅读(130)  评论(0编辑  收藏  举报