Jquery里的animate方法,用JS怎么写

2025-05-12 03:33:26
推荐回答(3个)
回答1:

可以使用递归

//js的animate方法
//参数:obj当前元素
//      arr属性名称
//      目标属性值
//      fn方法参数        
function run(obj,arr,target,fn){
    clearInterval(obj.timer);
    obj.timer = setInterval(function(){
      var cur = 0;
        if(attr == "opacity"){
          cur = Math.round(parseFloat(getstyle(obj,attr))*100);
        }else{
          cur = parseInt(getstyle(obj,attr));
        }
      var speed = (target-cur)/8;
        speed = speed>0?Math.ceil(speed):Math.floor(speed);
        
        if(cur == target){
          clearInterval(obj.timer);
            if(fn){
              fn();
            }
        }else{
          if(attr == "opacity"){
                obj.style.filter = "alpha(opacity="+(cur+speed)+")";
              obj.style.opacity = (cur+speed)/100;
            }else{
            obj.style[attr] = cur + speed + "px";
            }
        }
        
    },30)
}

//获取样式
function getstyle(obj,name){
  if(obj.currentStyle){
      return obj.currentStyle[name];
    }else{
      return getComputedStyle(obj,false)[name];
    }
}

//调用示例:
 run(box,"width",200,function(){run(box,"height",200,null);});

回答2:

js是jquery的父亲
js是一种网页语言
而jquery并不是一种语言,他是一种语言的产物
说白了,就是js封装的类

回答3:

看jquery代码啊。代码不就是javascript实现的么。。。。
!!!!!!!!!!!!!!!!!!!!