打字机效果
Typewriter Effect
文字像正在输入一样逐字出现,末尾常带有光标。
适用场景
让简短的首屏标语、提示词示例或终端命令逐字出现。 把逐字动画放在固定标题旁的一行短句上,让访客在动画播放时就能理解页面主题。
应用示例
命令行备份工具首次展示示例终端时,逐字输入「backup run ~/photos」。整条命令随后保留在屏幕上,方便访客复制语法。
也叫
- 打字动画
AI 提示词
给【目标短句】加上 Typewriter Effect。按完整可见字符逐字显示,为完整句子预留布局空间,让辅助技术一次读完。需要循环时提供暂停控件。减少动态效果模式下直接显示完整文字。
示例代码
保存为 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;
}
.terminal {
display: flex;
align-items: center;
gap: 9px;
width: min(100%, 232px);
padding: 17px;
border: 1px solid #63557f;
border-radius: 12px;
background: #242034;
color: #e9deff;
font: 650 clamp(12px, 5vw, 17px)/1.4 ui-monospace, monospace;
}
.terminal-prompt { color: #adf0cb; }
.typed-line {
box-sizing: content-box;
width: 12ch;
overflow: hidden;
white-space: nowrap;
border-right: 2px solid #adf0cb;
animation: type-line 4.1s steps(12, end) infinite;
}
@keyframes type-line {
0%, 5%, 100% { width: 12ch; }
6%, 14% { width: 0; }
58% { width: 12ch; }
}
.loop-example {
display: grid;
gap: 12px;
justify-items: center;
width: min(100%, 290px);
}
.loop-example [data-motion] { animation-play-state: paused; }
.loop-example[data-playing] [data-motion] { animation-play-state: running; }
@media (prefers-reduced-motion: reduce) {
.loop-example [data-motion] { animation: none; }
.loop-example [data-loop-toggle] { display: none; }
}
</style>
</head>
<body>
<main class="demo">
<div class="loop-example">
<div class="terminal">
<span class="terminal-prompt" aria-hidden="true">></span>
<span class="typed-line" data-motion>Type it out.</span>
</div>
<button class="demo-button" type="button" data-loop-toggle data-testid="effect-demo-loop-toggle">播放演示</button>
</div>
</main>
<script>
const example = document.querySelector('.loop-example');
const toggle = example.querySelector('[data-loop-toggle]');
toggle.addEventListener('click', () => {
const playing = example.toggleAttribute('data-playing');
toggle.textContent = playing ? "暂停演示" : "播放演示";
});
</script>
</body>
</html>UI Skills 原创示例