javascript動(dòng)畫效果類封裝代碼
更新時(shí)間:2007年08月28日 20:22:10 作者:
<input id=output3 style="position:absolute;top:300;left:300;"/>
<input id=output1 />
<input id=output2 />
<br/>
<input id=output4 />
<script>
function Animation(target,targetProperty,closure,precision)
{
closure=closure||function(x){return x;};
precision=precision||10;
this.handle;
var beginTime=new Date();
var stopTime=new Date();
this.Begin=function(){
beginTime=new Date();
this.handle=setInterval(
function(){
var now=new Date();
target[targetProperty]=closure(now.getTime()-beginTime.getTime());
},
precision
);
}
this.Continue=function(){
var now=new Date();
beginTime.setTime(now.getTime()-stopTime.getTime()+beginTime.getTime());
this.handle=setInterval(
function(){
var now=new Date();
target[targetProperty]=closure(now.getTime()-beginTime.getTime());
},
precision
);
}
this.Stop=function(duration){
clearInterval(this.handle);
if(duration===undefined)
{
stopTime=new Date();
duration=stopTime.getTime()-beginTime.getTime();
}
else stopTime.setTime(beginTime.getTime()+duration)
target[targetProperty]=closure(duration);
}
}
function StoryBoard(Duration,onfinish,flag)
{
onfinish=onfinish||function(){};
var r=new Array();
r.appendAnimation=function(animation)
{
if(animation instanceof Animation)
this.push(animation);
}
r.removeAnimation=function(animation)
{
for(var i=0;i<r.length;i++)
{
if(r[i]==animation)
{
r.splice(i,1);
break;
}
}
}
r.start=function(){
for(var i=0;i<r.length;i++)
{
r[i].Begin();
}
setTimeout(
function(){
for(var i=0;i<r.length;i++)
{
r[i].Stop(Duration);
}
},
Duration
);
onfinish();
}
return r;
}
//////////////////////////////下面是使用方法////////////////////////////////
function $(id)
{
return document.getElementById(id);
}
var a1=new Animation($("output1"),"value");
var a2=new Animation($("output2"),"width",function(x){return Math.floor(x/10);});
var a3=new Animation($("output3").style,"filter",function(x){return "alpha(opacity="+Math.floor(x/5000*100)+")";});
var a4=new Animation($("output4").style,"width",function(x){return Math.floor(x*x*x/50000/5000)+"px";});
var s=new StoryBoard(5000);
s.appendAnimation(a1);
s.appendAnimation(a2);
s.appendAnimation(a3);
s.appendAnimation(a4);
s.start();
</script>
<pre>
output1是改變value
output1是改變寬度
output3是淡入
output4是帶緩動(dòng)的寬度
</pre>
<input id=output1 />
<input id=output2 />
<br/>
<input id=output4 />
<script>
function Animation(target,targetProperty,closure,precision)
{
closure=closure||function(x){return x;};
precision=precision||10;
this.handle;
var beginTime=new Date();
var stopTime=new Date();
this.Begin=function(){
beginTime=new Date();
this.handle=setInterval(
function(){
var now=new Date();
target[targetProperty]=closure(now.getTime()-beginTime.getTime());
},
precision
);
}
this.Continue=function(){
var now=new Date();
beginTime.setTime(now.getTime()-stopTime.getTime()+beginTime.getTime());
this.handle=setInterval(
function(){
var now=new Date();
target[targetProperty]=closure(now.getTime()-beginTime.getTime());
},
precision
);
}
this.Stop=function(duration){
clearInterval(this.handle);
if(duration===undefined)
{
stopTime=new Date();
duration=stopTime.getTime()-beginTime.getTime();
}
else stopTime.setTime(beginTime.getTime()+duration)
target[targetProperty]=closure(duration);
}
}
function StoryBoard(Duration,onfinish,flag)
{
onfinish=onfinish||function(){};
var r=new Array();
r.appendAnimation=function(animation)
{
if(animation instanceof Animation)
this.push(animation);
}
r.removeAnimation=function(animation)
{
for(var i=0;i<r.length;i++)
{
if(r[i]==animation)
{
r.splice(i,1);
break;
}
}
}
r.start=function(){
for(var i=0;i<r.length;i++)
{
r[i].Begin();
}
setTimeout(
function(){
for(var i=0;i<r.length;i++)
{
r[i].Stop(Duration);
}
},
Duration
);
onfinish();
}
return r;
}
//////////////////////////////下面是使用方法////////////////////////////////
function $(id)
{
return document.getElementById(id);
}
var a1=new Animation($("output1"),"value");
var a2=new Animation($("output2"),"width",function(x){return Math.floor(x/10);});
var a3=new Animation($("output3").style,"filter",function(x){return "alpha(opacity="+Math.floor(x/5000*100)+")";});
var a4=new Animation($("output4").style,"width",function(x){return Math.floor(x*x*x/50000/5000)+"px";});
var s=new StoryBoard(5000);
s.appendAnimation(a1);
s.appendAnimation(a2);
s.appendAnimation(a3);
s.appendAnimation(a4);
s.start();
</script>
<pre>
output1是改變value
output1是改變寬度
output3是淡入
output4是帶緩動(dòng)的寬度
</pre>
相關(guān)文章
three.js中點(diǎn)對象(Point)和點(diǎn)材質(zhì)(PointsMaterial)的具體使用
本文主要介紹了three.js中點(diǎn)對象(Point)和點(diǎn)材質(zhì)(PointsMaterial)的具體使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07JavaScript變量類型以及變量之間的轉(zhuǎn)換你了解嗎
這篇文章主要為大家詳細(xì)介紹了JavaScript變量類型以及變量之間的轉(zhuǎn)換,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2022-02-02微信JS SDK接入的幾點(diǎn)注意事項(xiàng)(必看篇)
下面小編就為大家?guī)硪黄⑿臞S SDK接入的幾點(diǎn)注意事項(xiàng)(必看篇)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-06-06es6學(xué)習(xí)筆記之Async函數(shù)的使用示例
async 函數(shù),使得異步操作變得更加方便。它是 Generator 函數(shù)的語法糖。下面這篇文章主要給大家介紹了es6學(xué)習(xí)筆記之Async函數(shù)使用的相關(guān)資料,文中給出了詳細(xì)的示例代碼,需要的朋友們下面來一起看看吧。2017-05-05js實(shí)現(xiàn)移動(dòng)端tab切換時(shí)下劃線滑動(dòng)效果
這篇文章主要為大家詳細(xì)介紹了js實(shí)現(xiàn)移動(dòng)端tab切換時(shí)下劃線滑動(dòng)效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-09-09JavaScript設(shè)計(jì)模式之代理模式實(shí)例分析
這篇文章主要介紹了JavaScript設(shè)計(jì)模式之代理模式,簡單描述了代理模式的概念、原理并結(jié)合實(shí)例形式分析了javascript代理模式的相關(guān)實(shí)現(xiàn)與使用技巧,需要的朋友可以參考下2019-01-01js實(shí)現(xiàn)簡單的點(diǎn)名器隨機(jī)色實(shí)例代碼
這篇文章主要給大家介紹了關(guān)于js實(shí)現(xiàn)簡單的點(diǎn)名器隨機(jī)色的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09