一个简单的js动画函数封装
fwxstar 2018-06-30 680次浏览
摘要:
项目预览地址:http://jsrun.net/h2gKp/editvar btn = $("btn");
var btn1 = $...
项目预览地址:http://jsrun.net/h2gKp/edit
var btn = $("btn"); var btn1 = $("btn1"); var btn0 = $("btn0"); var zi= $("zi"); function doMove(ele,long,attr,last,endFun){ clearInterval( ele.timers ); long = parseInt(getStyle(ele,attr)) < last ? long :-long; ele.timers = setInterval(function(){ var speed= parseInt(getStyle(ele,attr)) + long ; ele.style[attr]= speed +'px'; if(speed > last && long > 0 || speed <= last && long < 0){ speed = last; } ele.style[attr] = speed + 'px'; if(speed == last){ clearInterval( zi.timers ); endFun && endFun(); } },100); } btn.onclick= function () { doMove(zi,50,"left",800, function () { doMove(zi,20,"height",400) }) }; btn1.onclick= function () { doMove(zi,50,"left",10); }; btn0.onclick= function () { clearInterval( zi.timers ); }