ナビゲーション

v2.1.0
データ表示

オドメーター

機械式カウンターの歯車のように、桁が回転して新しい値に切り替わる数値表示。

このページの内容

オドメーターは、機械式カウンターのように、桁を回転させて新しい値を表示します。各桁は高さ1行分の窓で、スタイルシートが縦に並べた数字の一つを表示します。ページ側で桁の--digitを変更すると、数字の列が新しい数字まで回転し、行の上下で数字がフェードイン、フェードアウトします。独自の背景面やサイズを持たないため、統計表示の数値、見出し、文中の一語として使えます。同じ数値をプレーンテキストでも一度記述し、スクリーンリーダーはそのテキストを読み上げます。

オドメーター

<div class="card w-full max-w(320)">  <div class="stat">    <p class="stat-label">Visitors today</p>    <div class="stat-row">      <p class="stat-value">        <span class="odometer" id="visitors">          <span class="odometer-value">12,847</span>          <span class="odometer-figures" aria-hidden="true">            <span class="odometer-digit" style="--digit: 1"></span> <span class="odometer-digit" style="--digit: 2"></span>, <span class="odometer-digit" style="--digit: 8"></span> <span class="odometer-digit" style="--digit: 4"></span> <span class="odometer-digit" style="--digit: 7"></span>          </span>        </span>      </p>      <p class="stat-delta stat-delta-up">        <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">          <path d="M13.6362 7.5189A0.4947 0.4947 0 0 0 13.5 7.5L2.4755 7.5006A0.5 0.5 0 0 0 2.4266 8.4946L2.5 8.5H13.5A0.4947 0.4947 0 0 0 13.6362 8.4811A0.7009 0.7009 0 0 0 13.6362 7.5189Z" fill="currentColor" stroke="none" fill-rule="evenodd" transform="rotate(-90 8 8)" /><path d="M8 4.5L13.2348 7.8313A0.2 0.2 0 0 1 13.2348 8.1687L8 11.5" transform="rotate(-90 8 8)" />        </svg>3.1%      </p>    </div>  </div>  <button class="btn btn-sm self-start" type="button" id="visitors-add">Add visitors</button></div><script>function roll(odometer, text, up) {  odometer.querySelector('.odometer-value').textContent = text  var numerals = text.split('').filter(function (character) { return character >= '0' && character <= '9' })  odometer.querySelectorAll('.odometer-digit').forEach(function (digit, index) {    var at = Number(digit.style.getPropertyValue('--digit')) || 0    var step = ((numerals[index] - at) % 10 + 10) % 10    if (!up && step) step -= 10    digit.style.setProperty('--digit', at + step)  })}var visitors = 12847document.getElementById('visitors-add').addEventListener('click', function () {  visitors += 1 + Math.floor(Math.random() * 240)  roll(document.getElementById('visitors'), visitors.toLocaleString('en-US'), true)})</script>
「訪問者を追加」を押すと、値が変わる桁が上に回転し、新しい人数を表示します。

#数値の記述方法

数値は、読み上げなどで使うテキストと画面表示用のマークアップの2通りで記述します。

  • odometer-valueには、画面上で見えないようにクリップしたテキストを入れます。スクリーンリーダーは一続きの文字列として読み上げ、ページ内検索やコピーでもこのテキストが使われます。
  • odometer-figuresは数値を描画する要素で、aria-hidden="true"を指定します。各桁に空のodometer-digitを一つ置き、その桁の数字を--digitに指定します。カンマ、小数点、通貨記号、単位など、動かない文字は各桁の要素の外に、表示順に沿ってプレーンテキストで記述します。

数字は Flexbox で横一列に並べるため、マークアップ内で桁の間に入れた改行やインデントが、数値の表示に空白として現れることはありません。単位の前などに空白を入れる場合は、改行なしスペース(&nbsp;)を使います。右から左に読むページでも、数字は通常の数値表記どおり左から右に並びます。

各桁には表示に現れない 0 があり、表示窓の幅を数字 1 文字分に保ち、ベースラインを行に揃えます。数字はすべて等幅なので、回転中も数値の隣にある要素は動きません。

#文字設定の継承

オドメーターは文字サイズ、太さ、色を親要素から継承します。表示窓の寸法、列内の数字の間隔、フェードの範囲もその文字設定に合わせるため、見出し、統計値、本文のいずれでも同じ仕組みが使えます。

見出しと本文での使用例

