翻牌显示
Split-Flap Display
每格字母牌翻过若干中间字符,再依次停在目标文字。
适用场景
用来切换站点名、简短倒计时文案或状态文字。 每格使用固定宽度,控制翻牌时长,并向辅助技术播报最终文字。
应用示例
一个车站风格的旅行规划器更新出发板上的城市名,每格字母自左向右依次翻到目标字符。
也叫
- 翻页字母牌
- 机械翻牌
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;
}
.demo {
background: radial-gradient(ellipse at 50% 11%, #4e4635, #28271f 74%, #1d1c18);
}
.flap-scene { display: grid; justify-items: center; width: min(100%, 442px); gap: 17px; }
.flap-board {
display: grid; grid-template-columns: repeat(6, minmax(0, 1fr)); gap: 3px;
width: min(100%, 355px); padding: 7px; border: 1px solid #6b6250; border-radius: 8px;
background: #161612; box-shadow: 0 9px 17px #16140d8c, inset 0 1px #fff1be26;
}
.flap-cell {
position: relative; height: clamp(35px, 20vh, 70px); perspective: 110px;
color: #eee2b4; font: 700 clamp(20px, 9vw, 40px)/1 ui-monospace, monospace;
font-variant-numeric: tabular-nums;
}
.flap-half {
position: absolute; display: block; inset-inline: 0; height: 50%; overflow: hidden;
background: linear-gradient(#2d2f28, #3a3a2e);
}
.flap-half[hidden] { display: none; }
.flap-top {
top: 0; border-radius: 4px 4px 1px 1px; border-bottom: 1px solid #1a1915;
}
.flap-bottom {
bottom: 0; border-radius: 1px 1px 4px 4px; background: linear-gradient(#282821, #323229);
}
.flap-half span { position: absolute; left: 50%; }
.flap-top span { top: 100%; transform: translate(-50%, -50%); }
.flap-bottom span { top: 0; transform: translate(-50%, -50%); }
.flap-moving { z-index: 1; transform-origin: center bottom; }
.flap-moving.flap-bottom { transform-origin: center top; }
.flap-status { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); }
[data-flap-next] { background: #3a372a; border-color: #827758; color: #f1e3b3; }
[data-flap-next]:focus-visible { outline-color: #f1e3b3; }
</style>
</head>
<body>
<main class="demo">
<div class="flap-scene">
<div class="flap-board" aria-hidden="true">
<span class="flap-cell">
<span class="flap-half flap-top"><span>L</span></span>
<span class="flap-half flap-bottom"><span>L</span></span>
<span class="flap-half flap-top flap-moving" hidden><span></span></span>
</span>
<span class="flap-cell">
<span class="flap-half flap-top"><span>O</span></span>
<span class="flap-half flap-bottom"><span>O</span></span>
<span class="flap-half flap-top flap-moving" hidden><span></span></span>
</span>
<span class="flap-cell">
<span class="flap-half flap-top"><span>N</span></span>
<span class="flap-half flap-bottom"><span>N</span></span>
<span class="flap-half flap-top flap-moving" hidden><span></span></span>
</span>
<span class="flap-cell">
<span class="flap-half flap-top"><span>D</span></span>
<span class="flap-half flap-bottom"><span>D</span></span>
<span class="flap-half flap-top flap-moving" hidden><span></span></span>
</span>
<span class="flap-cell">
<span class="flap-half flap-top"><span>O</span></span>
<span class="flap-half flap-bottom"><span>O</span></span>
<span class="flap-half flap-top flap-moving" hidden><span></span></span>
</span>
<span class="flap-cell">
<span class="flap-half flap-top"><span>N</span></span>
<span class="flap-half flap-bottom"><span>N</span></span>
<span class="flap-half flap-top flap-moving" hidden><span></span></span>
</span>
</div>
<output class="flap-status" aria-live="polite">LONDON</output>
<button class="demo-button" type="button" data-flap-next>下一站</button>
</div>
</main>
<script>
const reduced = matchMedia('(prefers-reduced-motion: reduce)');
const destinations = ["LONDON","BERLIN","BOSTON"];
const alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
const board = document.querySelector('.flap-board');
const cells = Array.from(board.children, (cell) => {
const [top, bottom, moving] = cell.children;
return { top: top.firstElementChild, bottom: bottom.firstElementChild, moving, face: moving.firstElementChild };
});
const next = document.querySelector('[data-flap-next]');
const status = document.querySelector('.flap-status');
const liveAnimations = new Set();
let current = 0;
let pending = 0;
function wait(duration) {
return new Promise((resolve) => setTimeout(resolve, duration));
}
function setLetter(cell, letter) {
cell.top.textContent = letter;
cell.bottom.textContent = letter;
cell.moving.hidden = true;
}
async function swing(cell, transforms, easing) {
const animation = cell.moving.animate(transforms.map((transform) => ({ transform })), {
duration: 61, easing,
});
liveAnimations.add(animation);
try {
await animation.finished;
} catch {
// A new motion preference or destination can finish this flip early.
} finally {
liveAnimations.delete(animation);
}
}
async function animateCell(cell, letter, index, sequence) {
await wait(index * 46);
const previous = alphabet.indexOf(cell.top.textContent);
const destination = alphabet.indexOf(letter);
const distance = (destination - previous + alphabet.length) % alphabet.length;
const steps = Math.min(distance, 4);
for (let step = 1; step <= steps; step += 1) {
if (sequence !== pending || reduced.matches) return;
const glyph = alphabet[(previous + Math.ceil(distance * step / steps)) % alphabet.length];
cell.face.textContent = cell.bottom.textContent;
cell.moving.classList.replace('flap-bottom', 'flap-top');
cell.moving.hidden = false;
cell.top.textContent = glyph;
await swing(cell, ['rotateX(0deg)', 'rotateX(-92deg)'], 'ease-in');
if (sequence !== pending || reduced.matches) return;
cell.face.textContent = glyph;
cell.moving.classList.replace('flap-top', 'flap-bottom');
await swing(cell, ['rotateX(92deg)', 'rotateX(0deg)'], 'ease-out');
if (sequence !== pending || reduced.matches) return;
setLetter(cell, glyph);
}
}
function settle() {
pending += 1;
liveAnimations.forEach((animation) => animation.cancel());
const word = destinations[current];
cells.forEach((cell, index) => setLetter(cell, word[index]));
status.textContent = word;
board.removeAttribute('aria-busy');
}
next.addEventListener('click', async () => {
settle();
current = (current + 1) % destinations.length;
if (reduced.matches) {
settle();
return;
}
const sequence = pending;
const word = destinations[current];
board.setAttribute('aria-busy', 'true');
await Promise.all(cells.map((cell, index) => animateCell(cell, word[index], index, sequence)));
if (sequence === pending) settle();
});
reduced.addEventListener('change', settle);
</script>
</body>
</html>UI Skills 原创示例