This effect can be used to validate certain criteria in your form. Other than using highlighter to highlight the error area, we can use a more creativity approach by vibrating that particular input box to alert the user. This tutorial demonstrate a very easy way to understanding how vibration works on the web and how it can be used in your web design.
The Concept
In order to perform a vibration effect, we must first understand how will a person feel during an earthquake. The whole world will be shaking right? In a more defined term, it means that the object are moving on it own, not us. Similarly, we are staring at the monitor while the boxes are moving on its own! Next question, how do you move? The answer to my question will be the same answer on how the boxes move but not by legs. Rather, the position will be moved. In conclusion, we will try to move the position left and right, up and down to simulate vibration.
The Code
Coding part will be split into 2 part. The structural and jQuery part. The CSS part is fairly simply to understand since we are just aligning them to the center and that's it.
HTML
The following will be the only thing we need in here.
<div id="frame"> <div id="container"> <div class="box" id="box1"> <p>Username<input type='text' size='20'></p> <p>Password<input type='text' size='20'></p> <p><input type='button' value='Submit' id='activate'/></p> </div> </div> </div>
We will only use the 'frame' and 'activate' ID in this tutorial. You can safely ignore all other structure as it will not affect the vibration. The reason is we are shaking the most outer box which is 'frame' and an event handler is attached on 'activate'.
jQuery
The confusing part may be the jQuery code. But we will only need to know certain variable in order for this to work. The variables are,
var interval = 30; var duration= 1000; var shake= 3; var vibrateIndex = 0; var selector = $('#frame'); var stopVibration; var vibrate;
Let me explain the variable shown above in a well indented manner,
- interval: this is used to set the duration for each movement. eg, every 30 millisecond it will move left,right,top or bottom once.
- duration: this is used to set the duration for the whole effect. eg, it will vibrate for 1000 millisecond
- selector: this is the jQuery selector we are going to apply the vibrate with
- stopVibration: this is the method that will stop the vibration after 'duration' variable
- vibrate: this is the method that will start the vibration
- vibrateIndex: this is the index that setInterval provides when used to tell 'stopVibration' which vibration to stop
It should be easy to understand the variables needed for this effect to accomplished. Now for the 'stopVibration' and 'vibrate' method explanation:
var vibrate = function(){ $(selector).stop(true,false) .css({position: 'relative', left: Math.round(Math.random() * shake) - ((shake + 1) / 2) +'px', top: Math.round(Math.random() * shake) - ((shake + 1) / 2) +'px'}); }
Let's start with 'vibrate'. The above code will take the selector and stop whatever animate it is performing and set the box position as relative so it will move starting from his original position. We set the left and top randomly with a mathematics calculation. So it will move itself left or top randomly. (change the equation to make it vibrate differently)
var stopVibration = function() { clearInterval(vibrateIndex); $(selector).stop(true,false) .css({position: 'static', left: '0px', top: '0px'}); };
'stopVibration' will stop the interval with the index given and return the box to its original position. That's it!
Lastly, we will need to add an event handler to the button for it to vibrate!
$('#activate').click( function(){ vibrateIndex = setInterval(vibrate, interval); setTimeout(stopVibration, duration); });
The button click will be attached with an event handler click that will use setInterval to vibrate the outer container in every 'interval' given (move left right top down randomly in every 'interval'). This will caused it to vibrate forever. Thus, we will have to use 'setTimeout' function to stop the vibration by placing the method 'stopVibration' and the 'duration' it wants to vibrate. And it is as simple as that. The final code will be:
$(function() { var interval = 30; var duration= 1000; var shake= 3; var vibrateIndex = 0; var selector = $('#frame'); $('#activate').click( function(){ vibrateIndex = setInterval(vibrate, interval); setTimeout(stopVibration, duration); }); var vibrate = function(){ $(selector).stop(true,false) .css({position: 'relative', left: Math.round(Math.random() * shake) - ((shake + 1) / 2) +'px', top: Math.round(Math.random() * shake) - ((shake + 1) / 2) +'px'}); } var stopVibration = function() { clearInterval(vibrateIndex); $(selector).stop(true,false) .css({position: 'static', left: '0px', top: '0px'}); }; });
That is all you need to make your own vibrate effect.
The Demo
You can get and view the demo files from the following link:
Although I don't know how practical this UI would be, it's definitely a neat effect!
Looks cool.
However, it doesn't quit if the user double clicks on the submit button.
So just add a check to see if it running or not.
This is accomplished by resetting vibrateIndex back to 0, and only starting a new Interval if vibrateIndex doesn't exist.
Or you could just disable the button until it is finished.
$('#activate').click( function(){
if( !vibrateIndex ){
vibrateIndex = setInterval(vibrate, interval);
setTimeout(stopVibration, duration);
}
});
var stopVibration = function() {
clearInterval(vibrateIndex);
vibrateIndex = 0;
$(selector).stop(true,false)
.css({position: 'static', left: '0px', top: '0px'});
};
Test out your javascript skills with a simple quiz.
http://bateru.com/jquery/jquizme2/demo3.html
nice work around Larry! That were some good suggestion to overcome the interval index from overwritten by the second click. There are other ways such as using an array or stack to keep track on the number of index being used and terminated. But yours is cool enough to simplify the process!