导航

v2.1.0
结构

滚动显现

内容随滚动进入可见区域时淡入并微微上移,使用滚动驱动动画,无需脚本。

本页内容

滚动显现让内容在滚入视口时逐渐出现。内容的起始部分逐渐进入视口时,内容一边淡入,一边向上移动几像素到达最终位置,随后保持静止。这个效果只用一个类和一段滚动驱动动画。无需监听元素或动态添加类,动画进度由滚动位置决定。

滚动显现

<div class="flex flex-col gap-md max-w(420) mx-auto">  <p class="m(0) fg-text-muted text-sm text-center py-xl">Scroll down</p>  <article class="card mt(240)">    <header class="card-header">      <h3 class="card-title">Native first</h3>      <p class="card-description">Every overlay is a dialog or a popover, opened without script.</p>    </header>  </article>  <article class="card reveal">    <header class="card-header">      <h3 class="card-title">One material</h3>      <p class="card-description">Surfaces step apart on a ladder of light, in both themes.</p>    </header>  </article>  <article class="card reveal">    <header class="card-header">      <h3 class="card-title">Contracts</h3>      <p class="card-description">Each stylesheet says what it promises in a file beside it.</p>    </header>  </article>  <article class="card reveal" style="--reveal-scale: .96">    <header class="card-header">      <h3 class="card-title">Yours to change</h3>      <p class="card-description">Tokens retune every component; the source is there to own.</p>    </header>  </article>  <p class="m(0) fg-text-muted text-sm text-center py-xl">The end</p></div>
<div className="flex flex-col gap-md max-w(420) mx-auto">  <p className="m(0) fg-text-muted text-sm text-center py-xl">Scroll down</p>  <article className="card mt(240)">    <header className="card-header">      <h3 className="card-title">Native first</h3>      <p className="card-description">        Every overlay is a dialog or a popover, opened without script.      </p>    </header>  </article>  <article className="card reveal">    <header className="card-header">      <h3 className="card-title">One material</h3>      <p className="card-description">Surfaces step apart on a ladder of light, in both themes.</p>    </header>  </article>  <article className="card reveal">    <header className="card-header">      <h3 className="card-title">Contracts</h3>      <p className="card-description">Each stylesheet says what it promises in a file beside it.</p>    </header>  </article>  <article className="card reveal" style={{ '--reveal-scale': '.96' } as React.CSSProperties}>    <header className="card-header">      <h3 className="card-title">Yours to change</h3>      <p className="card-description">Tokens retune every component; the source is there to own.</p>    </header>  </article>  <p className="m(0) fg-text-muted text-sm text-center py-xl">The end</p></div>
<div class="flex flex-col gap-md max-w(420) mx-auto">  <p class="m(0) fg-text-muted text-sm text-center py-xl">Scroll down</p>  <article class="card mt(240)">    <header class="card-header">      <h3 class="card-title">Native first</h3>      <p class="card-description">Every overlay is a dialog or a popover, opened without script.</p>    </header>  </article>  <article class="card reveal">    <header class="card-header">      <h3 class="card-title">One material</h3>      <p class="card-description">Surfaces step apart on a ladder of light, in both themes.</p>    </header>  </article>  <article class="card reveal">    <header class="card-header">      <h3 class="card-title">Contracts</h3>      <p class="card-description">Each stylesheet says what it promises in a file beside it.</p>    </header>  </article>  <article class="card reveal" style="--reveal-scale: .96">    <header class="card-header">      <h3 class="card-title">Yours to change</h3>      <p class="card-description">Tokens retune every component; the source is there to own.</p>    </header>  </article>  <p class="m(0) fg-text-muted text-sm text-center py-xl">The end</p></div>
<template>  <div class="flex flex-col gap-md max-w(420) mx-auto">    <p class="m(0) fg-text-muted text-sm text-center py-xl">Scroll down</p>    <article class="card mt(240)">      <header class="card-header">        <h3 class="card-title">Native first</h3>        <p class="card-description">          Every overlay is a dialog or a popover, opened without script.        </p>      </header>    </article>    <article class="card reveal">      <header class="card-header">        <h3 class="card-title">One material</h3>        <p class="card-description">Surfaces step apart on a ladder of light, in both themes.</p>      </header>    </article>    <article class="card reveal">      <header class="card-header">        <h3 class="card-title">Contracts</h3>        <p class="card-description">Each stylesheet says what it promises in a file beside it.</p>      </header>    </article>    <article class="card reveal" style="--reveal-scale: .96">      <header class="card-header">        <h3 class="card-title">Yours to change</h3>        <p class="card-description">Tokens retune every component; the source is there to own.</p>      </header>    </article>    <p class="m(0) fg-text-muted text-sm text-center py-xl">The end</p>  </div></template>
在预览中滚动,每张卡片进入可视区域时都会显现。

