# 边缘渐隐（Scroll edge fade）

- 俗称：标签太多时两头淡出；横向滚动条两侧边缘渐隐；滚动容器两头半透明渐变提示；标签栏溢出渐隐遮罩
- 用在：导航（内容站、个人网站、工具站）
- 触发：滚动
- 感觉：柔和、利落
- 版本：1，更新于 2026-12-01

## 原理

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

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

## 参数

| 参数名 | 含义 | 默认值 | 建议范围 |
|---|---|---|---|
| `fadeSize` | 渐隐宽度 | 36px | 16–72px |
| `duration` | 过渡时长 | 200ms | 80–500ms |

## 质量要求

- 开启"减少动态"时：取消渐隐带宽的过渡时间，仍按当前可滚状态静态保留或隐藏边缘提示。
- 手机上：移动端横向标签条最常用；设置 scroll-padding-inline 避免焦点或吸附项停在渐隐区里。
- 动画只在播放时改变元素；脚本出错或没有执行时，内容照样可见（不要先用 CSS 把内容藏起来再等脚本显示）。

## 提示词（可以直接交给 AI）

```text
请在我的项目里给导航加上「边缘渐隐」（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（原理、参数和参考代码）
```

## 参考代码

HTML 结构：

```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>
```

CSS：

```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;
  }
}
```

JS（原生，不依赖任何库）：

```js
// 边缘渐隐（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');
    },
  };
}
```

## 出处

- 本站原创实现：遵循 scroll-driven mask 渐进增强与 ResizeObserver/scroll 双端兜底方案，未使用第三方代码
