Earthquake Effect: Shake the browser using JavaScript

Note: This post was imported from the very old Twod.co.uk post archives.

This snippet of code will produce a JavaScript function which you can then attach via a JavaScript call and shake the user’s Internet browser.

Place this anywhere before you’re going to call the function, or remove the surrounding <script> tags and place it within an external JavaScript file.

<script type=”text/javascript”>
function shake(n) {
if (parent.moveBy) {
for (i = 10; i > 0; i–) {
for (j = n; j > 0; j–) {
parent.moveBy(0,i);
parent.moveBy(i,0);
parent.moveBy(0,-i);
parent.moveBy(-i,0);
}
}
}
}
</script>

An example of this function being called using the onclick method would look as follows:

<input type=”button” onclick=”javascript:shake(200)” value=”Shake Me!” />

This will produce (go ahead, click for an example of the effect):

Share this with friends:

Like what you've just read? Share it to your friends using your favourite service below:

Share on Twitter Share on Facebook Share on Stumble Upon Share on Google Share on MySpace Share on Delicious Share on Digg Share on Google Buzz Share via Reddit Share via E-Mail Share via Wordpress.com Share via Tumblr Share via Posterous Share via Newsvine

Comments:

  1. Clearly not written by a developer, Instead of all the repeated lines, you can just replace them with one occurrence of the following lines:

    parent.moveBy(0,i);
    parent.moveBy(i,0);
    parent.moveBy(0,-i);
    parent.moveBy(-i,0);

    and then just use a larger “n” parameter when calling the function. Otherwise, it works well, but just be sure to use this on a supported browser.

Add a Comment: