JavaScript animation effect class encapsulation code _javascript Tips

Source: Internet
Author: User
Tags closure setinterval
<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;

}
Here's how to use////////////////////////////////
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 is changing value
OUTPUT1 is changing the width
OUTPUT3 is fading in
Output4 is the width with ease
</pre>
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.