Hello wizards, I hope you are doing great. Today in this blog you’ll learn how to create the "Double Click Animation" project using HTML, CSS & JavaScript.
Preview
In the above video, you’ve seen the preview of the "Double Click Animation" project and I hope now you are able to create this type of project. If not, I have provided all the HTML CSS, and JavaScript code below.
Double Click Animation [Source Code]
To get the following HTML, CSS & JS code for the Double Click Animation project. You need to create three files one is an HTML file, the second one is a CSS file and another one is a JS file. After creating these three files then you can copy-paste the given codes on your document.
Remember, you’ve to create a file with a .html extension for HTML code, a .css extension for CSS code, and .js for JavaScript code.
You can also download all source code files from the given download button.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Double Click Animation</title> <link rel="stylesheet" href="style.css"> <!-- Font Awesome --> <script src="https://kit.fontawesome.com/ebb335c6c8.js" crossorigin="anonymous"></script> </head> <body> <h1>Double Click Animation</h1> <div class="container"> <i class="fa-solid fa-heart"></i> </div> <script src="index.js"></script> </body> </html>
@import url("https://fonts.googleapis.com/css2?family=Poppins:wght@100;200;300;400;500;600;700;800;900&display=swap"); * { margin: 0; padding: 0; box-sizing: border-box; font-family: "Poppins", sans-serif; } body { display: flex; justify-content: center; align-items: center; flex-direction: column; min-height: 100vh; background-color: lightgrey; } h1 { margin-bottom: 10px; color: darkcyan; } .container { position: relative; width: 300px; height: 400px; background: url("iamge.jpg"); background-size: cover; background-position: center; background-repeat: no-repeat; border-radius: 10%; box-shadow: 0 0 10px rgba(0, 0, 0, 0.3); cursor: pointer; } i { color: red; position: absolute; font-size: 40px; opacity: 0; } i.active { animation: heart 1s linear forwards; } @keyframes heart { 10% { transform: scale(1.2); opacity: 0.8; } 30% { transform: scale(1.1); } 50% { opacity: 0.8; transform: scale(1); } 70% { transform: scale(1.1); } 90% { transform: scale(1.2); opacity: 0.8; } }
const containerEl = document.querySelector(".container"); const iEl = document.querySelector("i"); // whenever we double click on the container it displays heart at the click position. containerEl.addEventListener("dblclick", (e) => { let x = e.pageX - e.target.offsetLeft - 15; let y = e.pageY - e.target.offsetTop - 15; iEl.style.left = `${x}px`; iEl.style.top = `${y}px`; // adding active class iEl.classList.add("active"); // removing active class after 1.5sec setTimeout(() => { iEl.classList.remove("active"); }, 1500); });