HTML、CSS 和 JavaScript 中的双击爱心动画
为了让您的网站对用户更友好,您可以添加一些动画效果,使交互更具视觉吸引力。其中一种广泛使用的效果是爱心动画,它在社交媒体平台上双击时出现。本文将教您如何在 HTML、CSS 和 JavaScript 中开发双击爱心动画。
先决条件
对于此项目,您只需要了解以下基本知识:
- HTML 用于构建元素。
- CSS 用于样式和动画。
- JavaScript 用于添加交互性。
HTML 结构
我们将创建一个简单的 HTML 结构,其中包括
- 动画的容器。
- 爱心图标的占位符。
index.html
<!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 For Heart</title> <link rel="stylesheet" href="style.css" /> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.3.0/css/all.min.css" /> <script src="script.js" defer></script> </head> <body> <div class="container"> <i class="fa-solid fa-heart heart"></i> </div> </body> </html>
CSS 样式
为了使爱心在视觉上更具吸引力,我们将使用 CSS 来定义
- 爱心的初始外观。
- 它出现时的动画效果。
style.css
/* style.css */ @import url("https://fonts.googleapis.com/css2?family=Poppins:wght@200;300;400;500;600;700&display=swap"); * { margin: 0; padding: 0; box-sizing: border-box; font-family: "Poppins", sans-serif; } body { height: 100vh; display: flex; align-items: center; justify-content: center; background: #f6f7fb; } .container { position: relative; height: 420px; width: 320px; background-image: url("img.jpg"); background-size: cover; background-position: center; border-radius: 12px; cursor: pointer; box-shadow: 0 15px 25px rgba(0, 0, 0, 0.1); } .heart { position: absolute; color: red; font-size: 40px; opacity: 0; transform: translate(-50%, -50%); } .heart.active { animation: animate 0.8s linear forwards; } @keyframes animate { 30% { font-size: 80px; opacity: 1; } 50% { opacity: 1; font-size: 60px; } 70% { font-size: 70px; } 80% { font-size: 60px; opacity: 1; } 90% { font-size: 60px; opacity: 1; } }
JavaScript 逻辑
在这里,我们将使爱心对双击做出反应。
script.js
// script.js // Select the container and heart elements from the DOM const container = document.querySelector(".container"), heart = document.querySelector(".heart"); // Add a double-click event listener to the container container.addEventListener("dblclick", (e) => { // Calculate the x and y position of the double-click event let xValue = e.clientX - e.target.offsetLeft, yValue = e.clientY - e.target.offsetTop; // Set the position of the heart element using the x and y values heart.style.left = `${xValue}px`; heart.style.top = `${yValue}px`; // Add the active class to the heart element to animate it heart.classList.add("active"); // Remove the active class after 1 second setTimeout(() => { heart.classList.remove("active"); }, 1000); });
双击爱心动画的完整代码
这里我们组合了上述 HTML、CSS 和 JavaScript 代码,以便您可以在我们的门户网站上运行它。
<!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 For Heart</title> <style> @import url("https://fonts.googleapis.com/css2?family=Poppins:wght@200;300;400;500;600;700&display=swap"); * { margin: 0; padding: 0; box-sizing: border-box; font-family: "Poppins", sans-serif; } body { height: 100vh; display: flex; align-items: center; justify-content: center; background: #f6f7fb; } .container { position: relative; height: 420px; width: 320px; background-image: url("img.jpg"); background-size: cover; background-position: center; border-radius: 12px; cursor: pointer; box-shadow: 0 15px 25px rgba(0, 0, 0, 0.1); } .heart { position: absolute; color: red; font-size: 40px; opacity: 0; transform: translate(-50%, -50%); } .heart.active { animation: animate 0.8s linear forwards; } @keyframes animate { 30% { font-size: 80px; opacity: 1; } 50% { opacity: 1; font-size: 60px; } 70% { font-size: 70px; } 80% { font-size: 60px; opacity: 1; } 90% { font-size: 60px; opacity: 1; } } </style> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.3.0/css/all.min.css" /> <script src="script.js" defer></script> </head> <body> <div class="container"> <i class="fa-solid fa-heart heart"></i> </div> <script> // Select the container and heart elements from the DOM const container = document.querySelector(".container"), heart = document.querySelector(".heart"); // Add a double-click event listener to the container container.addEventListener("dblclick", (e) => { // Calculate the x and y position of the double-click event let xValue = e.clientX - e.target.offsetLeft, yValue = e.clientY - e.target.offsetTop; // Set the position of the heart element using the x and y values heart.style.left = `${xValue}px`; heart.style.top = `${yValue}px`; // Add the active class to the heart element to animate it heart.classList.add("active"); // Remove the active class after 1 second setTimeout(() => { heart.classList.remove("active"); }, 1000); }); </script> </body> </html>
输出
广告