另存html 后缀即可,双击运行。
html
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>自定义倒计时</title>
<style>
body {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
margin: 0;
background: linear-gradient(45deg, #3498db, #8e44ad);
color: white;
}
.countdown-card {
background: rgba(255, 255, 255, 0.1);
border-radius: 20px;
padding: 40px;
box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
text-align: center;
}
h1 {
margin-bottom: 20px;
font-size: 24px;
text-transform: uppercase;
letter-spacing: 2px;
}
#countdown {
font-size: 72px;
font-weight: bold;
}
.time-label {
font-size: 18px;
text-transform: uppercase;
margin-top: 10px;
opacity: 0.7;
}
input, select, button {
margin-top: 20px;
padding: 10px;
font-size: 16px;
}
button {
background-color: #2ecc71;
color: white;
border: none;
cursor: pointer;
transition: background-color 0.3s;
}
button:hover {
background-color: #27ae60;
}
@media (max-width: 480px) {
.countdown-card { padding: 20px; }
h1 { font-size: 18px; }
#countdown { font-size: 48px; }
.time-label { font-size: 14px; }
}
</style>
</head>
<body>
<div class="countdown-card">
<h1>倒计时</h1>
<div id="countdown">00:00:00</div>
<div class="time-label">剩余时间</div>
<input type="time" id="targetTime" value="17:30">
<select id="displayFormat">
<option value="seconds">秒</option>
<option value="minutes">分秒</option>
<option value="hours" selected>时分秒</option>
</select>
<button onclick="setCountdown()">设置倒计时</button>
</div>
<script>
let targetTime = new Date();
targetTime.setHours(17, 30, 0, 0);
function setCountdown() {
const input = document.getElementById('targetTime').value;
const [hours, minutes] = input.split(':');
targetTime = new Date();
targetTime.setHours(hours, minutes, 0, 0);
if (targetTime <= new Date()) {
targetTime.setDate(targetTime.getDate() + 1);
}
updateCountdown();
}
function updateCountdown() {
const now = new Date();
const diff = Math.max(0, Math.floor((targetTime - now) / 1000));
const hours = Math.floor(diff / 3600);
const minutes = Math.floor((diff % 3600) / 60);
const seconds = diff % 60;
const countdownElement = document.getElementById('countdown');
const labelElement = document.querySelector('.time-label');
const format = document.getElementById('displayFormat').value;
if (diff <= 0) {
countdownElement.textContent = "时间到!";
labelElement.style.display = 'none';
} else {
let display;
switch (format) {
case 'seconds':
display = `${diff}`;
break;
case 'minutes':
display = `${String(minutes + hours * 60).padStart(2, '0')}:${String(seconds).padStart(2, '0')}`;
break;
case 'hours':
default:
display = `${String(hours).padStart(2, '0')}:${String(minutes).padStart(2, '0')}:${String(seconds).padStart(2, '0')}`;
}
countdownElement.textContent = display;
labelElement.style.display = '';
}
}
setCountdown(); // 初始化倒计时
setInterval(updateCountdown, 1000);
// 当显示格式改变时更新倒计时
document.getElementById('displayFormat').addEventListener('change', updateCountdown);
</script>
</body>
</html>