公告跑马灯CSS:无缝滚动+悬停暂停(附完整代码)
一条顶部公告栏:橙点脉冲 + 文字横向滚动 + 右侧关闭按钮。纯 CSS 写动画,只用一小段 JS 处理关闭。下面每块都能点「预览效果」直接看,复制走改文字就能用。
原版 · 横向滚动公告
<style>
.nt{display:flex;align-items:center;gap:10px;max-width:560px;padding:10px 12px;border-radius:10px;
background:linear-gradient(135deg,#fffaf0,#feebc8);border:1px solid #fbd38d}
.dot{width:8px;height:8px;border-radius:50%;background:#ed8936;flex:none;
box-shadow:0 0 0 0 rgba(237,137,54,.6);animation:pulse 1.8s infinite}
@keyframes pulse{70%{box-shadow:0 0 0 8px rgba(237,137,54,0)}100%{box-shadow:0 0 0 0 rgba(237,137,54,0)}}
.vp{flex:1;overflow:hidden}
.mv{white-space:nowrap;font-size:13px;color:#975a16;animation:mv 14s linear infinite}
@keyframes mv{from{transform:translateX(100%)}to{transform:translateX(-100%)}}
.nt button{border:none;background:transparent;color:#975a16;font-size:18px;cursor:pointer;
line-height:1;padding:0 2px;opacity:.6;transition:.2s}
.nt button:hover{opacity:1;transform:rotate(90deg)}
</style>
<div class="nt" id="nt">
<span class="dot"></span>
<div class="vp"><div class="mv">🎉 新版组件库已上线 · 支持一键复制代码 · 每周三更新布局技巧 ·</div></div>
<button onclick="document.getElementById('nt').style.display='none'">×</button>
</div>这份原版有几处问题,我实测确认过:
- 不是真正的无缝循环:
translateX(100%) → -100%是一次性的"进场—出场",文字走完会瞬间跳回右边重来。实测一个周期里 56% 的时间文字露出不足半屏,开头结尾有明显空窗。 - 鼠标悬停不能暂停:实测悬停 1 秒,文字照样移动了 80px,想停下来读完很难受。
- 关闭按钮用了内联
onclick:我做了对照实验,站点一旦开启 CSP(比如script-src 'self'),浏览器直接报Executing inline event handler violates...,按钮点了没反应。 - 关闭后刷新又冒出来:没有记录状态,用户关一次它就再弹一次。
- 没有
prefers-reduced-motion:系统开了「减少动态效果」(晕动症用户常开)也不会停。 - 关闭按钮缺
aria-label:屏幕阅读器只会读到一个「×」。
修复版 · 无缝循环 + 悬停暂停
<style>
/* 公告跑马灯 · 修复版 */
.nt{display:flex;align-items:center;gap:10px;max-width:560px;padding:10px 12px;border-radius:10px;
background:linear-gradient(135deg,#fffaf0,#feebc8);border:1px solid #fbd38d}
.nt.is-hidden{display:none}
.dot{width:8px;height:8px;border-radius:50%;background:#ed8936;flex:none;
box-shadow:0 0 0 0 rgba(237,137,54,.6);animation:pulse 1.8s infinite}
@keyframes pulse{70%{box-shadow:0 0 0 8px rgba(237,137,54,0)}100%{box-shadow:0 0 0 0 rgba(237,137,54,0)}}
.vp{flex:1;overflow:hidden}
/* 无缝的关键在这一行:
width:max-content 让盒子宽 = 两份文字的总宽,位移 -50% 正好等于一份;
min-width:200% 是保险——文字太短时强行把盒子撑到容器的两倍,
否则两份加起来还没容器宽,就会露出空白(这是最常踩的坑)。 */
.mv{display:flex;width:max-content;min-width:200%;
font-size:13px;color:#975a16;animation:mv 16s linear infinite}
.mv span{flex:1 0 auto;padding-right:3rem}
@keyframes mv{to{transform:translateX(-50%)}}
/* 鼠标移入暂停,方便读完 */
.nt:hover .mv,.nt:hover .dot{animation-play-state:paused}
.nt button{border:none;background:transparent;color:#975a16;font-size:18px;cursor:pointer;
line-height:1;padding:0 2px;opacity:.6;transition:.2s}
.nt button:hover{opacity:1;transform:rotate(90deg)}
/* 系统开启"减少动态效果"时停止动画 */
@media (prefers-reduced-motion:reduce){
.mv,.dot{animation:none}
.mv{transform:none}
}
</style>
<div class="nt" id="nt">
<span class="dot"></span>
<div class="vp">
<div class="mv">
<span>🎉 新版组件库已上线 · 支持一键复制代码 · 每周三更新布局技巧 ·</span>
<span>🎉 新版组件库已上线 · 支持一键复制代码 · 每周三更新布局技巧 ·</span>
</div>
</div>
<button type="button" aria-label="关闭公告">×</button>
</div>
<script>
/* 用事件监听代替内联 onclick:开启 CSP 的站点也能用 */
(function () {
var nt = document.getElementById('nt');
var btn = nt.querySelector('button');
var KEY = 'nt-closed';
// 沙箱里没有 localStorage 权限,包一层避免报错
function read(){ try { return localStorage.getItem(KEY) === '1'; } catch(e){ return false; } }
function write(v){ try { localStorage.setItem(KEY, v ? '1' : '0'); } catch(e){} }
if (read()) nt.classList.add('is-hidden');
btn.addEventListener('click', function () {
nt.classList.add('is-hidden');
write(true);
});
})();
</script>关键改动是无缝的写法:文字写两份,width:max-content 让盒子宽等于两份总宽,动画只位移 -50%——正好等于一份,第二份接上第一份,看不到接缝。实测整个周期里文字始终铺满视口(覆盖率 100%),对比原版 56% 的空窗,观感差别很明显。
这里必须配 min-width:200%,否则只在"一份文字比容器宽"时才成立。我做过对照:文字换成「短消息」后,只写 max-content 的版本覆盖率掉到 108/509,露出大片空白;加上 min-width:200% 后,短、中、长、超长四种文字长度实测覆盖率都是 100%,且"位移半程 = 单份宽度",接缝每次都对得上。
另外:悬停暂停、CSS 里加了 prefers-reduced-motion 分支、按钮补了 aria-label、关闭改用事件监听并记住状态(用 try/catch 包住 localStorage,避免在 iframe 沙箱里报错)。
顺带一提,这里用 width:max-content 而不是 fit-content:fit-content 会被容器宽度截断,文字很长时盒子宽度卡在容器宽、内容却溢出,-50% 就对不上一份的宽度了。
深色款 · 适合暗色主题
<style>
/* 公告跑马灯 · 深色款 */
.nt{display:flex;align-items:center;gap:10px;max-width:560px;padding:10px 12px;border-radius:10px;
background:linear-gradient(135deg,#161b22,#1c2430);border:1px solid #30363d}
.nt.is-hidden{display:none}
.dot{width:8px;height:8px;border-radius:50%;background:#3fb950;flex:none;
box-shadow:0 0 0 0 rgba(63,185,80,.6);animation:pulse 1.8s infinite}
@keyframes pulse{70%{box-shadow:0 0 0 8px rgba(63,185,80,0)}100%{box-shadow:0 0 0 0 rgba(63,185,80,0)}}
.vp{flex:1;overflow:hidden}
.mv{display:flex;width:max-content;min-width:200%;font-size:13px;color:#c9d1d9;animation:mv 16s linear infinite}
.mv span{flex:1 0 auto;padding-right:3rem}
@keyframes mv{to{transform:translateX(-50%)}}
.nt:hover .mv,.nt:hover .dot{animation-play-state:paused}
.nt button{border:none;background:transparent;color:#8b949e;font-size:18px;cursor:pointer;
line-height:1;padding:0 2px;opacity:.6;transition:.2s}
.nt button:hover{opacity:1;transform:rotate(90deg)}
@media (prefers-reduced-motion:reduce){.mv,.dot{animation:none}.mv{transform:none}}
</style>
<div style="background:#0d1117;padding:14px;border-radius:8px">
<div class="nt" id="nt2">
<span class="dot"></span>
<div class="vp">
<div class="mv">
<span>🎉 新版组件库已上线 · 支持一键复制代码 · 每周三更新布局技巧 ·</span>
<span>🎉 新版组件库已上线 · 支持一键复制代码 · 每周三更新布局技巧 ·</span>
</div>
</div>
<button type="button" aria-label="关闭公告">×</button>
</div>
</div>
<script>
(function () {
var nt = document.getElementById('nt2'), btn = nt.querySelector('button');
btn.addEventListener('click', function () { nt.classList.add('is-hidden'); });
})();
</script>深灰底 + 绿点,配色照着 GitHub 暗色调来的,放在深色页头里不突兀。外面那层 <div style="background:#0d1117..."> 只是为了在预览里衬出深色效果,你用时删掉,直接用你页面本身的深色背景。
多条公告 · 垂直轮播
文字太长或者想轮播好几条,横向滚动就不合适了。这版改成上下切换,纯 CSS 无 JS:
<style>
/* 多条公告 · 垂直轮播 */
.nt2{display:flex;align-items:center;gap:10px;max-width:560px;padding:10px 12px;border-radius:10px;
background:linear-gradient(135deg,#ebf8ff,#bee3f8);border:1px solid #90cdf4}
.nt2 .ic{flex:none;font-size:14px}
.vp2{flex:1;height:20px;overflow:hidden;position:relative}
/* 4 条公告 → 位移 -400%,每条停留 3s */
.ul2{margin:0;padding:0;list-style:none;animation:up 12s steps(4) infinite}
.ul2 li{height:20px;line-height:20px;font-size:13px;color:#2c5282;
white-space:nowrap;overflow:hidden;text-overflow:ellipsis}
@keyframes up{to{transform:translateY(-400%)}}
.nt2:hover .ul2{animation-play-state:paused}
@media (prefers-reduced-motion:reduce){.ul2{animation:none;height:20px;overflow:visible}.vp2{height:auto}}
</style>
<div class="nt2">
<span class="ic">📢</span>
<div class="vp2">
<ul class="ul2">
<li>新版组件库已上线,支持一键复制代码</li>
<li>每周三更新布局技巧,记得来看</li>
<li>所有代码免费开放,仅供学习交流</li>
<li>遇到问题可在评论区留言</li>
</ul>
</div>
</div>用 steps(4) 让切换是"跳"而不是"滑",一条停 3 秒。改条数要同时改两个数:steps(4) 和 -400% 里的数字(4 条 → 400%),总时长 12s = 4×3s 也要跟着调。
改的时候注意这几点
- 文字要写两份,改的时候两处都改:无缝靠两份相同内容首尾相接。只改一处会出现"新旧文字交替"的穿帮。
- 别删
min-width:200%:这是短文字时的保险。删掉后如果一份文字比容器窄,就会露出空白(实测覆盖率掉到 108/509)。 - 改速度调
16s:数值越大越慢。文字越长建议调越慢,否则看不清。 - 调两条公告的间距:
.mv span{padding-right:3rem},太小会挤在一起。 - 加链接要小心:滚动文字上挂
<a>,鼠标很难点中(它在移动)。想放链接建议用上面「垂直轮播」那版,静止时好点。 - 别用内联
onclick:CSP 下会失效(实测报Executing inline event handler violates)。用修复版里的addEventListener。 - 关闭状态存在
localStorage:用户关过就不再弹。想每次都显示,把那段 JS 里的read()/write()删掉即可。
怎么用
复制整块 → WordPress 后台添加「自定义 HTML」区块粘贴(经典编辑器切「文本」模式)→ 更新。想全站显示就放进 header.php,只在首页显示就放进 front-page.php(WordPress)或 index.php(Typecho)。
CSS 和 JS 也可以拆开:CSS 进「外观 → 自定义 → 额外 CSS」,JS 部分没有的话(垂直轮播那版)就只用 CSS。
注意别把 body{...} 这类全局规则带进你的站点。 早先几个版本我为了在预览里好看,代码块开头写了 body{margin:0;padding:16px;background:#fff;font-family:...}——那只是演示用的画布,你已经拿掉了。读者如果从别处拷到带 body{} 的版本,会把你整站的背景、字体、内边距一起改掉(实测:灰底 #f2f4f8 被刷成白底、Georgia 被换成苹方、内边距从 0 变 16px)。本文现在四块代码里都不含 body{},只有深色款外面那层 <div style="background:#0d1117..."> 是预览衬底,用时删掉。
代码仅供学习交流,禁止商用。更多网站美化代码,前往 维形筑网 www.wxzcw.com。
作者:Flechazo
如若转载,请注明出处:维形筑网 https://wxzcw.com