写过 sticky 的人都踩过这个坑——吸顶之后想加个阴影,只能写一段 IntersectionObserver 监听器。Chrome 133 今天把这件事用一行 CSS 彻底原生化了,连一行 JS 都不用写。

写过 sticky 的人都踩过这个坑——吸顶之后想加个阴影,只能写一段 IntersectionObserver 监听器。Chrome 133 今天把这件事用一行 CSS 彻底原生化了,连一行 JS 都不用写。

每个项目里大概都有个 useIsStuck.ts 或者 stickyHeader.js,作用只有一个:告诉你这个 sticky 元素当前有没有贴到屏幕边缘。有的话,加上阴影;没的话,把阴影去掉。

这个需求本身很简单,但实现起来一直很憋屈。

IntersectionObserver 方案的问题在哪

你需要一个看不见的 1px sentinel div 贴在 sticky 元素后面,IntersectionObserver 盯着它,sentinel 一旦离开视口就说明 sticky 生效了,再回调里 toggle class。

这套方案有三个问题:

第一,每次滚动都要走主线程。IntersectionObserver 虽然比 scroll 监听器轻,但仍然是在浏览器渲染pipeline 外面算的,60fps 滚动时每帧都要通知你。

第二,getBoundingClientRect 容易引发强制同步布局(layout thrashing)。如果你的回调里再查一次元素位置,write 紧跟 read,浏览器得停下渲染先把布局算完再执行你的 JS。

第三,sentinel 位置算错了效果就不对——设得太远会有延迟,设得太近会抖动,跨浏览器行为还不一致。

这个问题社区里讨论了快十年,一直没有浏览器原生的方案。

scroll-state() 是什么

Chrome 133(2026 年 3 月)引入的 @container scroll-state() 是一种容器查询,专门用来查询滚动容器的状态。你在一个滚动容器或 sticky 元素上设 container-type: scroll-state,然后它的子元素就可以通过 @container 查询容器当前处于什么状态。

它支持四种状态:

  • stuck: top|right|bottom|left — sticky 元素当前贴附在哪条边
  • snapped: x|y|inline|block — scroll-snap 目标当前对齐到哪个位置
  • scrollable: top|right|bottom|left — 某方向上还有没有内容可滚动
  • scrolled: top|right|bottom|left|none(Chrome 144+)— 最近一次滚动方向

怎么用

sticky 头部吸顶后自动加阴影,核心代码:

/* 把 sticky 元素声明为 scroll-state 容器 */
.sticky-header {
  container-type: scroll-state;
  position: sticky;
  top: 0;
  background: white;
  transition: box-shadow 0.3s ease;
}

/* 正确做法:在 sticky-header 上声明查询,样式加在子元素上 */
@container scroll-state(stuck: top) {
  .sticky-header {
    box-shadow: 0 4px 12px rgb(0 0 0 / 0.12);
  }
}

完整的 sticky 头部示例,三层结构:

<div class="header-wrap">
  <header class="sticky-header">
    <nav>Logo  Nav  User</nav>
  </header>
</div>
.header-wrap {
  container-type: scroll-state;
  position: sticky;
  top: 0;
}

.sticky-header nav {
  transition: box-shadow 0.3s ease;
}

@container scroll-state(stuck: top) {
  .sticky-header nav {
    box-shadow: 0 4px 12px rgb(0 0 0 / 0.12);
  }
}

snap 轮播的指示器也可以纯 CSS

scroll-state 的另一个高频场景:轮播当前 snap 到哪个 item,周围的 item 变暗。传统做法要 scrollsnapchange 事件配合 JS 改 class:

/* 滚动容器 */
.carousel {
  overflow-x: auto;
  scroll-snap-type: x mandatory;
}

/* 每个 snap 目标设为 scroll-state 容器 */
.carousel-item {
  scroll-snap-align: center;
  container-type: scroll-state;
}

/* 非当前 item 变暗 */
.carousel-item > * {
  transition: opacity 0.4s ease;
}

@container not scroll-state(snapped: x) {
  .carousel-item > * {
    opacity: 0.25;
  }
}

注意 snapped: x 查询的是 scroll-snap-align: center 的那个 item 是否在视口中央,浏览器内部处理,不需要任何 JS。

滚动提示也可以检测

判断用户是否已经滚动过某容器,从而隐藏「向下滚动」提示:

@container scroll-state(scrolled: top) {
  .scroll-hint {
    opacity: 0;
    pointer-events: none;
  }
}

谁还没支持

目前只有 Chrome 133+ / Edge 133+ 支持,覆盖率约 50%(caniuse 数据)。Safari 和 Firefox 尚未实现。

这不是 Baseline 特性,生产环境要用渐进增强:

/* 先写静态样式,浏览器不支持 @container 时显示默认阴影 */
.sticky-header nav {
  box-shadow: 0 4px 12px rgb(0 0 0 / 0.08);
}

@supports (container-type: scroll-state) {
  .sticky-header nav {
    box-shadow: none;
    transition: box-shadow 0.3s ease;
  }

  @container scroll-state(stuck: top) {
    .sticky-header nav {
      box-shadow: 0 4px 12px rgb(0 0 0 / 0.12);
    }
  }
}

三个容易踩的坑

第一个坑:container-type: scroll-state 必须设在 sticky 元素本身上,不能设在它的父容器。设错了查不到 stuck 状态。

第二个坑:容器自己不能被 @container 查询样式,必须在子元素上写样式。设反了浏览器会静默忽略。

第三个坑:stuck: top 查询的前提是 sticky 元素真的设了对应的 inset(比如 top: 0)。没有 inset 就没有 stuck 状态可以报告,查询永远不会触发。

下一步

如果你现在有 useIsStuck.ts 之类的文件,可以考虑把它替换成三行 CSS:

  1. 在 sticky 元素的父级加 container-type: scroll-state
  2. 用 @container scroll-state(stuck: top) 替代 JS class toggle
  3. 用 @supports (container-type: scroll-state) 做渐进增强保底

这套方案不需要任何 polyfill,不支持的浏览器直接看到静态样式,交互增强完全由 CSS 接管。主线程零开销,60fps 滚动也不担心。

评论区

0 条评论

登录后可评论。

小鹿·界面实验室 2514 阅读