EN
探索频道
滚动 · 原生 JS + CSS

边缘渐隐

Scroll edge fade

横向标签超出容器时,左右两边按还能继续滚的距离渐隐淡出,提示后面还有内容

常见大白话搜法:标签太多时两头淡出、横向滚动条两侧边缘渐隐、滚动容器两头半透明渐变提示、标签栏溢出渐隐遮罩

AI 专用 Markdown (.md)
适用位置
导航
气质感觉
柔和、利落
触发方式
滚动
适合网站
内容站、个人网站、工具站

实现原理与无障碍底线

在滚动容器自身上叠一层三段式 CSS mask:中间实色区域减去左右两条线性渐变透明带,渐变宽度用 @property 注册为长度变量;随横向滚动位置把左侧带宽从 0 拉到 fadeSize、右侧带宽在滚到头时收回到 0,支持双向切换与 RTL 排版;减少动态时直接切换带宽,无脚本时由 CSS scroll(self inline) 渐进增强。

适用场景:多标签分类栏、筛选胶囊条、横向工具栏:不放丑陋的左右箭头,也不用实色遮罩挡住渐变或花纹背景

减少动态(prefers-reduced-motion):取消渐隐带宽的过渡时间,仍按当前可滚状态静态保留或隐藏边缘提示

移动端与触屏:移动端横向标签条最常用;设置 scroll-padding-inline 避免焦点或吸附项停在渐隐区里

关键可调参数

参数键名默认值建议范围说明
渐隐宽度fadeSize36px——
过渡时长duration200ms——

可直接复制给 AI(Cursor / Claude / v0)的提示词

请在我的项目里给导航加上「边缘渐隐」(Scroll edge fade)效果:在滚动容器自身上叠一层三段式 CSS mask:中间实色区域减去左右两条线性渐变透明带,渐变宽度用 @property 注册为长度变量;随横向滚动位置把左侧带宽从 0 拉到 fadeSize、右侧带宽在滚到头时收回到 0,支持双向切换与 RTL 排版;减少动态时直接切换带宽,无脚本时由 CSS scroll(self inline) 渐进增强。
参数:渐隐宽度 36px,过渡时长 200ms。
要求:保留原有的内容和布局;系统开启"减少动态"时直接显示最终状态;脚本出错或没有执行时内容仍然可见。
参考:https://donglang.linter.top/effects/scroll-edge-fade.md(原理、参数和参考代码)

原生无依赖参考代码

CSS
/* 边缘渐隐(Scroll edge fade):配合 scroll-edge-fade.js。
   使用 @property 注册 --sef-left / --sef-right 以便在 mask 宽度变化时平滑过渡,不裁切焦点外框,
   并在任何渐变或纹理背景上都自然透出底层背景。 */
@property --sef-left {
  syntax: "<length>";
  inherits: false;
  initial-value: 0px;
}

@property --sef-right {
  syntax: "<length>";
  inherits: false;
  initial-value: 0px;
}

:where(.sef) {
  --sef-fade-size: 36px;
  --sef-duration: 200ms;
  --sef-dir: right;
  --sef-start-pos: 0;
  --sef-end-pos: 100%;
  overflow-x: auto;
  scrollbar-width: none;
  scroll-padding-inline: var(--sef-fade-size);
}

.sef::-webkit-scrollbar {
  display: none;
}

.sef:dir(rtl) {
  --sef-dir: left;
  --sef-start-pos: 100%;
  --sef-end-pos: 0;
}

