# 毛玻璃吸顶（Frosted sticky header）

- 俗称：顶栏半透明毛玻璃吸顶；导航栏滚动吸顶变磨砂玻璃；内容从半透明顶栏底下穿过；吸顶导航毛玻璃模糊效果
- 用在：导航（个人网站、作品集、产品官网、博客）
- 触发：滚动
- 感觉：高级、柔和
- 版本：1，更新于 2026-12-01

## 原理

吸顶外框设 position: sticky; top: 0 与 container-type: scroll-state，子级顶栏通过 @container scroll-state(stuck: top) 与脚本 .is-stuck 同步切换到 color-mix 半透明底色及 backdrop-filter: blur() 饱和模糊；滚动容器设置 scroll-padding-top 避免焦点被遮挡，无模糊支持时退回高不透明度底色，减少动态时跳过过渡。

适合：画廊官网、作品集、产品页顶部导航：页面在顶部时顶栏透明融入首屏，往下滑后浮起一层磨砂玻璃保持导航清晰。

## 参数

| 参数名 | 含义 | 默认值 | 建议范围 |
|---|---|---|---|
| `blur` | 模糊半径 | 14px | 6–28px |
| `opacity` | 底色不透明度 | 72% | 45–92% |
| `duration` | 切换时长 | 260ms | 100–600ms |

## 质量要求

- 开启"减少动态"时：跳过透明度与模糊的过渡动画，在吸顶触发时直接切换为毛玻璃底色。
- 手机上：与桌面一致；适当提高底色不透明度可保证小屏文字对比度，同时配合 scroll-padding-top 防遮挡。
- 动画只在播放时改变元素；脚本出错或没有执行时，内容照样可见（不要先用 CSS 把内容藏起来再等脚本显示）。

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

```text
请在我的项目里给导航加上「毛玻璃吸顶」（Frosted sticky header）效果：吸顶外框设 position: sticky; top: 0 与 container-type: scroll-state，子级顶栏通过 @container scroll-state(stuck: top) 与脚本 .is-stuck 同步切换到 color-mix 半透明底色及 backdrop-filter: blur() 饱和模糊；滚动容器设置 scroll-padding-top 避免焦点被遮挡，无模糊支持时退回高不透明度底色，减少动态时跳过过渡。
参数：模糊半径 14px，底色不透明度 72%，切换时长 260ms。
要求：保留原有的内容和布局；系统开启"减少动态"时直接显示最终状态；脚本出错或没有执行时内容仍然可见。
参考：https://donglang.linter.top/effects/frosted-sticky-header.md（原理、参数和参考代码）
```

## 参考代码

HTML 结构：

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

CSS：

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

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

```js
// 毛玻璃吸顶（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');
    },
  };
}
```

## 出处

- 本站原创实现：参考 Scroll-State Sticky Header 与 backdrop-filter 降级规范自研，未使用第三方代码
