ナビゲーション

v2.1.0
構造

スクロール表示

スクロールで画面に入るとフェードインして少し浮き上がるコンテンツ。スクロール駆動アニメーションで、スクリプトは不要です。

このページの内容

スクロール表示では、コンテンツがスクロールで画面内に入るときに現れます。要素がビューポートに入り始めると、数ピクセル上に移動しながらフェードインし、その後は静止します。1 つのクラスとスクロール駆動アニメーションで動作します。要素の監視やクラスの動的な追加は行いません。スクロール位置がアニメーションの進み具合を決めます。

スクロール表示

<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()に沿って、スクロール領域に入る区間で再生されます。スクロール位置に連動することで、次の 3 つの挙動になります。

  • ページを開いた時点で画面内にある要素は、最初のフレームより前に表示が完了しているため、動きません。
  • 逆方向にスクロールすると、アニメーションも逆再生されます。リセットは不要です。
  • 遅延や開始間隔を調整する必要はありません。要素は画面内に入る順に現れます。

アニメーションが完了するまでの範囲は--reveal-rangeで指定します。既定では、要素が画面内に入り始めてから最初の 12rem 以内に完了します。そのため、縦に長いセクションでも、画面 1 つ分にわたってフェードが続くことはありません。要素がその距離より短ければ、全体が画面内に入った時点でアニメーションが完了するため、ページの最後の要素でもアニメーションは最後まで進みます。

#調整

次の 3 つのプロパティを、対象の要素か、複数の対象要素に共通する親要素に設定できます。

  • --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);  }}