Welcome to our comprehensive guide on creating a Shake Effect using jQuery! By the end of this tutorial, you'll not only learn how to make an element shake but also understand why it works. Let's get started!
The Shake Effect is a visual animation where an element on a webpage appears to be shaking. It's often used to draw attention to important changes or alerts.
Before diving into the code, make sure you have the following prerequisites:
Let's create a simple HTML structure for our Shake Effect demo.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>jQuery Shake Effect</title>
<!-- Add jQuery library -->
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<!-- Add our custom JavaScript file -->
<script src="shake-effect.js"></script>
</head>
<body>
<div id="shaking-box">
This box will shake!
</div>
</body>
</html>In this example, we've added the jQuery library and created a #shaking-box div that will be animated with the Shake Effect.
Now, let's create the JavaScript file (shake-effect.js) to make the element shake.
$(document).ready(function() {
// Select the shaking box
var shakingBox = $('#shaking-box');
// Function to shake the box
function shakeBox() {
shakingBox.addClass('shake');
// After the shake animation, reset the box
setTimeout(function() {
shakingBox.removeClass('shake');
}, 600);
}
// Add a click event to shake the box on click
shakingBox.click(function() {
shakeBox();
});
// Add a shake-on-load effect
setTimeout(function() {
shakeBox();
}, 2000);
});In this code, we've defined a shakeBox() function that adds the shake animation class to the selected element and removes it after a short delay. We've also added a click event to trigger the shake effect when the box is clicked and a delay to shake the box on page load.
To create the actual shake animation, we'll add some CSS to our project. Add the following CSS code within the <style> tags in the HTML file or create a separate CSS file and link it in the <head> section.
.shake {
-webkit-animation: shake 0.6s;
animation: shake 0.6s;
}
@-webkit-keyframes shake {
0% {
-webkit-transform: translate(2px, 0px);
transform: translate(2px, 0px);
}
10% {
-webkit-transform: translate(-2px, 0px);
transform: translate(-2px, 0px);
}
20% {
-webkit-transform: translate(2px, 0px);
transform: translate(2px, 0px);
}
30% {
-webkit-transform: translate(-2px, 0px);
transform: translate(-2px, 0px);
}
40% {
-webkit-transform: translate(2px, 0px);
transform: translate(2px, 0px);
}
50% {
-webkit-transform: translate(-2px, 0px);
transform: translate(-2px, 0px);
}
60% {
-webkit-transform: translate(2px, 0px);
transform: translate(2px, 0px);
}
70% {
-webkit-transform: translate(-2px, 0px);
transform: translate(-2px, 0px);
}
80% {
-webkit-transform: translate(2px, 0px);
transform: translate(2px, 0px);
}
90% {
-webkit-transform: translate(-2px, 0px);
transform: translate(-2px, 0px);
}
100% {
-webkit-transform: translate(0px, 0px);
transform: translate(0px, 0px);
}
}
@keyframes shake {
0% {
-webkit-transform: translate(2px, 0px);
transform: translate(2px, 0px);
}
10% {
-webkit-transform: translate(-2px, 0px);
transform: translate(-2px, 0px);
}
20% {
-webkit-transform: translate(2px, 0px);
transform: translate(2px, 0px);
}
30% {
-webkit-transform: translate(-2px, 0px);
transform: translate(-2px, 0px);
}
40% {
-webkit-transform: translate(2px, 0px);
transform: translate(2px, 0px);
}
50% {
-webkit-transform: translate(-2px, 0px);
transform: translate(-2px, 0px);
}
60% {
-webkit-transform: translate(2px, 0px);
transform: translate(2px, 0px);
}
70% {
-webkit-transform: translate(-2px, 0px);
transform: translate(-2px, 0px);
}
80% {
-webkit-transform: translate(2px, 0px);
transform: translate(2px, 0px);
}
90% {
-webkit-transform: translate(-2px, 0px);
transform: translate(-2px, 0px);
}
100% {
-webkit-transform: translate(0px, 0px);
transform: translate(0px, 0px);
}
}This CSS code defines the shake animation for both WebKit (Safari and Chrome) and non-WebKit browsers (Firefox, Edge, etc.).
Now that we've completed the code, save your files and open the HTML file in a web browser to see the Shake Effect in action.
Which CSS property do we use to create the shake animation in the `shake-effect.js` file?