How to shake (or vide) a html element<!-- --> | <!-- -->Patrick Desjardins Blog
Patrick Desjardins Blog
Patrick Desjardins picture from a conference

How to shake (or vide) a html element

Posted on: April 5, 2013

When your user does an error, it might be interesting to select the control and to make it shakes few seconds just to get the user attention. It's not something you should do for every validation but some time, you may cross a situation where the user might be more confuse about which controls or elements he needs to take care of. Therefore, a way to do it is to shake the element on the screen and highlight this one.

The code in this post is not into the form of JQuery plugin but it should be. It's more indented to be educational. The first part of the code show you that you need to call the method "Vibrate" that takes the element to shake as parameter.

1var vibrateIndex = 0;
2var selector; //Animate the selector with the animation function
3function Vibrate(item) {
4 selector = $(item);
5 var interval = 10;
6 var duration = 500;
7 vibrateIndex = setInterval(Vibe, interval);
8 setTimeout(StopVibration, duration);

The second part is doing the animation of shaking the control. In fact, we are randomly moving the left and top position of the control and setting this one relative. This is important. Otherwise, the control won't move. It also set the border to red.

2function Vibe() {
3 var shake = 3;
4 $(selector)
5 .stop(true, false)
6 .css({
7 position: "relative",
8 left: Math.round(Math.random() * shake) + "px",
9 top: Math.round(Math.random() * shake) + "px",
10 borderColor: "red",
11 });

The last part is executed when the animation is over. This is called by the setTimout method previously set with a duration.

1//Remove all CSS applied to the selector
2function StopVibration() {
3 clearInterval(vibrateIndex);
4 $(selector).stop(true, false).css({
5 position: "static",
6 left: "0px",
7 top: "0px",
8 borderColor: "",
9 });

This could be improved by having everything inside a JQuery plugin which would be cleaner. The problem with the code above is that we use global variable to remember to control that is implicated into the animation and the interval reference is also global.

You can see a working example here.


Here is the JQuery Plugin version:

1(function ($) {
2 $.fn.vibrate = function (options) {
3 options = $.extend({}, $.fn.vibrate.defaultOptions, options);
4 var control = $(this);
5 var vibrateIndex = 0;
6 var selector;
7 vibrateIndex = setInterval(vibe, options.interval);
8 setTimeout(stopVibration, options.duration);
9 function vibe() {
10 var shake = 3;
11 $(control)
12 .stop(true, false)
13 .css({
14 position: "relative",
15 left: Math.round(Math.random() * shake) + "px",
16 top: Math.round(Math.random() * shake) + "px",
17 borderColor: "red",
18 });
19 }
21 function stopVibration() {
22 clearInterval(vibrateIndex);
23 $(control)
24 .stop(true, false)
25 .css({ position: "static", left: "0px", top: "0px", borderColor: "" });
26 }
28 return this;
29 };
31 $.fn.vibrate.defaultOptions = { interval: 10, duration: 500 };

Here is an example of how to use it :

1$("#btnShake").click(function () {
2 $("input[type=text]").vibrate();

You can also specify option like the duration:

1$("input[type=text]").vibrate({ duration: 3000 });

I'll do a post concerning how to create a JQuery plugin soon to explain in detail how and why the plugin has been created this way. For the moment, you can refer to this Microsoft's tutorial which is very fine.