代碼在圖片后面? 點贊加關注 謝謝大佬照顧😜
圖例
時間到前
?時間到后
?源代碼
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>倒計時</title>
<style>
? ? body {
? ? ? ? display: flex;
? ? ? ? justify-content: center;
? ? ? ? align-items: center;
? ? ? ? height: 100vh;
? ? ? ? background-color: #f4f4f4;
? ? ? ? margin: 0;
? ? ? ? font-family: Arial, sans-serif;
? ? }
? ??
? ? .countdown-container {
? ? ? ? text-align: center;
? ? ? ? border-radius: 8px;
? ? ? ? padding: 20px;
? ? ? ? transition: border-color 0.3s ease;
? ? }
? ??
? ? h1 {
? ? ? ? font-size: 2.5em;
? ? ? ? color: #333;
? ? ? ? margin-bottom: 20px;
? ? ? text-align:center;
? ? }
? ??
? ? .time-left {
? ? ? ? font-size: 1.8em;
? ? ? ? color: #333;
? ? ? ? border: 2px solid #4caf50;
? ? ? ? padding: 10px;
? ? ? ? border-radius: 8px;
? ? }
? ??
? ? .time-left.time-up {
? ? ? ? border-color: #e74c3c;
? ? }
</style>
</head>
<body>
<div class="countdown-container">
? ? <p class="time-left">距離下班還1分鐘</p>
</div>
<script>
? ? const timeDisplay = document.querySelector('.time-left');
? ? let countdown = 1 * 60; // 5 minutes in seconds
? ? function updateCountdown() {
? ? ? ? let minutes = Math.floor(countdown / 60);
? ? ? ? let seconds = countdown % 60;
? ? ? ? minutes = minutes < 10 ? '0' + minutes : minutes;
? ? ? ? seconds = seconds < 10 ? '0' + seconds : seconds;
? ? ? ? timeDisplay.textContent = `距離下班還有${minutes}:${seconds}`;
? ? ? ? if (countdown <= 0) {
? ? ? ? ? ? timeDisplay.classList.add('time-up');
? ? ? ? ? ? clearInterval(intervalId);
? ? ? ? }
? ? ? ? countdown--;
? ? }
? ? const intervalId = setInterval(updateCountdown, 1000);
</script>
</body>
</html>