<h3 class="m(0) text-4xl font-semibold fg-text-strong">  <span class="odometer" id="total">    <span class="odometer-value">$1,280.50</span>    <span class="odometer-figures" aria-hidden="true">      $<span class="odometer-digit" style="--digit: 1"></span>, <span class="odometer-digit" style="--digit: 2"></span> <span class="odometer-digit" style="--digit: 8"></span> <span class="odometer-digit" style="--digit: 0"></span>. <span class="odometer-digit" style="--digit: 5"></span> <span class="odometer-digit" style="--digit: 0"></span>    </span>  </span></h3><p class="m(0) text-sm fg-text-muted">  Includes <span class="odometer" id="tax">    <span class="odometer-value">$106.71</span>    <span class="odometer-figures" aria-hidden="true">      $<span class="odometer-digit" style="--digit: 1"></span> <span class="odometer-digit" style="--digit: 0"></span> <span class="odometer-digit" style="--digit: 6"></span>. <span class="odometer-digit" style="--digit: 7"></span> <span class="odometer-digit" style="--digit: 1"></span>    </span>  </span> in tax.</p><button class="btn btn-sm mt-sm" type="button" id="stand" aria-pressed="false">  Add the stand</button><script>function roll(odometer, text, up) {  odometer.querySelector('.odometer-value').textContent = text  var numerals = text.split('').filter(function (character) { return character >= '0' && character <= '9' })  odometer.querySelectorAll('.odometer-digit').forEach(function (digit, index) {    var at = Number(digit.style.getPropertyValue('--digit')) || 0    var step = ((numerals[index] - at) % 10 + 10) % 10    if (!up && step) step -= 10    digit.style.setProperty('--digit', at + step)  })}var stand = document.getElementById('stand')stand.addEventListener('click', function () {  var added = stand.getAttribute('aria-pressed') !== 'true'  stand.setAttribute('aria-pressed', String(added))  stand.textContent = added ? 'Remove the stand' : 'Add the stand'  roll(document.getElementById('total'), added ? '$1,329.50' : '$1,280.50', added)  roll(document.getElementById('tax'), added ? '$110.79' : '$106.71', added)})</script>
「スタンドを追加」を押すと、合計額と税額の桁が上に回転します。もう一度押すと、下に回転して元の金額に戻ります。

#数値の増減に合わせた回転

各列には 0 から 9 までの数字が並び、各桁は指定された数字まで列に沿って上下に回転します。カウンターのようにすべての桁を数値の増減と同じ方向に回転させるには、--digit の値を 9 を超えて増やしたり、0 を下回って減らしたりします。表示窓には 10 を法とする剰余に対応した数字が表示されます。9 から 10 を指定すると 1 つ先の 0 に進み、0 から -1 を指定すると 1 つ前の 9 に戻ります。

上のプレビューでは、スクリプトでこの動作を実現しています。各桁の前回の設定値を読み取り、数値が増えたときは前に、減ったときは後ろに進めて、新しい数字に合わせます。どちらの方向にも移動は最大 9 ステップです。

var at = Number(digit.style.getPropertyValue('--digit')) || 0var step = ((numeral - at) % 10 + 10) % 10if (!up && step) step -= 10digit.style.setProperty('--digit', at + step)

スクリプトがなければ、オドメーターには数値がそのまま表示されます。数値を一度だけ描画するページなら、各桁に指定する値は 0 から 9 までで十分です。

#タイミング

各桁の回転には、テーマの基本時間の 2 倍にあたる --odometer-duration がかかります。すべての桁にテーマ共通のイージング曲線を使い、素早く動き始めて緩やかに止まるため、新しい数字を早い段階で読み取れます。各桁は直前の桁から --odometer-stagger 遅れて動き始めるので、変化が左から右に伝わります。表示窓では数字 1 文字分の高さがフェードのかからない範囲に収まり、次の数字はその範囲の外にあるため、静止時は現在の数値だけが見えます。

#アクセシビリティ

  • スクリーンリーダーが読み上げるのは odometer-value だけです。区切り記号や単位を含めて表示と一致させ、各桁と同時に更新してください。
  • odometer-figures には aria-hidden="true" が必要です。列の数字は空の代替テキストを持つ生成コンテンツですが、桁の間の記号は実際のテキストなので、そのままでは重複して読み上げられます。
  • オドメーターは既定でライブリージョンになっていません。ユーザーの選択後に合計が変わるなど、新しい値を読み上げる必要がある場合は、aria-live="polite" を odometer のルート要素に追加してください。視覚的に隠した値だけがルート要素のテキストなので、新しい数値は一度だけ読み上げられます。数秒ごとに自動で変わる数値には、この指定を使わないでください。
  • 動きを抑える設定では、数字は回転せずその場で切り替わります。強制カラーモードでは、数字にシステムの文字色が適用されます。

#ブラウザー対応

回転では、登録済みのカスタムプロパティ(@property)をイージング付きで変化させ、表示窓では mod() を使って 10 を法とする剰余を求めます。@property に対応していないブラウザーでは、数字はその場で切り替わります。mod() に対応していない場合は、--digit を 0 から 9 の範囲に収めてください。桁ごとの遅延には sibling-index() を使います。対応ブラウザー(Chromium)では桁が順に回転し、それ以外では同時に回転します。

#クラス

クラス説明
odometer配置先と同じ書体で表示する数値。等幅数字を使用し、固有の背景面やサイズは持ちません。
odometer-valueクリッピングで見えなくしたテキスト形式の数値。支援技術の読み上げ、ページ内検索、コピーの対象
odometer-figures支援技術から隠す表示用の数値。数字の間にあるコンマ、小数点、通貨記号、単位など、動かない要素はプレーンテキストで配置
odometer-digit1 桁を表す空要素です。縦に並ぶ数字のうち、--digit が示す位置の数字を 1 行分の窓に表示します。値が変わると数字列が回転します。

