首页 > web前端 > js教程 > 正文

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

WBOY
发布: 2016-05-16 19:09:38
原创
993 人浏览过







<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>
登录后复制

来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板