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>

輸出

更新於: 2024年11月7日

52 次瀏覽

開啟您的 職業生涯

透過完成課程獲得認證

立即開始
廣告