Hi People, In this post we are going to write a simple jQuery script to shake effect (animate) your input box.
To animate we need to load jQuery and jQuery UI libraries in our script and then a single line of script would do the magic for us.
1 |
$('input').effect("shake",{times: 3}, 800); |
That’s all, this much of script is enough to animate the input box.
But, wait let’s write a real scenario script where we can use this animation, so you can also use it in your project.
A simple form validation with shake effect.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>JQuery Shake Effect Example</title> <meta name="viewport" content="width=device-width, initial-scale=1"> <!-- Load Bootstrap library for better UI look & feel --> <link rel="stylesheet" type="text/css" media="screen" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" /> <!-- Load the jQuery & jQuery UI libraries --> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script> <style> .highlight{ box-shadow: 0 0 5px red; padding: 3px 0px 3px 3px; margin: 5px 1px 3px 0px; border: 1px solid red; } </style> <script> $(function(){ $('#myform').submit(function(){ var submit = true; var eachVal; //loop through all the input which has the class .req $('.req').each(function(index){ eachVal = $(this).val(); //find them if empty if(eachVal == ''){ //highlight with red & shake it 3 times $(this).addClass('highlight'); $(this).effect('shake',{times: 3}, 800); submit = false; }else{ //if everything find remove the highlight $(this).removeClass('highlight'); } }); return submit; }); }); </script> </head> <body> <div class="container"> <br /> <h2>JQuery Shake Effect - Demo by Theonlytutorials.com</h2> <br /> <form action="" id="myform"> <div class="form-group"> <label for="email">Email address:</label> <input type="email" class="form-control req" id="email" > </div> <div class="form-group"> <label for="pwd">Password:</label> <input type="password" class="form-control req" id="pwd" > </div> <div class="checkbox"> <label><input type="checkbox"> Remember me</label> </div> <button type="submit" class="btn btn-default">Submit</button> </form> </div> </body> </html> |
You can just go through the above script to understand it, any doubt you can ask them in the comment section.
Finally here is the demo: