Scroll edge fade
边缘渐隐
Fade both edges of an overflowing tab strip to hint that more items lie off-screen.
Also searched as: 标签太多时两头淡出, 横向滚动条两侧边缘渐隐, 滚动容器两头半透明渐变提示, 标签栏溢出渐隐遮罩
- Placement
- Navigation
- Mood
- Soft, Crisp
- Trigger
- Scroll
- Site types
- 内容站, 个人网站, 工具站
How it works & accessibility
Applies a three-layer CSS mask with @property-registered fade widths on the scroller so any background shows through without clipping focus rings, updating edge widths from scrollLeft and ResizeObserver with scroll-driven animation progressive enhancement.
Best for: Navigation (Scroll).
Reduced motion: 取消渐隐带宽的过渡时间,仍按当前可滚状态静态保留或隐藏边缘提示
Mobile & touch: 移动端横向标签条最常用;设置 scroll-padding-inline 避免焦点或吸附项停在渐隐区里
Key parameters
| Parameter | Key | Default | Range | Notes |
|---|---|---|---|---|
| 渐隐宽度 | fadeSize | 36px | — | — |
| 过渡时长 | duration | 200ms | — | — |
Ready-to-paste AI prompt
Add “Scroll edge fade” to the navigation in my project: Applies a three-layer CSS mask with @property-registered fade widths on the scroller so any background shows through without clipping focus rings, updating edge widths from scrollLeft and ResizeObserver with scroll-driven animation progressive enhancement.
Parameters: fadeSize: 36px, duration: 200ms.
Preserve existing content and layout. Respect prefers-reduced-motion by showing the final state. Content must remain visible if JavaScript fails or is disabled.
Reference: https://donglang.linter.top/effects/scroll-edge-fade.md (canonical specification and reference code).Dependency-free reference code
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>