动画跟着滚动跑偏了我盯了三年,今天终于把 scroll-state() 的四种状态全拆开了

DOM 节点还在跑动画,滚动方向一变整个效果全乱了——我盯了三年今天终于把 scroll-driven 动画「跟谁绑定」这件事彻底想清楚了。scroll-state() 让我把滚动方向和动画时间线完全拆开,导航栏想怎么跟随就怎么跟随,不用再写那堆 scroll 事件。

先说个真实的坑。你写了一个入场动画,用 scroll() 把滚动进度映射成动画时间线,元素跟着页面滚动一点一点滑进来。结果产品说「能不能下滑的时候动画反着走」,你一想——完了,这动画跟 scroll 绑死了,要么重写,要么上 JS。

这个问题的根源是:scroll-driven 动画的时间线默认跟「有滚动条的最近祖先容器」绑定死,出了这个容器边界,timeline 就断了。想跨容器组合?对不起,不支持。

Chrome 144 给了答案:timeline-scope。

timeline-scope:把时间线的「作用域」从容器里放出来

timeline-scope 这个属性,是给滚动时间线扩大作用范围的。

.container {
  container-type: scroll-state;
  timeline-scope: --my-scroll-timeline;
}

/* 动画在任何位置都能引用这个时间线 */
.animated-element {
  animation: slide linear(auto);
  animation-timeline: scroll(--my-scroll-timeline);
}

之前 scroll() 函数只能找到离自己最近的滚动祖先。有了 timeline-scope,你可以给任何一个有 container-type 的祖先声明「这个滚动时间线我也要用」,时间线的范围就扩大到了那一层祖先。

scroll-state():四种滚动状态,CSS 自己会判断

scroll-state() 是 container query 的一种,查询的是「这个容器的滚动状态」。配合 container-type: scroll-state,CSS 能直接读出四种状态:

状态 含义
scrolled 容器已经发生了滚动
stuck 元素被滚动卡在了边缘(比如 sticky 定位)
snapped 滚动位置落在了某个 snap point 上
scrollable 容器有溢出的内容,可以滚动

Chrome 144 支持。实战里最常用的是 scrolled,用来判断导航栏的显示隐藏:

.header {
  container-type: scroll-state;
  /* 默认:导航栏显示 */
  transform: translateY(0);
  transition: transform 0.3s ease;
}

/* 容器滚动过 → 导航栏隐藏 */
@container scroll-state(scrolled) {
  .header {
    transform: translateY(-100%);
  }
}

没有 JS,没有 scroll 事件监听,纯 CSS。页面滚动了,CSS 自己知道。

组合拳:scroll() + timeline-scope + scroll-state()

三个配合起来,能做的事比单独用任何一个都多。来看一个完整的例子:滚动进度条 + 导航栏跟随 + 侧边目录高亮,全部联动同一个滚动容器,互不干扰。

.page-wrapper {
  container-type: scroll-state;
  timeline-scope: --main-scroll;
  overflow-y: auto;
}

/* 滚动进度条 */
.reading-progress {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 3px;
  background: #3b82f6;
  transform-origin: left;
  transform: scaleX(
    calc(scroll(--main-scroll) * 100%)
  );
  animation: progress-fill linear(auto);
  animation-timeline: scroll(--main-scroll);
}

/* 导航栏:滚动过就隐藏 */
@container scroll-state(scrolled) {
  .site-nav {
    transform: translateY(-100%);
  }
}

/* 侧边目录:滚动到对应章节高亮 */
.toc-item {
  transition: color 0.2s;
}
@container scroll-state(scrolled) {
  .toc-item:has(+ * > .active) {
    color: #3b82f6;
    font-weight: 600;
  }
}

时间线是同一个 --main-scroll,但三个 UI 元素各自按自己的逻辑响应它,互不耦合。

降级方案

timeline-scope 和 scroll-state() 目前只有 Chrome 144+ 支持。Safari 和 Firefox 还没上,生产环境用的时候加 @supports:

.page-wrapper {
  container-type: scroll-state;
  timeline-scope: --main-scroll;
}

@supports (timeline-scope: name) {
  /* 现代浏览器用 CSS 时间线 */
  .reading-progress {
    animation-timeline: scroll(--main-scroll);
  }
}

/* 不支持就用 JS 兜底 */
@supports not (timeline-scope: name) {
  /* 传统的 scroll 事件监听 */
}

实际项目中,大多数情况下只需要 scrolled 状态做导航栏,整个降级成本很低。

下一步

如果你现在用的是 JS 监听 scroll 事件做导航栏跟随,试试把 container-type: scroll-state 配上去,一个 @container scroll-state(scrolled) 就能把那个 scroll 事件监听器删掉。摸真实项目的时候注意 position: sticky 和 scroll-state() 混用时 stuck 状态的触发时机,实际测一下跟你的预期是否一致。

核心就一件事:scroll-driven 动画的时间线不再被容器边界锁死,timeline-scope 把它变成了可共享的资源。配好了,一个滚动动作能同时驱动进度条、目录高亮、导航栏三个 UI,代码量比原来少一个数量级。

评论区

0 条评论

登录后可评论。

小鹿·界面实验室 2171 阅读