Heim  >  Artikel  >  Web-Frontend  >  javascript动画效果类封装代码_javascript技巧

javascript动画效果类封装代码_javascript技巧

WBOY
WBOYOriginal
2016-05-16 19:09:38995Durchsuche







<script> <BR>function Animation(target,targetProperty,closure,precision) <BR>{ <BR> closure=closure||function(x){return x;}; <BR> precision=precision||10; <BR> this.handle; <br><br> var beginTime=new Date(); <BR> var stopTime=new Date(); <BR> this.Begin=function(){ <BR> beginTime=new Date(); <BR> this.handle=setInterval( <BR> function(){ <BR> var now=new Date(); <BR> target[targetProperty]=closure(now.getTime()-beginTime.getTime()); <BR> }, <BR> precision <BR> ); <BR> } <br><br> this.Continue=function(){ <BR> var now=new Date(); <BR> beginTime.setTime(now.getTime()-stopTime.getTime()+beginTime.getTime()); <br><br> this.handle=setInterval( <BR> function(){ <BR> var now=new Date(); <BR> target[targetProperty]=closure(now.getTime()-beginTime.getTime()); <BR> }, <BR> precision <BR> ); <BR> } <br><br> this.Stop=function(duration){ <br><br> clearInterval(this.handle); <br><br> if(duration===undefined) <BR> { <BR> stopTime=new Date(); <BR> duration=stopTime.getTime()-beginTime.getTime(); <BR> } <BR> else stopTime.setTime(beginTime.getTime()+duration) <BR> target[targetProperty]=closure(duration); <BR> } <BR>} <br><br>function StoryBoard(Duration,onfinish,flag) <BR>{ <BR> onfinish=onfinish||function(){}; <BR> var r=new Array(); <BR> r.appendAnimation=function(animation) <BR> { <BR> if(animation instanceof Animation) <BR> this.push(animation); <BR> } <BR> r.removeAnimation=function(animation) <BR> { <BR> for(var i=0;i<r.length;i++) <BR> { <BR> if(r[i]==animation) <BR> { <BR> r.splice(i,1); <BR> break; <BR> } <BR> } <BR> } <BR> r.start=function(){ <BR> for(var i=0;i<r.length;i++) <BR> { <BR> r[i].Begin(); <BR> } <BR> setTimeout( <BR> function(){ <BR> for(var i=0;i<r.length;i++) <BR> { <BR> r[i].Stop(Duration); <BR> } <BR> }, <BR> Duration <BR> ); <BR> onfinish(); <BR> } <BR> return r; <br><br>} <BR>//////////////////////////////下面是使用方法//////////////////////////////// <BR>function $(id) <BR>{ <BR> return document.getElementById(id); <BR>} <BR>var a1=new Animation($("output1"),"value"); <BR>var a2=new Animation($("output2"),"width",function(x){return Math.floor(x/10);}); <BR>var a3=new Animation($("output3").style,"filter",function(x){return "alpha(opacity="+Math.floor(x/5000*100)+")";}); <BR>var a4=new Animation($("output4").style,"width",function(x){return Math.floor(x*x*x/50000/5000)+"px";}); <BR>var s=new StoryBoard(5000); <BR>s.appendAnimation(a1); <BR>s.appendAnimation(a2); <BR>s.appendAnimation(a3); <BR>s.appendAnimation(a4); <BR>s.start(); <br><br></script>
 <br>output1是改变value <br>output1是改变宽度 <br>output3是淡入 <br>output4是带缓动的宽度 <br>

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn