倒计时反馈
Countdown Timer
数字逐秒减至零,并在时间结束时给出提示。
适用场景
展示会话、练习或重发冷却的剩余时间。 把倒计时放在归零时会变化的操作旁边,让用户提前知道等到零以后能做什么。
应用示例
验证码发出后,验证页在「重新发送」旁显示剩余秒数。归零时,界面提示可以重新获取验证码,同时启用发送按钮。
AI 提示词
给【任务】添加倒计时。由截止时间计算剩余秒数,使用等宽数字,在到期时播报结果,避免每次跳秒都播报。
示例代码
保存为 HTML 文件后,用浏览器打开即可运行。代码与页面里的演示一致。
查看 HTML
<!doctype html>
<html lang="zh">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>倒计时反馈</title>
<style>
:root {
color-scheme: dark;
--demo-bg: #171629;
--demo-ink: #f8f5ff;
--demo-accent: #b9a6ff;
--demo-muted: #bfbbd3;
font-family: system-ui, sans-serif;
color: var(--demo-ink);
background: var(--demo-bg);
}
*, *::before, *::after { box-sizing: border-box; }
body { margin: 0; min-width: 0; }
button, input { font: inherit; }
.demo {
display: grid;
place-items: center;
align-content: center;
min-height: 100vh;
padding: 14px;
overflow: hidden;
background: radial-gradient(ellipse at 50% 24%, #28233f, var(--demo-bg) 78%);
}
.demo-button {
border: 1px solid #ffffff3d;
border-radius: 7px;
background: #ffffff12;
color: var(--demo-ink);
padding: 6px 11px;
font-size: 12px;
cursor: pointer;
}
:focus-visible {
outline: 2px solid var(--demo-accent);
outline-offset: 3px;
}
.example { width: min(100%, 228px); position: relative; display: grid; gap: 10px; }
.panel { background: #292240; border: 1px solid #675785; border-radius: 11px; padding: 10px; }
.row { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.actions { display: flex; gap: 6px; flex-wrap: wrap; justify-content: center; }
.label { margin: 0; font-size: 13px; font-weight: 650; }
.detail, .caption { margin: 0; color: #d7cbea; font-size: 11px; line-height: 1.35; }
.caption { text-align: center; }
.control { border: 1px solid #8874ae; background: #392e57; border-radius: 7px; padding: 5px 9px; min-height: 32px; color: #f6efff; cursor: pointer; font-size: 11px; font-weight: 650; }
.control:hover { background: #514174; }
.control.primary { color: #261b3f; background: #d1bfff; border-color: #e3d8ff; }
.control.primary:hover { background: #e3d8ff; }
.control:disabled { opacity: .55; cursor: default; }
.field { width: 100%; min-width: 0; padding: 6px 8px; border: 1px solid #875fa8; border-radius: 6px; color: #f6efff; background: #1e1732; font-size: 12px; }
.field::placeholder { color: #c2abd8; }
[hidden] { display: none !important; }
.timer { display: grid; gap: 5px; justify-items: center; }
.timer time { color: #f0dbff; font: 700 26px/1.2 ui-monospace, monospace; font-variant-numeric: tabular-nums; }
.timer .detail { min-height: 15px; }
@media (prefers-reduced-motion: reduce) {
.example *, .example *::before, .example *::after { animation: none !important; transition: none !important; scroll-behavior: auto !important; }
}
</style>
</head>
<body>
<main class="demo">
<div class="example"><div class="panel timer"><p class="label">倒计时</p><time datetime="PT10S">00:10</time><p class="detail" role="status"></p></div><div class="actions"><button class="control primary" type="button" data-run>开始</button><button class="control" type="button" data-reset>重置</button></div></div>
</main>
<script>
const time = document.querySelector('time');
const status = document.querySelector('[role="status"]');
const run = document.querySelector('[data-run]');
let remaining = 10000;
let finish = 0;
let interval = 0;
function render() {
const seconds = Math.ceil(remaining / 1000);
time.textContent = '00:' + String(seconds).padStart(2, '0');
time.dateTime = 'PT' + seconds + 'S';
}
function stop() {
clearInterval(interval);
interval = 0;
run.textContent = remaining > 0 && remaining < 10000 ? "继续" : "开始";
}
function tick() {
remaining = Math.max(0, finish - Date.now());
render();
if (!remaining) {
stop();
status.textContent = "时间到。";
}
}
run.addEventListener('click', () => {
if (interval) { tick(); stop(); return; }
if (!remaining) remaining = 10000;
status.textContent = '';
finish = Date.now() + remaining;
run.textContent = "暂停";
interval = setInterval(tick, 100);
render();
});
document.querySelector('[data-reset]').addEventListener('click', () => {
remaining = 10000;
status.textContent = '';
stop();
render();
});
</script>
</body>
</html>UI Skills 原创示例