关于使用CSS滚动时间线构建反向滚动列的方法

368 阅读1分钟

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滚动-时间线prefers-reduced-motion

我唯一想添加的是尊重prefers-reduced-motion ,因为我可以看到这种滚动运动会影响到有晕动症的人。要做到这一点,你可以在支持测试的同一行中结合测试,在JavaScript中进行。

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

我不太清楚是测试no-preference 还是测试reduce 的反面。无论如何,在CSS中的诀窍是将你要对@scroll-timelineanimation-timeline 做的任何事情包在一个@supports 测试中(以防你想做不同的事情),然后将其包在一个偏好测试中。

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

    @supports (animation-timeline: works) {

      /* Do fancy stuff */

    }

}

这里有一个演示,所有真正的功劳都归功于Bramus,因为他让这个演示得以进行。

哦,你知道吗?CSS变得更漂亮应该 [@when](https://css-tricks.com/proposal-for-css-when/)作为一项功能登陆。

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

} @else {

}