#工作原理

动画使用元素自己的视图时间线 animation-timeline: view(),在元素进入滚动容器可视区域的这段范围内播放。由滚动位置决定动画进度,会带来以下三种表现:

  • 页面打开时已经可见的内容完全不会移动,因为它在首帧之前就已完成入场。
  • 往回滚动时,动画会倒放,无需重置。
  • 元素按进入滚动容器可视区域的顺序依次显现,无需设置延迟或错开播放时间。

--reveal-range 将动画限制在元素进入滚动容器可视区域的前 12rem 内,因此即使区块很高,淡入也不会持续一整屏。高度不足 12rem 的元素在完全进入可视区域时就会完成动画,因此页面末尾的内容也总能完整显现。

#调整效果

可在单个元素上或多个元素的共同父元素上设置以下三个属性:

  • --reveal-distance 控制上移距离,默认为 16px。设置 0 可仅保留淡入效果。
  • --reveal-scale 控制显现时的初始缩放比例。.96 适合图片或卡片,文字应保持原尺寸。
  • --reveal-range 控制元素进入滚动容器可视区域多远时完成动画。

#何时使用

在长页面中,将滚动显现用于完整的内容区块,例如落地页中的一个区段、一排卡片或一幅插图。适量使用。如果所有内容都带有入场动画,页面就会一直有内容在动,快速滚动的读者也得等内容显现。

不要用于读者需要立即看到的文字、对话框或菜单内的任何内容,以及加载时会改变尺寸的内容。

#无滚动驱动动画时

浏览器不支持 animation-timeline: view(),或读者在系统中启用了减少动态效果时,这个类不起作用,内容会直接显示。打印时也是如此。透明度只在动画中设置,因此内容的显示始终独立于动画。

#无障碍

  • 内容从一开始就在文档和无障碍树中。进入滚动容器的可视区域时,只有透明度和位置会变化。
  • 启用 prefers-reduced-motion: reduce 时,内容直接显示,无淡入或位移。
  • 将位移控制在较小范围内。大区块长距离移动会扫过视口的大部分区域,有些读者看了会感到不适。

#类名

类名说明
reveal元素的起始部分进入视口时淡入并上移 16px,随后保持静止。

#自定义属性

属性默认值说明
--reveal-distance1rem上移距离。设为 0 时仅淡入
--reveal-scale1显现动画开始时的缩放比例,例如图片可设为 .96
--reveal-range12rem显现动画结束时,元素进入视口的部分所需达到的高度。元素比该值矮时,整个元素进入视口即结束动画

#源码

@master/ui/components/reveal.css
/* * Reveal: content that arrives as it scrolls into view. * A scroll-driven animation on the element's own view timeline: it fades in and rises the last few pixels * while its first stretch crosses the edge of the viewport, and is at rest from then on. Nothing observes * it and no class is added: the position of the scroll is the clock, so scrolling back plays it backwards * and what is already in view when the page opens does not move at all. * Without scroll-driven animations, and for a reader who asks for less motion, the content is simply there. * Geometry: 16px of travel, finished within the first 12rem of the element entering. */@layer components.ui {  .reveal {    --reveal-distance: 1rem;    --reveal-scale: 1;    --reveal-range: 12rem;    @supports (animation-timeline: view()) {      @media (prefers-reduced-motion: no-preference) {        /* Longhands: tools that fold the timeline into the shorthand write a value no browser reads. */        animation-name: reveal-arrive;        animation-timing-function: var(--easing-glide);        animation-fill-mode: both;        animation-timeline: view();        /* An element shorter than the range has arrived once all of it is in view. */        animation-range: entry 0% entry min(100%, var(--reveal-range));        /*         * Focus scrolls a control only just into view, where the content is still arriving: what holds focus is         * shown whole at once, so a faded ring is never where the keys landed.         */        &:focus-within {          animation-name: none;        }      }    }    @media print {      animation: none;    }  }}@keyframes reveal-arrive {  from {    opacity: 0;    scale: var(--reveal-scale);    translate: 0 var(--reveal-distance);  }}