Shake Things Up Using jQuery UI’s Shake Effect

By  on  

Yesterday I created a tutorial showing you how you can shake an element using Fx.Shake, a MooTools component written by Aaron Newton. It turns out that jQuery UI also has a shake effect which can draw attention to an element.

The XHTML

<div id="shaker">
<a href="https://davidwalsh.name"><img src="/demo/mootools-shake.png" alt="David Walsh Blog" /></a>
</div>

Exactly the same as my MooTools method.

The CSS

#shaker		{ position:fixed; top:10px; right:10px; width:299px; height:253px; display:none; }

Also exactly the same as my MooTools method.

The jQuery JavaScript

function periodical() {
	$('#shaker').effect('shake', { times:3 }, 200);
}
$(document).ready(function() {
	$('#shaker').hide().css('display','').fadeIn(600);
	var shake = setInterval(periodical, 5000);
	
	/* updated: click clear */
	$('#shaker').click(function() {
		clearInterval(shake);
	})	
});

The code is basically the exact same logic ported into jQuery's syntax and methodology. Remember that you need jQuery UI to do this.

What I loved about creating these tutorial was that these are perfect examples of how flexible these two libraries are. They both allowed me to create the same functionality in very little code. People shouldn't focus on "jQuery against MooTools," people should focus on "jQuery or MooTools."

Update: Click Clear

I added clearing the interval (and thus stop the shaking) once the user has clicked on the element.

Recent Features

  • By
    Facebook Open Graph META Tags

    It's no secret that Facebook has become a major traffic driver for all types of websites.  Nowadays even large corporations steer consumers toward their Facebook pages instead of the corporate websites directly.  And of course there are Facebook "Like" and "Recommend" widgets on every website.  One...

  • By
    Camera and Video Control with HTML5

    Client-side APIs on mobile and desktop devices are quickly providing the same APIs.  Of course our mobile devices got access to some of these APIs first, but those APIs are slowly making their way to the desktop.  One of those APIs is the getUserMedia API...

Incredible Demos

  • By
    MooTools TwitterGitter Plugin

    Everyone loves Twitter. Everyone loves MooTools. That's why everyone should love TwitterGitter, a MooTools plugin that retrieves a user's recent tweets and allows the user to format them however the user would like. TwitterGitter allows the user to choose the number of...

  • By
    Dynamically Create Charts Using MooTools MilkChart and Google Analytics

    The prospect of creating graphics charts with JavaScript is exciting. It's also the perfect use of JavaScript -- creating non-essential features with unobtrusive scripting. I've created a mix of PHP (the Analytics class), HTML, and MooTools JavaScript that will connect to Google Analytics...

Discussion

  1. nice work, but in the demo instead of just shaking its moving horizontally to the left side of the page shakes and then goes back to write side of the page, i am using Safari.

    Good work though :)

  2. I get the same result Mohamed gets. The actual shaking happens in the left corner in Safari 4.0

  3. Whoa…Safari is acting weird. I’ll check that out a bit later. I’m guessing the absolute positioning used in the jQuery plugin is messing with it. I may need to create a container and make that relative.

  4. Don’t forget a return: false; for the click if you don’t actually want the user to be redirected anywhere (or for that silly # sign to show up) :-)

    Great quick tip, David. I’m sure a use for this will pop up for myself sometime.

  5. Jasminka

    Very nice effect;-)…Hello David! First of all I would like to thank you very much for all yours tutorials that you are kindly sharing ..they are very actual and helpful for novice like me..:-))… Thanks to you I found many interesting information..like Jquery.. I began to study..I would like to ask you as a professional.. if I will use for example Jquery plug-ins in my website project.. can it be attacked?.. as for example sql injection ? Thank you in advance.. ;-)

  6. I just saw that my favorite CMS (Umbraco) had implemented it on the admin login form. I couldn’t figure out how they were doing it but I saw your posting on using Jquery Shake and sure enough they’re using the same code.

    Thanks for the lesson and all your other tips!

  7. now i can clone the mac login box error to my web forms.. :D

    thanks

  8. Delphi

    It seems to be acting up in Safari and Chrome. I tried the relative container and that didn’t seem to work. Did you ever figure this issue out?

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!