YL

SCROLL EFFECT

无缝跑马灯Infinite Marquee

一排内容匀速循环滚动,接缝完全看不出来。两端渐隐是点睛之笔。

中英对照

中文
无缝跑马灯
英文
Infinite Marquee
别名
无缝滚动、循环滚动带、走马灯
一句话
一排内容匀速循环滚动,接缝完全看不出来。两端渐隐是点睛之笔。

效果预览

◆ Nova● Orbit▲ Summit✦ Lumen■ Forge◐ Pulse⬢ Hexa✚ Vertex
两端渐隐 · 悬停暂停

常见变体

双排反向Dual-row Opposite Marquee

两条轨道一正一反、速度错开,密度更高,适合关键词带;共用一个暂停按钮。

适合用在哪

Logo 墙、客户评价滚动带、关键词标签云。

不适合:必须被读完或点到的信息(服务中断公告、价格变动、带操作链接的通知):内容一直在移动,用户读到一半就被带走,想点的链接也在跑;Logo 只有 4-5 个、一行放得下时也不需要滚动,静态 Logo 墙更清楚。

和相近模式的区别

横向滚动区Horizontal Scroll Section

横向滚动区静止不动、由用户自己滑,适合每一项都值得点开的卡片;跑马灯自己匀速移动、用户什么都不用做,适合不需要逐个阅读的「数量感」展示,比如客户 Logo、技能关键词。跑马灯里不要放需要点击的主要入口,移动中的目标很难点中。

Logo 跑马灯Logo Marquee

Logo 跑马灯是本模式在社会证明区的具体用法:只放灰度统一的客户 Logo、间距 48-64px,并建议移动端减速;本条讲通用的实现原理(两份等宽内容、-50% 衔接、暂停控制),同样适用于评价卡片带和关键词带。

容易踩的坑

  • 两份内容宽度不完全一致或 gap 未计入,循环到 -50% 时出现明显跳帧。
  • 内容少、容器宽时每份比容器还窄,滚动中右侧会露出空白;每份至少要与容器等宽(如 min-width:100cqw)。
  • 持续滚动超过 5 秒却不提供暂停,违反 WCAG 2.2.2,干扰注意力障碍用户。
  • 复制的那一份没加 aria-hidden="true",读屏会把所有内容重复念两遍。

设计提示词 · Prompt

