animate用法 js原生_js原生复杂实现animate操作Css3属性(升级版!)

本文介绍了一种使用JavaScript原生方法实现animate动画效果的方法,可以直接操作CSS2和CSS3属性。通过提供的animate函数,可以实现对象的平滑过渡,包括宽度、高度、旋转等变化。示例代码展示了如何为一个红色方块依次改变宽度、高度、旋转角度,并在动画结束后触发回调函数。
摘要由CSDN通过智能技术生成
sadsadsadsadsadsadsa

#box{width:200px;height:200px;background:red;

}

/**

* [animate 动画效果(可操作CSS2和CSS3)直接书写CSS3动画效果就可直接识别]

* @param {[type]} obj [要操作的对象]

* @param {[type]} json [属性]

* @param {[type]} speed [时间]

* @param {Function} fn [回调]

* @return {[type]} [description]*/

functionanimate(obj ,json ,speed ,fn) {varstartTime= newDate();variCur= 0;variSpeed= 0;varCss3=[];vari= 0;varpx= '';

clearInterval(obj.timer);

obj.timer=setInterval(function() {variStop= true;

i++;

document.title=i;for(varnameinjson) {variTarget=parseInt(json[name].match(/([0-9])+/g,""));

iCur=parseInt(obj.style[name].match(/([0-9])+/g,""))||parseInt(css(obj ,name).match(/([0-9])+/g,""))|| 0;varpercentage=Math.min(1, (newDate-startTime)/speed);if(!inArray(name ,Css3)) {

iSpeed=( iTarget-iCur )/ 8;

iSpeed=iSpeed> 0 ?Math.ceil(iSpeed) : Math.floor(iSpeed);

iSpeed=(iCur+(iTarget-iCur)*percentage);

iValue=iSpeed+css(obj ,name).match(/([a-z])+/g,"");

}else{

}if(iCur!=iTarget&&json[name].match(/([a-z])+\((.)+\)/g,"")) {

iStop= false;

iValue=json[name].replace(/([0-9])+/, iSpeed)

}if(iCur!=iTarget) {

iStop= false;

obj.style[name]=iValue;

}

}if(iStop) {

console.log(newDate-startTime);

clearInterval(obj.timer);

fn&&fn.call(obj);

}

} ,13);

}functioninArray(b,a){for(vari= 0; i<=a.length- 1; i++) {if(a[i]==b){return true;

}return false;

};

}functioncss(obj, attr) {if(obj.currentStyle) {returnobj.currentStyle[attr];

}else{returngetComputedStyle(obj ,false)[attr];

}

}

window.οnlοad= function() {

box.οnclick= function() {

animate(box ,{'width':'400px','height':'600px'} ,1000,function() {

animate(this,{'transform':'rotate(180deg)'},1000,function() {

animate(this,{'width':'100px'},1000,function() {

animate(this,{'height':'100px'},1000,function() {

alert(1)

})

})

})

})

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值