标准的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;  

  }
原文地址:https://www.cnblogs.com/host-hotel/p/8511124.html