轮换词语
Rotating Words
一个完整的词滑出文字槽位,另一个词接着滑进来。
适用场景
在一条短标题里展示几个相关的角色或能力。 选能接在同一个句式里的词,按最长的词留出轮换宽度。
应用示例
预约工具固定「管理你的」这几个字,让「工作坊」「导览行程」「私教课程」依次滑进同一个位置。下方预约按钮在轮换过程中一直保持原位。
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;
}
.rotating-phrase { display: flex; gap: .32em; font-size: clamp(18px, 9vw, 26px); font-weight: 700; }
.word-slot { height: 1.25em; line-height: 1.25; overflow: hidden; color: #cdb8ff; }
.word-slot span { display: grid; animation: words 5.4s ease-in-out infinite; }
.word-slot b { display: block; min-height: 1.25em; }
@keyframes words {
0%, 24% { transform: translateY(0); }
33%, 57% { transform: translateY(-25%); }
66%, 90% { transform: translateY(-50%); }
100% { transform: translateY(-75%); }
}
.motion-example { display: grid; justify-items: center; width: 100%; gap: 10px; }
.motion-specimen {
display: grid;
place-items: center;
width: min(100%, 295px);
height: 108px;
position: relative;
isolation: isolate;
}
.motion-example [data-motion] { animation-play-state: paused; }
.motion-example[data-playing] [data-motion] { animation-play-state: running; }
@media (prefers-reduced-motion: reduce) {
.motion-example [data-motion] { animation: none; }
.motion-example [data-loop-toggle] { display: none; }
}
</style>
</head>
<body>
<main class="demo">
<div class="motion-example">
<div class="motion-specimen" aria-hidden="true"><p class="rotating-phrase">开始<span class="word-slot"><span data-motion><b>构思</b><b>设计</b><b>制作</b><b>构思</b></span></span></p></div>
<button class="demo-button" type="button" data-loop-toggle data-testid="effect-demo-loop-toggle">播放演示</button>
</div>
</main>
<script>
const example = document.querySelector('.motion-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 原创示例