横向滑动卡片:侧栏热门内容左右滑

侧栏空间有限,热门内容一排排放不下时,做成横向滑动是个好办法——左右箭头控制,也能直接拖。比纵向列表省地方,信息密度还高。

你给的这段只有 HTML 结构,样式和脚本都没给,我按它的类名补全了一套,补全后才把问题暴露出来。下面是补全版和修复版,每块都能点「预览效果」直接看。

原版 · 补全后的实际效果

HTML横向滑动卡片 · 原版(补全 CSS/JS 后)原版·有问题
<style>
.c3{max-width:460px;font-family:system-ui,-apple-system,"PingFang SC",sans-serif}
.c3-h{display:flex;justify-content:space-between;align-items:baseline;margin-bottom:9px}
.c3-h b{font-size:13px;color:#141a24}
.c3-h span{font-size:11px;color:#94a3b8}
.c3-v{overflow-x:auto}
.c3-tk{display:flex;gap:10px}
.c3-c{flex:none;width:120px;border:1px solid #edf2f7;border-radius:10px;padding:8px}
.c3-i{height:60px;border-radius:7px;margin-bottom:6px}
.c3-i.a{background:linear-gradient(135deg,#667eea,#764ba2)}
.c3-i.b{background:linear-gradient(135deg,#f093fb,#f5576c)}
.c3-i.c{background:linear-gradient(135deg,#4facfe,#00f2fe)}
.c3-i.d{background:linear-gradient(135deg,#43e97b,#38f9d7)}
.c3-i.e{background:linear-gradient(135deg,#fa709a,#fee140)}
.c3-c b{display:block;font-size:12.5px;color:#1a202c;margin-bottom:3px}
.c3-c span{font-size:11px;color:#a0aec0}
.c3-b{display:flex;gap:6px;margin-top:9px}
.c3-b button{width:28px;height:28px;border-radius:7px;border:1px solid #e2e8f0;
  background:#fff;cursor:pointer;font-size:15px;color:#64748b}
</style>

<div class="c3">
  <div class="c3-h"><b>本周热门</b><span>滑动查看</span></div>
  <div class="c3-v" id="c3v">
    <div class="c3-tk" id="c3t">
      <div class="c3-c"><div class="c3-i a"></div><b>鼠标跟随粒子</b><span>1.2k 阅读</span></div>
      <div class="c3-c"><div class="c3-i b"></div><b>滚动条美化</b><span>980 阅读</span></div>
      <div class="c3-c"><div class="c3-i c"></div><b>标签云组件</b><span>1.5k 阅读</span></div>
      <div class="c3-c"><div class="c3-i d"></div><b>噪点质感背景</b><span>760 阅读</span></div>
      <div class="c3-c"><div class="c3-i e"></div><b>备案徽章美化</b><span>640 阅读</span></div>
    </div>
  </div>
  <div class="c3-b"><button onclick="c3g(-1)">‹</button><button onclick="c3g(1)">›</button></div>
</div>

<script>
function c3g(d){var v=document.getElementById('c3v');v.scrollLeft+=d*140;}
</script>

这是按原版 HTML 的结构补全样式与脚本后的真实效果 —— 不补全根本跑不起来(c3g 函数没定义)。补全后能滑动,但按钮有一堆问题:读屏只念出「‹」「›」、放在表单里会误提交、加了安全策略就彻底失效。详见下方正文实测。

补全后能滑,但有六个问题,我逐项实测过:

① c3g() 没定义,点了直接报错

原版 HTML 里写了 onclick="c3g(-1)",但没有给这个函数。实测点击:

```

c3g is not defined

```

控制台直接抛错,scrollLeft 纹丝不动。这段代码单独粘进去是跑不起来的,必须先自己写个 c3g。

② 内联 onclick 在加了安全策略的站点上会失效

很多站点为了安全会加 CSP(内容安全策略)。我做了对照实验——同样提供了 c3g 函数,只差一条 CSP 头:

点击右箭头后 scrollLeft
无 CSP 0 → 140 ✓
有 CSP(script-src 'self') 0 → 0 ✗

浏览器报的就是:

Executing inline event handler violates the following Content Security Policy directive

函数明明存在,却完全不执行。这种"本地能跑、上线失效"的问题最难查。

③ 按钮没有 aria-label,读屏只念出符号

按钮内容是 ‹ 和 ›。我抓了浏览器的可访问性树:

```

role=button name='‹'

role=button name='›'

```

可访问名就是这两个符号。读屏软件遇到它们,要么念"小于号""大于号",要么干脆跳过——视障用户完全不知道这是"上一页/下一页"。

④ 按钮没写 type,放进表单会误提交

<button> 的 type 默认是 submit。我实测把它包进一个 <form> 里点一下:

```

点击后 URL: ...?#submitted ← 表单被提交了

```

如果这个组件出现在搜索表单、订阅表单附近,点一下箭头就把表单提交了,页面直接跳转。

⑤ 滑到尽头没有禁用态

scrollLeft 已经到 0 了还能点左箭头,已经到最右了还能点右箭头。点了没反应,用户会以为坏了。

⑥ 卡片是 <div> 不是链接

又是"假卡片"——看着能点,实际点不动、Tab 不到、搜索引擎抓不到。这已经是这批代码里第六次出现同一类问题了。

还有几个小问题:滚动容器没有 scroll-snap,滑完停在半张卡的位置很别扭;容器 tabIndex 是 -1,键盘用户没法用方向键滚动;文字色 #a0aec0 对比度只有 2.26。

修复版 · 事件绑定 + 完整无障碍

HTML横向滑动卡片 · 修复版已修复
<style>
.sw2{max-width:460px;font-family:system-ui,-apple-system,"PingFang SC",sans-serif}
.sw2 .hd{display:flex;justify-content:space-between;align-items:baseline;
  margin-bottom:9px;gap:10px}
.sw2 .hd b{font-size:13px;color:#141a24}
.sw2 .hd .tip{font-size:11.5px;color:#64748b}              /* 4.76 ✓ */
/* scroll-snap:滑完自动对齐到卡片边缘,不会停在半张卡上 */
.sw2 .vp{overflow-x:auto;scroll-snap-type:x mandatory;
  scroll-behavior:smooth;-webkit-overflow-scrolling:touch;
  scrollbar-width:thin}
.sw2 .vp:focus-visible{outline:2px solid #2b6cb0;outline-offset:2px}
.sw2 .track{display:flex;gap:10px}
/* 真链接:可点击、可键盘聚焦、SEO 能抓到 */
.sw2 a.card{flex:none;width:120px;text-decoration:none;
  border:1px solid #edf2f7;border-radius:10px;padding:8px;
  scroll-snap-align:start;
  transition:border-color .2s,box-shadow .2s,transform .2s}
.sw2 a.card:focus-visible{outline:2px solid #2b6cb0;outline-offset:2px}
.sw2 a.card:hover{border-color:#c3dafe;box-shadow:0 6px 16px rgba(20,30,60,.08);
  transform:translateY(-2px)}
.sw2 .img{height:60px;border-radius:7px;margin-bottom:6px;
  background:var(--g,linear-gradient(135deg,#cbd5e0,#a0aec0));
  background-size:cover;background-position:center}
.sw2 .t{display:block;font-size:12.5px;font-weight:600;color:#1a202c;
  margin-bottom:3px;line-height:1.4}
.sw2 .m{font-size:11.5px;color:#64748b}                     /* 4.76 ✓ */
.sw2 .ctrl{display:flex;gap:6px;margin-top:10px;align-items:center}
.sw2 .ctrl button{width:30px;height:30px;border-radius:8px;
  border:1px solid #e2e8f0;background:#fff;color:#475569;
  cursor:pointer;font-size:15px;line-height:1;
  display:flex;align-items:center;justify-content:center;
  transition:background-color .18s,border-color .18s,opacity .18s}
.sw2 .ctrl button:hover:not(:disabled){background:#eff6ff;border-color:#c3dafe}
.sw2 .ctrl button:focus-visible{outline:2px solid #2b6cb0;outline-offset:2px}
/* 禁用态:滑到尽头变灰,用户知道到头了 */
.sw2 .ctrl button:disabled{opacity:.38;cursor:not-allowed}
@media (prefers-reduced-motion:reduce){
  .sw2 .vp{scroll-behavior:auto}
  .sw2 a.card{transition:none}
  .sw2 a.card:hover{transform:none}
}
</style>

<div class="sw2" id="sw2a">
  <div class="hd"><b>本周热门</b><span class="tip">滑动查看</span></div>
  <!-- tabindex=0 + role/aria-label:键盘用户能聚焦并用方向键滚动 -->
  <div class="vp" id="sw2vp" tabindex="0" role="region" aria-label="本周热门文章,可横向滚动">
    <div class="track">
      <a class="card" href="/?s=鼠标特效">
        <span class="img" aria-hidden="true" style="--g:linear-gradient(135deg,#667eea,#764ba2)"></span>
        <span class="t">鼠标跟随粒子</span><span class="m">1.2k 阅读</span></a>
      <a class="card" href="/?s=滚动条美化">
        <span class="img" aria-hidden="true" style="--g:linear-gradient(135deg,#f093fb,#f5576c)"></span>
        <span class="t">滚动条美化</span><span class="m">980 阅读</span></a>
      <a class="card" href="/?s=标签美化">
        <span class="img" aria-hidden="true" style="--g:linear-gradient(135deg,#4facfe,#00f2fe)"></span>
        <span class="t">标签云组件</span><span class="m">1.5k 阅读</span></a>
      <a class="card" href="/?s=纹理材质">
        <span class="img" aria-hidden="true" style="--g:linear-gradient(135deg,#43e97b,#38f9d7)"></span>
        <span class="t">噪点质感背景</span><span class="m">760 阅读</span></a>
      <a class="card" href="/?s=备案美化">
        <span class="img" aria-hidden="true" style="--g:linear-gradient(135deg,#fa709a,#fee140)"></span>
        <span class="t">备案徽章美化</span><span class="m">640 阅读</span></a>
    </div>
  </div>
  <!-- type="button":放进 form 也不会误提交;aria-label 给读屏 -->
  <div class="ctrl">
    <button type="button" aria-label="向左滚动" id="sw2prev">‹</button>
    <button type="button" aria-label="向右滚动" id="sw2next">›</button>
  </div>
</div>

<script>
(function () {
  var vp = document.getElementById('sw2vp');
  var pv = document.getElementById('sw2prev');
  var nx = document.getElementById('sw2next');
  if (!vp || !pv || !nx) return;
  // 用 addEventListener 而不是内联 onclick:不依赖全局函数,
  // 也不会被 CSP 的 script-src 拦掉。
  function step(dir) {
    var card = vp.querySelector('.card');
    var w = card ? card.getBoundingClientRect().width + 10 : 130;
    vp.scrollBy({ left: dir * w, behavior: 'smooth' });
  }
  pv.addEventListener('click', function () { step(-1); });
  nx.addEventListener('click', function () { step(1); });
  function sync() {
    var max = vp.scrollWidth - vp.clientWidth;
    // 留 2px 容差,避免缩放/小数误差导致按钮永远不禁用
    pv.disabled = vp.scrollLeft <= 2;
    nx.disabled = vp.scrollLeft >= max - 2;
  }
  vp.addEventListener('scroll', sync, { passive: true });
  addEventListener('resize', sync);
  sync();
})();
</script>

修复版:改用 addEventListener 绑事件(CSP 下照样生效)、按钮加 type 与 aria-label、滑到尽头自动禁用、卡片换成真链接、加 scroll-snap 对齐、滚动容器可键盘聚焦。试试点箭头,滑到最右看右箭头是否变灰。

改了这些,每项都有实测支撑:

改动 作用 实测
补全并改用 addEventListener 不依赖全局函数,CSP 下照样生效 CSP 环境 0→140 ✓(内联版 0→0 ✗)
按钮加 type="button" 放进表单不再误提交 实测 URL 不再跳转 ✓
按钮加 aria-label 读屏念"向左滚动/向右滚动" 可访问名 '‹' → '向左滚动' ✓
加 禁用态 滑到尽头按钮变灰 到头后 disabled 自动置位 ✓
卡片改 <a href> 可点击、可 Tab、SEO 能抓到 —
加 scroll-snap-type 滑完自动对齐卡片边缘,不停在半张 —
容器加 tabindex="0" + role/aria-label 键盘可聚焦,方向键能滚动 tabIndex -1 → 0 ✓
文字色 → #64748b 对比度达标 2.26 → 4.76 ✓
补 prefers-reduced-motion 尊重系统减弱动效(关掉平滑滚动) —

最值得一提的是第①条。我做了对照实验,同一个页面只差事件绑定方式:

写法 无 CSP 有 CSP(script-src 'self')
onclick="c3g(1)" 生效 失效,报 "Executing inline event handler violates..."
addEventListener 生效 生效 ✓

很多安全插件、CDN 防护都会加 CSP,内联事件是最先被拦的一类。改用 addEventListener 后,脚本还能顺带放进 .js 文件做缓存。

无按钮版 · 纯手势滑动

手机上其实不需要箭头按钮——直接滑更自然。这版去掉按钮,只靠 scroll-snap 对齐。

HTML横向滑动卡片 · 无按钮纯手势版无按钮版
<style>
.sw3{max-width:520px;font-family:system-ui,-apple-system,"PingFang SC",sans-serif}
.sw3 .hd{display:flex;justify-content:space-between;align-items:baseline;margin-bottom:9px}
.sw3 .hd b{font-size:13px;color:#141a24}
.sw3 .hd .tip{font-size:11.5px;color:#64748b}
.sw3 .vp{display:flex;gap:10px;overflow-x:auto;padding-bottom:6px;
  scroll-snap-type:x mandatory;-webkit-overflow-scrolling:touch;
  scrollbar-width:thin}
.sw3 .vp:focus-visible{outline:2px solid #2b6cb0;outline-offset:3px}
.sw3 a.card{flex:none;width:130px;text-decoration:none;scroll-snap-align:start;
  border:1px solid #edf2f7;border-radius:10px;padding:9px;
  transition:border-color .2s,box-shadow .2s}
.sw3 a.card:hover{border-color:#c3dafe;box-shadow:0 6px 16px rgba(20,30,60,.08)}
.sw3 .img{height:66px;border-radius:8px;margin-bottom:7px;
  background:var(--g,linear-gradient(135deg,#cbd5e0,#a0aec0))}
.sw3 .t{display:block;font-size:12.5px;font-weight:600;color:#1a202c;margin-bottom:3px}
.sw3 .m{font-size:11.5px;color:#64748b}
/* 右侧渐隐:暗示后面还有内容 */
.sw3 .wrap{position:relative}
.sw3 .wrap::after{content:"";position:absolute;right:0;top:0;bottom:6px;width:32px;
  pointer-events:none;background:linear-gradient(90deg,rgba(255,255,255,0),#fff)}
@media (prefers-reduced-motion:reduce){.sw3 a.card{transition:none}}
</style>

<div class="sw3">
  <div class="hd"><b>本周热门</b><span class="tip">← 左右滑动 →</span></div>
  <div class="wrap">
    <div class="vp" tabindex="0" role="region" aria-label="本周热门文章,可横向滚动">
      <a class="card" href="/?s=鼠标特效">
        <span class="img" aria-hidden="true" style="--g:linear-gradient(135deg,#667eea,#764ba2)"></span>
        <span class="t">鼠标跟随粒子</span><span class="m">1.2k 阅读</span></a>
      <a class="card" href="/?s=滚动条美化">
        <span class="img" aria-hidden="true" style="--g:linear-gradient(135deg,#f093fb,#f5576c)"></span>
        <span class="t">滚动条美化</span><span class="m">980 阅读</span></a>
      <a class="card" href="/?s=标签美化">
        <span class="img" aria-hidden="true" style="--g:linear-gradient(135deg,#4facfe,#00f2fe)"></span>
        <span class="t">标签云组件</span><span class="m">1.5k 阅读</span></a>
      <a class="card" href="/?s=纹理材质">
        <span class="img" aria-hidden="true" style="--g:linear-gradient(135deg,#43e97b,#38f9d7)"></span>
        <span class="t">噪点质感背景</span><span class="m">760 阅读</span></a>
      <a class="card" href="/?s=备案美化">
        <span class="img" aria-hidden="true" style="--g:linear-gradient(135deg,#fa709a,#fee140)"></span>
        <span class="t">备案徽章美化</span><span class="m">640 阅读</span></a>
    </div>
  </div>
</div>

无按钮版:去掉箭头,纯靠手指/触控板滑动,靠 scroll-snap 自动对齐。手机上更自然,也省了两个按钮的位置。试着左右拖动下面的卡片。

右侧那个渐隐遮罩是个小技巧:暗示"后面还有内容",比干巴巴一句提示更自然。注意它必须加 pointer-events:none,否则会挡住滑动。

深色款 · 配暗色主题

HTML横向滑动卡片 · 深色款深色款
<style>
.swd{max-width:460px;font-family:system-ui,-apple-system,"PingFang SC",sans-serif;
  background:#1a202c;padding:14px;border-radius:12px}
.swd .hd{display:flex;justify-content:space-between;align-items:baseline;margin-bottom:10px}
.swd .hd b{font-size:13px;color:#f7fafc}
.swd .hd .tip{font-size:11.5px;color:#a0aec0}              /* 7.23 ✓ */
.swd .vp{overflow-x:auto;scroll-snap-type:x mandatory;scroll-behavior:smooth;
  scrollbar-width:thin;scrollbar-color:#4a5568 #1a202c}
.swd .vp:focus-visible{outline:2px solid #90cdf4;outline-offset:2px}
.swd .track{display:flex;gap:10px}
.swd a.card{flex:none;width:120px;text-decoration:none;scroll-snap-align:start;
  background:#232a38;border:1px solid #2d3748;border-radius:10px;padding:8px;
  transition:border-color .2s,background-color .2s}
.swd a.card:hover{background:#2a3140;border-color:#3182ce}
.swd a.card:focus-visible{outline:2px solid #90cdf4;outline-offset:2px}
.swd .img{height:60px;border-radius:7px;margin-bottom:6px;
  background:var(--g,linear-gradient(135deg,#4a5568,#2d3748))}
.swd .t{display:block;font-size:12.5px;font-weight:600;color:#cbd5e0;margin-bottom:3px}  /* 10.98 ✓ */
.swd .m{font-size:11.5px;color:#a0aec0}                    /* 7.23 ✓ */
.swd .ctrl{display:flex;gap:6px;margin-top:10px}
.swd .ctrl button{width:30px;height:30px;border-radius:8px;border:1px solid #3a4457;
  background:#232a38;color:#cbd5e0;cursor:pointer;font-size:15px;line-height:1;
  display:flex;align-items:center;justify-content:center}
.swd .ctrl button:hover:not(:disabled){background:#2d3748;border-color:#3182ce}
.swd .ctrl button:focus-visible{outline:2px solid #90cdf4;outline-offset:2px}
.swd .ctrl button:disabled{opacity:.32;cursor:not-allowed}
@media (prefers-reduced-motion:reduce){.swd .vp{scroll-behavior:auto}}
</style>

<div class="swd">
  <div class="hd"><b>本周热门</b><span class="tip">滑动查看</span></div>
  <div class="vp" id="swdvp" tabindex="0" role="region" aria-label="本周热门文章,可横向滚动">
    <div class="track">
      <a class="card" href="/?s=鼠标特效">
        <span class="img" aria-hidden="true" style="--g:linear-gradient(135deg,#667eea,#764ba2)"></span>
        <span class="t">鼠标跟随粒子</span><span class="m">1.2k 阅读</span></a>
      <a class="card" href="/?s=滚动条美化">
        <span class="img" aria-hidden="true" style="--g:linear-gradient(135deg,#f093fb,#f5576c)"></span>
        <span class="t">滚动条美化</span><span class="m">980 阅读</span></a>
      <a class="card" href="/?s=标签美化">
        <span class="img" aria-hidden="true" style="--g:linear-gradient(135deg,#4facfe,#00f2fe)"></span>
        <span class="t">标签云组件</span><span class="m">1.5k 阅读</span></a>
      <a class="card" href="/?s=纹理材质">
        <span class="img" aria-hidden="true" style="--g:linear-gradient(135deg,#43e97b,#38f9d7)"></span>
        <span class="t">噪点质感背景</span><span class="m">760 阅读</span></a>
      <a class="card" href="/?s=备案美化">
        <span class="img" aria-hidden="true" style="--g:linear-gradient(135deg,#fa709a,#fee140)"></span>
        <span class="t">备案徽章美化</span><span class="m">640 阅读</span></a>
    </div>
  </div>
  <div class="ctrl">
    <button type="button" aria-label="向左滚动" id="swdprev">‹</button>
    <button type="button" aria-label="向右滚动" id="swdnext">›</button>
  </div>
</div>

<script>
(function () {
  var vp = document.getElementById('swdvp');
  var pv = document.getElementById('swdprev');
  var nx = document.getElementById('swdnext');
  if (!vp || !pv || !nx) return;
  function step(dir) {
    var c = vp.querySelector('.card');
    var w = c ? c.getBoundingClientRect().width + 10 : 130;
    vp.scrollBy({ left: dir * w, behavior: 'smooth' });
  }
  pv.addEventListener('click', function () { step(-1); });
  nx.addEventListener('click', function () { step(1); });
  function sync() {
    var max = vp.scrollWidth - vp.clientWidth;
    pv.disabled = vp.scrollLeft <= 2;
    nx.disabled = vp.scrollLeft >= max - 2;
  }
  vp.addEventListener('scroll', sync, { passive: true });
  addEventListener('resize', sync);
  sync();
})();
</script>

深色款:深灰底 + 亮色字,对比度 7.2~11.0 全部达标,按钮禁用态更明显。同样用真链接与 scroll-snap。

改的时候注意这几点

  • 别用内联 onclick:改用 addEventListener。内联事件在加了 CSP 的站点上会被直接拦截,而且依赖全局函数、不利于脚本缓存。我实测对照过:CSP 环境下内联版完全失效,addEventListener 正常。
  • <button> 一定写 type="button":默认 type 是 submit,放进表单会误提交导致页面跳转。
  • 图标按钮必须加 aria-label:只有 ‹ › 的话,读屏念出来的是符号,用户不知道它是干什么的。
  • 滑到尽头要禁用按钮:不给反馈,用户会以为组件坏了。判断时留 2px 容差,避免缩放或小数误差导致按钮永远不禁用。
  • 卡片用 <a href>:别用 <div>。这是这批代码里反复出现的问题,第①次看到就该改。
  • 加 scroll-snap-type:x mandatory:滑完自动对齐卡片边缘,不会停在半张卡的尴尬位置。
  • 滚动容器加 tabindex="0" 和 aria-label:否则键盘用户聚焦不到,也没法用方向键滚动。
  • 渐隐遮罩记得 pointer-events:none:不然会挡住滑动手势。
  • 补 prefers-reduced-motion:把 scroll-behavior:smooth 关掉,尊重系统设置。

怎么用

CSS 放进「外观 → 自定义 → 额外 CSS」;HTML 结构写在小工具或主题的「自定义 HTML」区块。

JS 建议单独存成 .js 文件在页脚引入,而不是跟 HTML 写在一起——这样能被浏览器缓存,也不会被 CSP 拦(如果是外部文件,CSP 需要允许对应的 script-src)。

多个滑动组件同页时,注意 ID 要唯一(sw2vp、swdvp 这些),或者干脆改成用 class 遍历:

```js

document.querySelectorAll('.sw2').forEach(function (root) {

var vp = root.querySelector('.vp');

var pv = root.querySelector('[data-prev]');

var nx = root.querySelector('[data-next]');

// ...同样的逻辑,用 root 内的相对查找

});

```

兼容性

scroll-snap-type、scroll-behavior:smooth、scrollBy({behavior:'smooth'})、:focus-visible 在 Chrome、Edge、Firefox、Safari 全系支持,微信内置浏览器也没问题。scrollbar-width 需要 Firefox 64+ / Chrome 121+,不支持的浏览器忽略即可(不影响功能)。


代码仅供学习交流,禁止商用。更多网站美化代码,前往 维形筑网 www.wxzcw.com。

作者:Flechazo

如若转载,请注明出处:维形筑网 https://wxzcw.com

赞 (0)
文章列表卡片:带缩略图的横向卡片列表
上一篇 2026年10月29日 下午11:37
CSS 加载动画:四种纯 CSS 等待效果
下一篇 2026年10月29日 下午11:58

相关推荐

维 形 筑 网