无缝跑马灯(Infinite Marquee):内容复制两份放进一条 flex 轨道(width:max-content,轨道本身不设 gap;每份内容自己用 gap 排列,并在末尾加与 gap 相等的 padding-right,保证两份宽度完全相同),动画 translateX(0→-50%) 匀速循环(20-30s linear infinite)——因为两份内容等宽,-50% 恰好无缝衔接。两端加 mask-image:linear-gradient(90deg,transparent,#000 12%,#000 88%,transparent) 渐隐。悬停或键盘聚焦时 animation-play-state:paused,并提供一个可见的「暂停滚动」按钮(WCAG 2.2.2:自动播放超过 5 秒的移动内容必须可暂停),减弱动效时停止滚动并让内容换行铺开;复制的那一份加 aria-hidden="true" 与 inert。变体:上下两条反向滚、鼠标位置控制滚动速度。内容必须首尾相接等宽,否则会跳;每份内容还要至少与容器等宽(内容少时给容器加 container-type:inline-size,每份设 min-width:100cqw 并用 justify-content:space-around 均匀铺开),否则宽屏下右侧会露出空白。
Infinite Marquee: duplicate the content twice inside one flex track (width:max-content, no gap on the track itself; each copy lays out its items with gap and adds a padding-right equal to that gap, so both copies are exactly the same width), animate translateX(0→-50%) at constant speed in a loop (20-30s linear infinite) — because both copies are equally wide, -50% joins seamlessly. Add mask-image:linear-gradient(90deg,transparent,#000 12%,#000 88%,transparent) at both ends to fade out. Pause with animation-play-state:paused on hover and keyboard focus, and provide a visible "Pause" button (WCAG 2.2.2: auto-playing motion longer than 5 seconds must be pausable); under reduced motion stop scrolling and let the items wrap; mark the duplicate copy aria-hidden="true" and inert. Variants: two rows scrolling in opposite directions, mouse position controlling scroll speed. The content must join end to end with equal width, otherwise it jumps; each copy must also be at least as wide as the container (with few items, set container-type:inline-size on the container, give each copy min-width:100cqw and spread the items with justify-content:space-around), otherwise a blank gap shows on the right on wide screens.

可复制 CSS

自包含的最小实现,粘贴到空白页面即可生效;类名可以按项目规范改名。

/* <div class="marquee" role="region" aria-label="合作客户">
     <button class="marquee__toggle" type="button" aria-pressed="false">暂停滚动</button>
     <div class="marquee__viewport">
       <div class="marquee__track">
         <ul class="marquee__group"><li>…</li></ul>
         <ul class="marquee__group" aria-hidden="true" inert><li>…</li></ul>(与上一组完全相同)
       </div>
     </div>
   </div>
   按钮切换 .marquee 的 .is-paused 和自身的 aria-pressed */
.marquee { --marquee-gap: 48px; }
.marquee__viewport {
  overflow: hidden;
  container-type: inline-size;
  -webkit-mask-image: linear-gradient(90deg, transparent, #000 12%, #000 88%, transparent);
  mask-image: linear-gradient(90deg, transparent, #000 12%, #000 88%, transparent);
}
.marquee__track {
  display: flex;
  width: max-content;
  animation: marquee-scroll 24s linear infinite;
}
/* 间距写成每组的右内边距,两组宽度完全相等,-50% 正好是一组;
   每组至少与视口等宽(100cqw),内容少时用 space-around 均匀铺开,宽屏右侧不露空白。
   不支持容器单位的浏览器会忽略 min-width,退回按内容宽度循环 */
.marquee__group {
  display: flex;
  flex-shrink: 0;
  align-items: center;
  justify-content: space-around;
  gap: var(--marquee-gap);
  min-width: 100cqw;
  margin: 0;
  padding: 0 var(--marquee-gap) 0 0;
  list-style: none;
}
.marquee__viewport:hover .marquee__track,
.marquee__viewport:focus-within .marquee__track,
.marquee.is-paused .marquee__track { animation-play-state: paused; }
.marquee__toggle {
  height: 32px;
  margin-bottom: 12px;
  padding: 0 12px;
  border: 1px solid #d1d5db;
  border-radius: 8px;
  background: #fff;
  color: #111827;
  font: 500 13px/1 system-ui, sans-serif;
  cursor: pointer;
}
.marquee__toggle[aria-pressed="true"] { background: #f3f4f6; }
.marquee__toggle:focus-visible { outline: 2px solid #6366f1; outline-offset: 2px; }
@keyframes marquee-scroll {
  to { transform: translateX(-50%); }
}
@media (prefers-reduced-motion: reduce) {
  .marquee__track { width: auto; animation: none; }
  .marquee__group { flex-shrink: 1; flex-wrap: wrap; justify-content: center; row-gap: 16px; padding-right: 0; }
  .marquee__group[aria-hidden="true"] { display: none; }
  .marquee__viewport { -webkit-mask-image: none; mask-image: none; }
  .marquee__toggle { display: none; }
}

演示 HTML 结构

<div class="s d-marquee">
  <div class="d-marquee__track">
    <span class="d-marquee__set"><span class="d-marquee__chip">◆ Nova</span><span class="d-marquee__chip">● Orbit</span><span class="d-marquee__chip">▲ Summit</span><span class="d-marquee__chip">✦ Lumen</span><span class="d-marquee__chip">■ Forge</span><span class="d-marquee__chip">◐ Pulse</span><span class="d-marquee__chip">⬢ Hexa</span><span class="d-marquee__chip">✚ Vertex</span></span><span class="d-marquee__set" aria-hidden="true" inert><span class="d-marquee__chip">◆ Nova</span><span class="d-marquee__chip">● Orbit</span><span class="d-marquee__chip">▲ Summit</span><span class="d-marquee__chip">✦ Lumen</span><span class="d-marquee__chip">■ Forge</span><span class="d-marquee__chip">◐ Pulse</span><span class="d-marquee__chip">⬢ Hexa</span><span class="d-marquee__chip">✚ Vertex</span></span>
  </div>
  <span class="d-marquee__cap">两端渐隐 · 悬停暂停<button class="d-pausebtn" type="button" data-demo="pause" aria-pressed="false">暂停</button></span>
</div>

无障碍与性能

无障碍

  • WCAG SC 2.2.2:跑马灯自动开始、持续超过 5 秒,又和其他内容同屏,必须提供暂停、停止或隐藏的方式。悬停暂停只照顾到鼠标用户,触屏和键盘用户需要一个可见的「暂停滚动」按钮;按钮文字保持不变,用 aria-pressed 表示是否已暂停。prefers-reduced-motion 只是额外的系统级开关,不能代替这个按钮。
  • 复制的那一组同时加 aria-hidden="true" 和 inert:aria-hidden 让读屏跳过,inert 让组里的链接不能被 Tab 聚焦,避免键盘焦点落进一组被读屏忽略的元素。inert 在主流浏览器均已支持。
  • 减弱动效时停止滚动、去掉复制组并让内容换行铺开,所有项都能看到,而不是停在只露出一半的位置;这时没有运动,暂停按钮也一起隐藏。

性能

  • 整条轨道只有一个 transform 动画,可以交给合成线程;不要用 JS 定时器每帧改 left,或者把第一项移到队尾,那样每帧都要重排。
  • 渐隐遮罩写在不动的 .marquee__viewport 上,动画只发生在里面的轨道;不需要在两端叠渐变色块,也就不用关心背景色和暗色主题。

常见问题

无缝跑马灯的英文是什么?

无缝跑马灯的英文是 Infinite Marquee。

Infinite Marquee 的中文是什么?

Infinite Marquee 的中文是「无缝跑马灯」。常见中文别名还包括 无缝滚动、循环滚动带、走马灯。一排内容匀速循环滚动,接缝完全看不出来。两端渐隐是点睛之笔。

跑马灯的速度怎么定?

按每秒移动的像素来想,而不是只看总时长:时长相同,内容越长移动越快。本条 24 秒走完一组,一组宽 1200px 时约 50px/秒,经验上 Logo 带在 30-60px/秒之间比较舒服。内容长度不固定时,用 JS 读出一组的宽度,按「宽度 ÷ 目标速度」算出 animation-duration 写回去。

内容不够一屏宽,跑马灯会露出空白怎么办?

一组的宽度必须不小于容器宽度:轨道最多平移一组的宽度,平移到接近一组时,右侧露出的就是第二组的内容,如果一组比容器窄,第二组的末尾之后就是空白。内容太少时,在每一组里把内容重复多次(比如 5 个 Logo 重复成 10 个算一组),两组仍要完全一样。上面的 CSS 还给视口加了 container-type: inline-size、给每组加了 min-width: 100cqw 和 justify-content: space-around,组比视口窄时自动拉到视口宽并均匀铺开,接缝处的间距与组内一致;不支持容器单位的旧浏览器会忽略这一行,所以内容很少时仍建议同时重复内容兜底。

跑马灯用 CSS 动画还是 JS 做?

匀速循环用 CSS @keyframes 就够:transform 动画可以在合成线程运行,主线程忙时也不容易掉帧,暂停只要改 animation-play-state。只有「速度跟随鼠标位置」「拖拽甩动」这类变体才需要 JS:在 requestAnimationFrame 里累加位移,超过一组宽度时减去一组宽度,再写进 transform。暂停按钮只需要几行 JS:点击时切换 .marquee 的 .is-paused,并同步按钮的 aria-pressed。

常见搭配

同一页面里经常和「无缝跑马灯」一起出现的模式,可以收藏后用「组合收藏为整页提示词」一起交给 AI。

标签