使用 CSS Scroll-Timeline 反向滚动列的笔记

Avatar of Chris Coyier
Chris Coyier

DigitalOcean 为您的旅程每个阶段提供云产品。立即开始使用 价值 200 美元的免费积分!

让我快速地做一下

Showing three columns of photos of actresses demonstrating the CSS scroll-timeline effect. The first and their columns scroll up and down together. The second columns scrolls the opposite direction.

CSS Scroll-Timeline 与 prefers-reduced-motion

我要补充的唯一一点是,要尊重 prefers-reduced-motion,因为我可以看到这种滚动运动会影响晕动症患者。为此,您可以在 JavaScript 中将测试与支持测试组合在一起。

if (
    !CSS.supports("animation-timeline: foo") && 
    !window.matchMedia('(prefers-reduced-motion: reduce)').matches
   ) {
     // Do fancy stuff
}

我不确定是否最好测试 no-preferencereduce 的相反情况。无论哪种方式,CSS 中的技巧是将您要使用 @scroll-timelineanimation-timeline 执行的所有操作包装在 @supports 测试中(如果您想做其他事情,请保留),然后将它包装在首选项测试中。

@media (prefers-reduced-motion: no-preference) {

    @supports (animation-timeline: works) {

      /* Do fancy stuff */

    }

}

这是该演示,所有功劳都归功于 Bramus,感谢他让它运行起来。

哦,你知道吗?如果 @when 作为一项功能落地,CSS 会变得更好。

@when supports(animation-timeline: works) and media(prefers-reduced-motion: no-preference) {

} @else {

}