文本与代码预览
index.html
html · 11.63 KB
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="theme-color" content="#0b1020">
<title>倒计时器</title>
<style>
* { box-sizing: border-box; }
body {
margin: 0;
min-height: 100vh;
display: flex;
align-items: center;
justify-content: center;
padding: 24px;
background: radial-gradient(circle at 30% 15%, #1e2a4a 0%, #0b1020 60%);
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC",
"Microsoft YaHei", sans-serif;
color: #e8eefc;
-webkit-tap-highlight-color: transparent;
}
.card {
width: min(420px, 100%);
padding: 26px 24px 22px;
border-radius: 26px;
background: rgba(255, 255, 255, 0.06);
border: 1px solid rgba(255, 255, 255, 0.12);
backdrop-filter: blur(14px);
-webkit-backdrop-filter: blur(14px);
box-shadow: 0 20px 60px rgba(0, 0, 0, 0.45);
text-align: center;
transition: box-shadow .3s;
}
.card.flash { animation: flashGlow 0.9s ease-in-out infinite; }
@keyframes flashGlow {
0%, 100% { box-shadow: 0 20px 60px rgba(0, 0, 0, 0.45); }
50% { box-shadow: 0 0 0 6px rgba(255, 95, 109, 0.35),
0 20px 60px rgba(0, 0, 0, 0.45); }
}
.ring-wrap {
position: relative;
width: min(260px, 72vw);
aspect-ratio: 1 / 1;
margin: 0 auto 6px;
}
.ring-wrap svg {
width: 100%;
height: 100%;
transform: rotate(-90deg);
display: block;
}
circle {
fill: none;
stroke-width: 10;
stroke-linecap: round;
}
.track { stroke: rgba(255, 255, 255, 0.10); }
.progress {
stroke: #4dd4ac;
filter: drop-shadow(0 0 6px rgba(77, 212, 172, 0.55));
transition: stroke .3s, filter .3s;
}
.progress.warn {
stroke: #ff5f6d;
filter: drop-shadow(0 0 9px rgba(255, 95, 109, 0.75));
}
.display {
position: absolute;
inset: 0;
display: flex;
align-items: center;
justify-content: center;
font-size: clamp(38px, 13vw, 54px);
font-weight: 700;
font-variant-numeric: tabular-nums;
letter-spacing: 2px;
color: #eaf2ff;
transition: color .3s;
}
.display.warn { color: #ff8b95; }
.status {
height: 20px;
font-size: 13px;
color: #93a6c9;
letter-spacing: .5px;
margin-bottom: 6px;
}
button {
font-family: inherit;
cursor: pointer;
border: none;
transition: transform .15s, filter .2s, background .2s, opacity .2s;
}
button:active { transform: scale(0.97); }
button:disabled {
opacity: .45;
cursor: not-allowed;
transform: none;
}
.controls {
display: flex;
gap: 10px;
margin-top: 6px;
}
.controls button {
flex: 1;
padding: 13px 0;
border-radius: 14px;
font-size: 15px;
font-weight: 600;
}
.primary {
background: linear-gradient(135deg, #4dd4ac, #3aa3ff);
color: #06121f;
box-shadow: 0 8px 20px rgba(77, 212, 172, 0.25);
}
.primary:hover:not(:disabled) { filter: brightness(1.08); }
.ghost {
background: rgba(255, 255, 255, 0.08);
color: #dce6ff;
border: 1px solid rgba(255, 255, 255, 0.14);
}
.ghost:hover:not(:disabled) { background: rgba(255, 255, 255, 0.14); }
.divider {
height: 1px;
background: rgba(255, 255, 255, 0.1);
margin: 18px 0 14px;
}
.settings {
display: flex;
align-items: center;
gap: 8px;
}
.settings label {
font-size: 13px;
color: #9fb0d0;
white-space: nowrap;
}
input[type="number"] {
flex: 1;
min-width: 0;
padding: 10px 12px;
border-radius: 12px;
border: 1px solid rgba(255, 255, 255, 0.15);
background: rgba(255, 255, 255, 0.06);
color: #e8eefc;
font-size: 15px;
font-family: inherit;
outline: none;
transition: border-color .2s, background .2s;
}
input[type="number"]:focus {
border-color: #4dd4ac;
background: rgba(77, 212, 172, 0.08);
}
input[type="number"]::-webkit-outer-spin-button,
input[type="number"]::-webkit-inner-spin-button { -webkit-appearance: none; margin: 0; }
input[type="number"] { -moz-appearance: textfield; appearance: textfield; }
#setBtn {
padding: 10px 16px;
border-radius: 12px;
font-size: 14px;
font-weight: 600;
background: rgba(77, 212, 172, 0.16);
color: #7fe7c4;
border: 1px solid rgba(77, 212, 172, 0.35);
}
#setBtn:hover { background: rgba(77, 212, 172, 0.26); }
.presets {
display: flex;
gap: 8px;
margin-top: 10px;
}
.presets button {
flex: 1;
padding: 8px 0;
border-radius: 10px;
font-size: 13px;
color: #b9c8e4;
background: rgba(255, 255, 255, 0.06);
border: 1px solid rgba(255, 255, 255, 0.1);
}
.presets button:hover {
background: rgba(255, 255, 255, 0.14);
color: #e8eefc;
}
.hint {
margin-top: 14px;
font-size: 12px;
color: #6c7d9c;
}
.hint kbd {
display: inline-block;
padding: 1px 6px;
margin: 0 2px;
border-radius: 5px;
background: rgba(255, 255, 255, 0.1);
border: 1px solid rgba(255, 255, 255, 0.15);
font-family: inherit;
font-size: 11px;
color: #b9c8e4;
}
</style>
</head>
<body>
<div class="card" id="card">
<div class="ring-wrap">
<svg viewBox="0 0 200 200">
<circle class="track" cx="100" cy="100" r="90"></circle>
<circle class="progress" id="progress" cx="100" cy="100" r="90"></circle>
</svg>
<div class="display" id="display">00:20</div>
</div>
<div class="status" id="status">倒计时中…</div>
<div class="controls">
<button class="primary" id="startBtn" disabled>运行中</button>
<button class="ghost" id="pauseBtn">暂停</button>
<button class="ghost" id="resetBtn">重置</button>
</div>
<div class="divider"></div>
<div class="settings">
<label for="setSeconds">设置秒数</label>
<input type="number" id="setSeconds" value="20" min="1" max="5999" step="1">
<button id="setBtn">设置</button>
</div>
<div class="presets">
<button data-preset="10">10 秒</button>
<button data-preset="20">20 秒</button>
<button data-preset="30">30 秒</button>
<button data-preset="60">1 分钟</button>
<button data-preset="300">5 分钟</button>
</div>
<div class="hint">
快捷键:<kbd>空格</kbd> 开始 / 暂停 <kbd>R</kbd> 重置
</div>
</div>
<script>
(function () {
'use strict';
const RADIUS = 90;
const CIRC = 2 * Math.PI * RADIUS;
const DEFAULT_SECONDS = 20;
const MAX_SECONDS = 5999;
let total = DEFAULT_SECONDS;
let remain = DEFAULT_SECONDS;
let running = false;
let endTime = 0;
let rafId = null;
let audioCtx = null;
const cardEl = document.getElementById('card');
const displayEl = document.getElementById('display');
const progressEl = document.getElementById('progress');
const statusEl = document.getElementById('status');
const inputEl = document.getElementById('setSeconds');
const startBtn = document.getElementById('startBtn');
const pauseBtn = document.getElementById('pauseBtn');
const resetBtn = document.getElementById('resetBtn');
const setBtn = document.getElementById('setBtn');
progressEl.style.strokeDasharray = String(CIRC);
progressEl.style.strokeDashoffset = '0';
function formatTime(sec) {
const s = Math.max(0, Math.ceil(sec - 1e-6));
const m = Math.floor(s / 60);
const r = s % 60;
return String(m).padStart(2, '0') + ':' + String(r).padStart(2, '0');
}
function render() {
displayEl.textContent = formatTime(remain);
const ratio = total > 0 ? Math.min(1, Math.max(0, remain / total)) : 0;
progressEl.style.strokeDashoffset = String(CIRC * (1 - ratio));
const warn = remain > 0 && remain <= 5;
progressEl.classList.toggle('warn', warn);
displayEl.classList.toggle('warn', warn);
document.title = formatTime(remain) + ' - 倒计时';
}
function playBeep() {
try {
if (!audioCtx) {
const AC = window.AudioContext || window.webkitAudioContext;
if (!AC) return;
audioCtx = new AC();
}
if (audioCtx.state === 'suspended') audioCtx.resume();
const t0 = audioCtx.currentTime;
for (let i = 0; i < 3; i++) {
const start = t0 + i * 0.42;
const osc = audioCtx.createOscillator();
const gain = audioCtx.createGain();
osc.type = 'sine';
osc.frequency.setValueAtTime(880, start);
gain.gain.setValueAtTime(0.0001, start);
gain.gain.linearRampToValueAtTime(0.3, start + 0.02);
gain.gain.exponentialRampToValueAtTime(0.0001, start + 0.32);
osc.connect(gain).connect(audioCtx.destination);
osc.start(start);
osc.stop(start + 0.35);
}
} catch (e) {}
}
function loop() {
remain = (endTime - performance.now()) / 1000;
if (remain <= 0) {
remain = 0;
render();
finish();
return;
}
render();
rafId = requestAnimationFrame(loop);
}
function start() {
if (running) return;
if (remain <= 0.001) remain = total;
endTime = performance.now() + remain * 1000;
running = true;
cardEl.classList.remove('flash');
statusEl.textContent = '倒计时中…';
startBtn.textContent = '运行中';
startBtn.disabled = true;
pauseBtn.disabled = false;
cancelAnimationFrame(rafId);
rafId = requestAnimationFrame(loop);
render();
}
function pause() {
if (!running) return;
running = false;
cancelAnimationFrame(rafId);
rafId = null;
remain = Math.max(0, (endTime - performance.now()) / 1000);
render();
statusEl.textContent = '已暂停';
startBtn.textContent = '继续';
startBtn.disabled = false;
pauseBtn.disabled = true;
}
function reset() {
running = false;
cancelAnimationFrame(rafId);
rafId = null;
remain = total;
cardEl.classList.remove('flash');
startBtn.textContent = '开始';
startBtn.disabled = false;
pauseBtn.disabled = true;
statusEl.textContent = '准备就绪';
render();
}
function finish() {
running = false;
cancelAnimationFrame(rafId);
rafId = null;
startBtn.textContent = '重新开始';
startBtn.disabled = false;
pauseBtn.disabled = true;
statusEl.textContent = '⏰ 时间到!';
cardEl.classList.add('flash');
playBeep();
setTimeout(function () {
cardEl.classList.remove('flash');
}, 3000);
}
function applySetting() {
let v = parseInt(inputEl.value, 10);
if (isNaN(v) || v <= 0) v = DEFAULT_SECONDS;
v = Math.min(v, MAX_SECONDS);
inputEl.value = v;
total = v;
reset();
start(); // 设置后自动开始倒数
}
startBtn.addEventListener('click', start);
pauseBtn.addEventListener('click', pause);
// 重置后直接自动开始
resetBtn.addEventListener('click', function () {
reset();
start();
});
setBtn.addEventListener('click', applySetting);
inputEl.addEventListener('keydown', function (e) {
if (e.key === 'Enter') {
e.preventDefault();
applySetting();
inputEl.blur();
}
});
document.querySelectorAll('[data-preset]').forEach(function (btn) {
btn.addEventListener('click', function () {
inputEl.value = btn.dataset.preset;
applySetting();
});
});
document.addEventListener('keydown', function (e) {
if (e.target && e.target.tagName === 'INPUT') return;
if (e.code === 'Space') {
e.preventDefault();
running ? pause() : start();
} else if (e.key === 'r' || e.key === 'R') {
reset();
start();
}
});
// ===== 初始化:打开页面立即自动倒数 20 秒 =====
inputEl.value = DEFAULT_SECONDS;
total = DEFAULT_SECONDS;
remain = DEFAULT_SECONDS;
render();
start();
})();
</script>
</body>
</html>