Frosted sticky header
毛玻璃吸顶
Turn a sticky header into translucent frosted glass as content scrolls underneath.
Also searched as: 顶栏半透明毛玻璃吸顶, 导航栏滚动吸顶变磨砂玻璃, 内容从半透明顶栏底下穿过, 吸顶导航毛玻璃模糊效果
- Placement
- Navigation
- Mood
- Refined, Soft
- Trigger
- Scroll
- Site types
- 个人网站, 作品集, 产品官网, 博客
How it works & accessibility
Pairs container-type: scroll-state and @container scroll-state(stuck: top) with a scroll listener fallback to switch the bar to a color-mix translucent surface and backdrop-filter blur while providing an opaque fallback and reduced-motion support.
Best for: Navigation (Scroll).
Reduced motion: 跳过透明度与模糊的过渡动画,在吸顶触发时直接切换为毛玻璃底色
Mobile & touch: 与桌面一致;适当提高底色不透明度可保证小屏文字对比度,同时配合 scroll-padding-top 防遮挡
Key parameters
| Parameter | Key | Default | Range | Notes |
|---|---|---|---|---|
| 模糊半径 | blur | 14px | — | — |
| 底色不透明度 | opacity | 72% | — | — |
| 切换时长 | duration | 260ms | — | — |
Ready-to-paste AI prompt
Add “Frosted sticky header” to the navigation in my project: Pairs container-type: scroll-state and @container scroll-state(stuck: top) with a scroll listener fallback to switch the bar to a color-mix translucent surface and backdrop-filter blur while providing an opaque fallback and reduced-motion support.
Parameters: blur: 14px, opacity: 72%, duration: 260ms.
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/frosted-sticky-header.md (canonical specification and reference code).Dependency-free reference code
CSS
/* 毛玻璃吸顶(Frosted sticky header):配合 frosted-sticky-header.js。
外层 .fsh-sticky 负责吸顶与 scroll-state 容器查询,内层 .fsh-bar 在吸顶后切到半透明毛玻璃,
并保留 @supports 降级与 prefers-reduced-motion 处理。 */
:where(.fsh-sticky) {
position: sticky;
top: 0;
z-index: 10;
container-type: scroll-state;
container-name: fsh-header;
}
.fsh-bar {
--fsh-blur: 14px;
--fsh-opacity: 72%;
--fsh-duration: 260ms;
--fsh-surface: #f4f3f0;
--fsh-border: rgb(20 21 25 / 0.12);
background-color: transparent;
backdrop-filter: blur(0px) saturate(1);
border-bottom: 1px solid transparent;
transition:
background-color var(--fsh-duration) ease-out,
backdrop-filter var(--fsh-duration) ease-out,
border-color var(--fsh-duration) ease-out;
}
.fsh-sticky.is-stuck > .fsh-bar {
background-color: color-mix(in srgb, var(--fsh-surface) var(--fsh-opacity), transparent);
backdrop-filter: blur(var(--fsh-blur)) saturate(1.5);
border-bottom-color: var(--fsh-border);
}
@container fsh-header scroll-state(stuck: top) {
.fsh-bar {
background-color: color-mix(in srgb, var(--fsh-surface) var(--fsh-opacity), transparent);
backdrop-filter: blur(var(--fsh-blur)) saturate(1.5);
border-bottom-color: var(--fsh-border);
}
}
@supports not (backdrop-filter: blur(1px)) {
.fsh-sticky.is-stuck > .fsh-bar {
background-color: color-mix(in srgb, var(--fsh-surface) 96%, transparent);
}
}
@media (prefers-reduced-motion: reduce) {
.fsh-sticky:not(.fsh-force) > .fsh-bar {
transition: none;
}
}JavaScript
// 毛玻璃吸顶(Frosted sticky header)· 原生 JS,不依赖任何库。
// 用法:吸顶容器加 class="fsh-sticky",内部顶栏加 class="fsh-bar",滚动容器设 scroll-padding-top,
// 调用 frostedStickyHeader(sticky, { blur: 14, opacity: 72, duration: 260, root })。
// 返回 { update(), destroy() }。
/**
* @param {HTMLElement} sticky
* @param {{ blur?: number, opacity?: number, duration?: number, threshold?: number, root?: Window | HTMLElement | null, ignoreReducedMotion?: boolean }} [options]
*/
export function frostedStickyHeader(sticky, {
blur = 14,
opacity = 72,
duration = 260,
threshold = 8,
root = null,
ignoreReducedMotion = false,
} = {}) {
if (!(sticky instanceof Element)) return null;
let destroyed = false;
const bar = sticky.querySelector('.fsh-bar') || sticky;
const view = root || sticky.closest('.fsh-view') || window;
const blurPx = Math.max(0, Number(blur) || 14);
const pct = Math.min(100, Math.max(0, Number(opacity) || 72));
const dur = Math.max(0, Number(duration) || 260);
const minScroll = Math.max(1, Number(threshold) || 8);
sticky.classList.add('fsh-sticky');
if (ignoreReducedMotion) sticky.classList.add('fsh-force');
bar.style.setProperty('--fsh-blur', `${blurPx}px`);
bar.style.setProperty('--fsh-opacity', `${pct}%`);
bar.style.setProperty('--fsh-duration', `${dur}ms`);
const scrollPos = () => (view === window ? window.scrollY || 0 : view.scrollTop || 0);
const update = () => {
if (destroyed) return;
sticky.classList.toggle('is-stuck', scrollPos() > minScroll);
};
let ticking = false;
const onScroll = () => {
if (ticking || destroyed) return;
ticking = true;
requestAnimationFrame(() => {
ticking = false;
update();
});
};
view.addEventListener('scroll', onScroll, { passive: true });
update();
return {
update,
destroy() {
if (destroyed) return;
destroyed = true;
view.removeEventListener('scroll', onScroll);
sticky.classList.remove('is-stuck', 'fsh-force');
bar.style.removeProperty('--fsh-blur');
bar.style.removeProperty('--fsh-opacity');
bar.style.removeProperty('--fsh-duration');
},
};
}HTML
<!-- 毛玻璃吸顶:外层 .fsh-sticky 吸在顶部,内层 .fsh-bar 在滚动吸顶后切换为半透明磨砂玻璃 -->
<div class="fsh-sticky">
<header class="fsh-bar" style="--fsh-surface: #f4f3f0; display: flex; align-items: center; justify-content: space-between; height: 56px; padding: 0 24px;">
<a href="/">动廊画报</a>
<nav style="display: flex; gap: 16px;">
<a href="/works/">展览</a>
<a href="/about/">关于</a>
</nav>
</header>
</div>
<main>……</main>
<style>
html { scroll-padding-top: 56px; }
</style>
<script type="module">
import { frostedStickyHeader } from './frosted-sticky-header.js';
frostedStickyHeader(document.querySelector('.fsh-sticky'), { blur: 14, opacity: 72, duration: 260 });
</script>