HTML語言中,我們可以通過簡單的代碼實現一些有趣的效果,比如制作一個心形計時器,今天就來看看具體的代碼吧!
<!DOCTYPE html> <html> <head> <title>心形計時器</title> <meta charset="utf-8"> <style> #heart { width: 30px; height: 30px; position: relative; animation: beat 1s linear infinite; } #heart:before, #heart:after { content: ""; position: absolute; background-color: red; border-radius: 30px 30px 0 0; } #heart:before { width: 30px; height: 30px; top: -15px; left: 0px; } #heart:after { width: 30px; height: 30px; top: 0px; left: 15px; } @keyframes beat { 0% { transform: scale(1); } 50% { transform: scale(1.2); } 100% { transform: scale(1); } } </style> </head> <body> <div id="heart"></div> <div id="time"></div> <button onclick="startTimer()">開始計時</button> <script> var startTime, currentTime, timeDifference, timeSeconds, timeMinutes, timeHours, interval; function startTimer() { startTime = Date.now(); interval = setInterval(updateTimer, 1000); } function updateTimer() { currentTime = Date.now(); timeDifference = currentTime - startTime; timeSeconds = Math.floor(timeDifference / 1000); timeMinutes = Math.floor(timeSeconds / 60); timeHours = Math.floor(timeMinutes / 60); timeSeconds = timeSeconds % 60; timeMinutes = timeMinutes % 60; document.getElementById("time").innerHTML = timeHours + ":" + timeMinutes + ":" + timeSeconds; } </script> </body> </html>
以上是實現心形計時器的HTML代碼,其中包含了CSS樣式和JavaScript代碼。CSS樣式部分用于制作心形的形狀和動態效果,JavaScript代碼部分則負責計時的邏輯。我們可以通過點擊“開始計時”按鈕啟動計時器,實時顯示已流逝的時間。
希望大家可以根據這個例子,深入學習HTML語言,掌握更多有趣、實用的功能。代碼雖然有些復雜,但只要有耐心,相信大家都能掌握!