.sef {
  -webkit-mask:
    linear-gradient(#000, #000) no-clip subtract,
    linear-gradient(to var(--sef-dir), #000, transparent) var(--sef-start-pos) 0 / var(--sef-left) 100% no-repeat no-clip,
    linear-gradient(to var(--sef-dir), transparent, #000) var(--sef-end-pos) 0 / var(--sef-right) 100% no-repeat no-clip;
  mask:
    linear-gradient(#000, #000) no-clip subtract,
    linear-gradient(to var(--sef-dir), #000, transparent) var(--sef-start-pos) 0 / var(--sef-left) 100% no-repeat no-clip,
    linear-gradient(to var(--sef-dir), transparent, #000) var(--sef-end-pos) 0 / var(--sef-right) 100% no-repeat no-clip;
}

.sef.sef-js {
  transition: --sef-left var(--sef-duration) ease-out, --sef-right var(--sef-duration) ease-out;
}

@supports (animation-timeline: scroll()) {
  .sef:not(.sef-js) {
    animation: sef-reveal-start linear both, sef-hide-end linear both;
    animation-timeline: scroll(self inline);
    animation-range: 0 var(--sef-fade-size), calc(100% - var(--sef-fade-size)) 100%;
  }

  @keyframes sef-reveal-start {
    to {
      --sef-left: var(--sef-fade-size);
    }
  }

  @keyframes sef-hide-end {
    from {
      --sef-right: var(--sef-fade-size);
    }
  }
}

@media (prefers-reduced-motion: reduce) {
  .sef:not(.sef-force) {
    animation: none;
    transition: none;
  }
}
JavaScript
// 边缘渐隐(Scroll edge fade)· 原生 JS,不依赖任何库。
// 用法:横向滚动容器加上 class="sef",调用 scrollEdgeFade(scroller, { fadeSize: 36, duration: 200 })。
// 容器右侧还有可滚内容时右侧渐隐;往右滚后左侧出现渐隐;滚到尽头时对应一侧收回到 0px。
// 返回 { update(), destroy() }。

/**
 * @param {HTMLElement} scroller
 * @param {{ fadeSize?: number, duration?: number, ignoreReducedMotion?: boolean }} [options]
 */
export function scrollEdgeFade(scroller, {
  fadeSize = 36,
  duration = 200,
  ignoreReducedMotion = false,
} = {}) {
  if (!(scroller instanceof Element)) return null;
  let destroyed = false;
  const size = Math.max(0, Number(fadeSize) || 36);
  const dur = Math.max(0, Number(duration) || 200);

  scroller.classList.add('sef', 'sef-js');
  if (ignoreReducedMotion) scroller.classList.add('sef-force');
  scroller.style.setProperty('--sef-fade-size', `${size}px`);
  scroller.style.setProperty('--sef-duration', `${dur}ms`);

  const update = () => {
    if (destroyed) return;
    const max = Math.max(0, scroller.scrollWidth - scroller.clientWidth);
    if (max <= 1 || size <= 0) {
      scroller.style.setProperty('--sef-left', '0px');
      scroller.style.setProperty('--sef-right', '0px');
      return;
    }
    const pos = Math.min(Math.max(scroller.scrollLeft, 0), max);
    const left = Math.round(size * Math.min(1, pos / size));
    const right = Math.round(size * Math.min(1, (max - pos) / size));
    scroller.style.setProperty('--sef-left', `${left}px`);
    scroller.style.setProperty('--sef-right', `${right}px`);
  };

  let ticking = false;
  const onScroll = () => {
    if (ticking || destroyed) return;
    ticking = true;
    requestAnimationFrame(() => {
      ticking = false;
      update();
    });
  };

  const ro = new ResizeObserver(update);
  ro.observe(scroller);
  scroller.addEventListener('scroll', onScroll, { passive: true });
  update();

  return {
    update,
    destroy() {
      if (destroyed) return;
      destroyed = true;
      scroller.removeEventListener('scroll', onScroll);
      ro.disconnect();
      scroller.classList.remove('sef-js', 'sef-force');
      scroller.style.removeProperty('--sef-fade-size');
      scroller.style.removeProperty('--sef-duration');
      scroller.style.removeProperty('--sef-left');
      scroller.style.removeProperty('--sef-right');
    },
  };
}
HTML
<!-- 边缘渐隐:横向可滚容器加 class="sef",溢出时两头按可滚距离淡出 -->
<div class="sef chip-bar" role="region" aria-label="频道筛选">
  <button type="button" class="is-on">推荐</button>
  <button type="button">设计</button>
  <button type="button">动效</button>
  <button type="button">字体</button>
  <button type="button">排版</button>
  <button type="button">交互</button>
  <button type="button">品牌</button>
  <button type="button">专题</button>
</div>
<script type="module">
  import { scrollEdgeFade } from './scroll-edge-fade.js';
  scrollEdgeFade(document.querySelector('.chip-bar'), { fadeSize: 36, duration: 200 });
</script>

同属「滚动」的其他动效

查看全部 86 个动效 →

本次更新仅弹窗提醒一次 · 可随时在历史公告回看