Shake is an effect from jQuery UI to shake an element of HTML. In this tutorial we will add shake effect on login Form element when invalid entry. For more detail, Lets pay attentions on steps bellow :
shake.html.<html>
<head>
<title>Shake Form</title>
</head>
<body>
</body>
</html>
<div id="panel" style="width: 500px; height:auto; border: solid 1px">
</div>
Username : <input id="username" type="text"><br><br>
Password : <input id="pwd" type="password"><br><br>
<button id="submit">TEST</button>
Because we don't want to install or host the jQuery and jQuery UI file, we should add The CDN as replacement.
<head> element.<script src="http://code.jquery.com/jquery-3.3.1.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script> tag. You can put it in element or in <body> or in <head> element.<script></script>
$(document).ready(function(){
});
$("#submit").click(function(){
});
var user=$("#username").val();
var pwd=$("#pwd").val();
if(user!="sogata"&&pwd!="1234"){
}
$( "#panel" ).effect("shake");
else{
alert("the username and password is correct")
}
Save the file and try to run
The correct username and password here is sogata and 1234. Try to fill with the correct and uncorrect entry. when you fill invalid entry you will get your login form is shaked. and when valid, you will get the message that your entry is valid.
We can modify this shake effect like set the direction, distance, time to shake and duration. here the code
$( "#panel" ).effect( "shake", { direction: "right", times: 4, distance: 2}, 1000);
directionto Set the direction of the effect, It takes left, right, up, and down value.timesto set how many time the shaking.distanceto set the distance of shake, this is in px.1000this is the duration of shaking in milisecond.
<html>
<head>
<title>SHAKE FORM</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
</head>
<body>
<center>
<div id="panel" style="width: 500px; height:auto; border: solid 1px">
<h3>LOGIN FORM</h3>
Username : <input id="username" type="text"><br><br>
Password : <input id="pwd" type="password"><br><br>
<button id="submit">TEST</button>
</div>
</center>
<script>
$(document).ready(function(){
$("#submit").click(function(){
var user=$("#username").val();
var pwd=$("#pwd").val();
if(user!="sogata"&&pwd!="1234"){
$( "#panel" ).effect( "shake", { direction: "right", times: 4, distance: 2}, 1000);
}
else{
alert("the username and password is correct")
}
});
});
</script>
</body>
</html>