#カスタムプロパティ

プロパティ既定値説明
--digit表示する数字は 0 から 9 です。範囲外の値は、10 を法とする 0 から 9 の剰余で表示します。たとえば、9 から 10 に進むと 1 桁分回転して 0 になります。未設定の場合、スタイルシートは 0 として扱います。指定先:odometer-digit
--odometer-durationcalc(var(--duration-base) * 2)各桁が次の数字に切り替わるまでの回転時間
--odometer-stagger24ms各桁が一つ前の桁より遅れて動き始める時間

#ソース

@master/ui/components/odometer.css
/* * Odometer: a number whose digits roll to their new value, like the wheels of a mechanical counter. * It has no surface and no size of its own: it takes the type of the place it stands in, a heading, a stat's * figure or a line of text, and sets tabular figures so every digit is as wide as the next and nothing beside * the number moves while it rolls. * The number is written twice, once for each reader. odometer-value holds it as text, clipped out of sight, so * assistive technology reads one plain string, and find in page and a copy take the number; odometer-figures, * aria-hidden, draws it. In the figures each odometer-digit is a window one line tall onto a column of * numerals drawn by the stylesheet, standing at --digit. A change of --digit eases the column to the new * numeral, and a mask fades it out over and under the line, so numerals roll in and out of sight instead of * being cut. Anything else in the figures, a comma, a point, a currency or a unit, is plain text and stands still. * A wheel goes round: --digit may count on past 9 or below 0, and the window shows it modulo ten, so 9 to 10 * rolls on to 0 and 0 to -1 rolls back to 9, one step each, as a counter does. * Platform: the roll eases a registered number (@property) and the wheel reads it with mod(). Without * @property the numeral changes in place; without mod(), write --digit from 0 to 9. Where sibling-index() is * supported, each digit sets off a little after the one before it, so a change reads left to right; elsewhere * they roll together. Under reduced motion the numerals change in place. */@property --odometer-at {  syntax: '<number>';  inherits: true;  initial-value: 0;}@layer components.ui {  .odometer {    --odometer-duration: calc(var(--duration-base) * 2);    --odometer-stagger: 24ms;    position: relative;    font-variant-numeric: tabular-nums;    white-space: nowrap;  }  /* The number as text, for assistive technology, find in page and copying; clipped out of sight. */  .odometer-value {    position: absolute;    width: 1px;    height: 1px;    margin: -1px;    padding: 0;    overflow: hidden;    clip-path: inset(50%);    white-space: nowrap;  }  /*   * The drawing. A flex row, so the whitespace between digits in the markup never becomes a space in the   * number, and left to right in any direction of text, as numbers are written.   */  .odometer-figures {    display: inline-flex;    align-items: baseline;    direction: ltr;    unicode-bidi: isolate;    user-select: none;    -webkit-user-select: none;  }  /*   * One digit. A hidden zero gives the window a numeral's width and the line's baseline. The window is the   * line itself, reaching a hair over and under it, and gives that hair back with a negative margin, so the   * line keeps its height and a rolling numeral never crosses into the line above or below. The mask is clear   * across a numeral's own height and fades out by the window's edge, as the wheel turns away. The window   * clips the column too: a mask hides the other numerals but would leave them making the page scroll.   */  .odometer-digit {    --odometer-reach: .08em;    --odometer-at: var(--digit, 0);    position: relative;    display: block;    padding-block: var(--odometer-reach);    margin-block: calc(var(--odometer-reach) * -1);    overflow: clip;    mask-image: linear-gradient(transparent, black calc(50% - .38em), black calc(50% + .38em), transparent);    transition: --odometer-at var(--odometer-duration) var(--easing-glide);    @supports (width: calc(sibling-index() * 1px)) {      transition-delay: calc((sibling-index() - 1) * var(--odometer-stagger));    }    /*     * The column: 0 to 9 and 0 again, so a roll past 9 ends on a 0 that looks like the first. Numerals are     * spaced .4em further apart than lines of text, so the next one waits outside the box, and each stands in     * the middle of its line, as the line's own numeral does.     */    &::before {      content: '0\A 1\A 2\A 3\A 4\A 5\A 6\A 7\A 8\A 9\A 0';      content: '0\A 1\A 2\A 3\A 4\A 5\A 6\A 7\A 8\A 9\A 0' / '';      position: absolute;      inset: calc(var(--odometer-reach) - .2em) 0 auto;      line-height: calc(1lh + .4em);      text-align: center;      white-space: pre;      translate: 0 calc(var(--odometer-at) * -1lh);      @supports (width: calc(mod(3px, 2px))) {        translate: 0 calc(mod(var(--odometer-at), 10) * -1lh);      }    }    &::after {      content: '0';      content: '0' / '';      visibility: hidden;    }    @media (prefers-reduced-motion: reduce) {      transition: none;    }  }}