A small example of JavaScript jitter elements _javascript tips

Source: Internet
Author: User

Copy Code code as follows:

<!doctype html>
<meta charset= "Utf-8" >
<title>xxxxxx</title>
<style>

#control {
height:100px;
width:100%;
Background:gray;
}

</style>
<script>
Function Shake (E, oncomplete, distance, interval)
{
if (typeof e = = "string")
{
E = document.getElementById (e);
}//End If
Distance = Distance | | 8;
Interval = Interval | | 800;

var originalstyle = E.style.csstext;
E.style.position = "relative";
var start = (new Date ()). GetTime ();

Animate ();

function animate ()
{
var now = (new Date ()). GetTime ();
var elapsed = Now-start;
var progress = Elapsed/interval;
if (Progress < 1)
{
var y = distance * Math.sin (Math.PI * progress * 4);
var x = distance * Math.Cos (Math.PI * progress * 4);
E.style.left = x + "px";
E.style.top = y + "px";
Console.log (E.style.csstext);
settimeout (animate, Math.min (elapsed));
}//End If
Else
{
E.style.csstext = Originalstyle;
if (OnComplete)
{
OnComplete (e);
}//End If

}//End Else

}//End animate ()

}//End Shake ()

</script>
<body>

<div id= "Control" onclick= "Shake" (this); " >
</div>

</div>

</body>

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.