ナビゲーション

v2.1.0
フォーム

スライダー

細くくぼんだトラックに、真珠調のつまみを備えたネイティブの範囲入力。

このページの内容

スライダーは、音量、不透明度、ズームなど、正確な数値よりも範囲内の位置が重要な値を選ぶために使います。ネイティブの<input type="range">に.sliderを指定します。キーボード操作、フォームでの値の扱い、タッチでのドラッグはブラウザーが担います。クラスは、細くくぼんだトラックと、スイッチと同じ真珠調のつまみを描画します。

スライダー

<label class="field">Volume <input type="range" class="slider" value="60"></label>
<label className="field">Volume <input type="range" className="slider" defaultValue="60" /></label>
<label class="field">Volume <input type="range" class="slider" value="60"></label>
<template>  <label class="field">Volume <input type="range" class="slider" value="60"></label></template>

#トラックの塗りつぶし

トラックの始端からつまみまでを、スクリプトなしで文字色で塗りつぶします。Firefoxではネイティブ機能で描画されます。ほかのブラウザーでは、スタイルシートがスクロール駆動アニメーションでつまみの位置を取得します。入力要素をつまみの位置を測るためのスクロールコンテナーとし、つまみのビュータイムラインが示す進行位置までトラックを塗ります。どちらの仕組みにも対応していない場合は、トラックは塗りつぶされず、つまみの位置で値を示します。

刻み幅と範囲

<label class="field">  Zoom <input type="range" class="slider" min="50" max="200" step="25" value="100"></label><label class="field">Opacity <input type="range" class="slider" value="35" disabled></label>
<>  <label className="field">    Zoom <input type="range" className="slider" min="50" max="200" step="25" defaultValue="100" />  </label>  <label className="field">    Opacity <input type="range" className="slider" defaultValue="35" disabled />  </label></>
<label class="field">  Zoom <input type="range" class="slider" min="50" max="200" step="25" value="100"></label><label class="field">Opacity <input type="range" class="slider" value="35" disabled></label>
<template>  <label class="field">    Zoom <input type="range" class="slider" min="50" max="200" step="25" value="100">  </label>  <label class="field">Opacity <input type="range" class="slider" value="35" disabled></label></template>

#縦向き

slider-verticalを指定すると、スライダーが縦向きになり、最小値が下端に来ます。ネイティブの入力要素をwriting-mode: vertical-lrとdirection: rtlで縦向きにするため、ブラウザーは縦向きの範囲入力として扱います。ArrowUpや上方向へのドラッグで値が大きくなり、支援技術には縦向きのスライダーとして伝わります。スタイルシートは入力要素自身の軸に沿って各部のサイズを決めるため、トラック、塗り、つまみ、フォーカス、無効状態、タッチ時のサイズは横向きの場合と同じです。

長さは指定した高さ(この例ではh(160))になり、指定しない場合はプラットフォーム標準の範囲入力の長さになります。

縦向き

<label class="field items-center">  <input type="range" class="slider slider-vertical h(160)" value="70"> Bass</label><label class="field items-center">  <input type="range" class="slider slider-vertical h(160)" value="45"> Mid</label><label class="field items-center">  <input type="range" class="slider slider-vertical h(160)" value="60"> Treble</label><label class="field items-center">  <input type="range" class="slider slider-vertical h(160)" value="30" disabled> Gain</label>
<>  <label className="field items-center">    <input type="range" className="slider slider-vertical h(160)" defaultValue="70" /> Bass  </label>  <label className="field items-center">    <input type="range" className="slider slider-vertical h(160)" defaultValue="45" /> Mid  </label>  <label className="field items-center">    <input type="range" className="slider slider-vertical h(160)" defaultValue="60" /> Treble  </label>  <label className="field items-center">    <input type="range" className="slider slider-vertical h(160)" defaultValue="30" disabled /> Gain  </label></>
<label class="field items-center">  <input type="range" class="slider slider-vertical h(160)" value="70"> Bass</label><label class="field items-center">  <input type="range" class="slider slider-vertical h(160)" value="45"> Mid</label><label class="field items-center">  <input type="range" class="slider slider-vertical h(160)" value="60"> Treble</label><label class="field items-center">  <input type="range" class="slider slider-vertical h(160)" value="30" disabled> Gain</label>
<template>  <label class="field items-center">    <input type="range" class="slider slider-vertical h(160)" value="70"> Bass  </label>  <label class="field items-center">    <input type="range" class="slider slider-vertical h(160)" value="45"> Mid  </label>  <label class="field items-center">    <input type="range" class="slider slider-vertical h(160)" value="60"> Treble  </label>  <label class="field items-center">    <input type="range" class="slider slider-vertical h(160)" value="30" disabled> Gain  </label></template>
つまみを上下にドラッグするか、フォーカスして矢印キーを使います。

範囲入力の縦向き表示に対応していないブラウザーでは、指定した長さのまま横向きに表示され、そのまま操作できます。Chromium、Safari 17.4、Firefoxは縦向き表示に対応しています。

#アクセシビリティ

  • スライダーには、fieldによるラベルまたはaria-labelで名前を付けます。
  • スライダー自体には数値が表示されません。正確な値が重要な場合は、スライダーの横に値を表示するか、type="number"を指定した入力欄を使います。
  • タッチスクリーンでもつまみのサイズは変わりません。トラックを横切る方向に44pxの操作領域を確保するため、スライダーの見た目を変えずに指で操作しやすくなります。

#クラス

クラス説明
slider細いくぼみに真珠のようなつまみを配した range 入力です。つまみの手前までを文字色で塗りつぶします。
slider-vertical縦向きで、最小値が下端になります。writing-mode: vertical-lr と direction: rtl でネイティブの入力を回転させるため、ArrowUp で値が上がります。長さは指定された高さ、またはプラットフォーム標準の長さです。slider と併用します

#カスタムプロパティ

プロパティ既定値説明
--slider-track.375remトラックの太さ
--slider-thumb1.125remつまみの直径。タッチ画面では入力要素がトラックの周囲に44pxまで広がります。範囲スライダーではつまみ自体が唯一の操作領域になるため、つまみは1.5rem以上になります
--slider-colorvar(--color-surface-inverse)塗りつぶし色
--slider-backgroundvar(--color-surface-inset)塗りの下のトラックに描く色またはグラデーションです。カラーピッカーの色相などに使います。

#ソース

@master/ui/components/slider.css
/* * Slider: a native range input: a thin well with a pearl thumb, the same thumb a switch has. * The part before the thumb fills with ink: Firefox draws it natively, and where scroll-driven animations * exist the thumb's own position in the track drives the fill, with no script. Elsewhere the track stays a well. * Sizes are logical, so slider-vertical only turns the input with writing-mode: vertical-lr and direction: rtl, * which the platform reads as a vertical range with its lowest value at the foot. * --slider-background is what the track is painted with under the fill: the well's tone, or a gradient such as * the hues of a color picker, which sets --slider-color to transparent so nothing covers it. */@property --slider-fill {  syntax: '<percentage>';  inherits: true;  initial-value: 0%;}@layer components.ui {  .slider {    --slider-track: .375rem;    --slider-thumb: 1.125rem;    --slider-color: var(--color-surface-inverse);    --slider-background: var(--color-surface-inset);    /* Room around the thumb for its shadow and its focus ring; the negative margin keeps the track on the edge. */    --slider-bleed: 5px;    display: block;    box-sizing: content-box;    inline-size: 100%;    block-size: var(--slider-thumb);    margin: calc(-1 * var(--slider-bleed));    padding: var(--slider-bleed);    border: 0;    background: transparent;    appearance: none;    cursor: pointer;    -webkit-tap-highlight-color: transparent;    &:focus-visible {      outline: none;    }    &::-webkit-slider-runnable-track {      block-size: var(--slider-track);      border-radius: 999px;      background: linear-gradient(90deg, var(--slider-color) var(--slider-fill), transparent 0), var(--slider-background);      box-shadow: var(--shadow-pressed);    }    &::-webkit-slider-thumb {      inline-size: var(--slider-thumb);      block-size: var(--slider-thumb);      margin-block-start: calc((var(--slider-track) - var(--slider-thumb)) / 2);      border: 0;      border-radius: 999px;      @apply --surface-thumb;      box-shadow: 0 0 0 .5px var(--color-line-strong), 0 1px 2px -.5px var(--color-shade-1), 0 2px 4px -1px var(--color-shade-2), 0 4px 8px -4px var(--color-shade-3);      appearance: none;      transition: box-shadow var(--duration-quick) var(--easing-glide), scale var(--duration-quick) var(--easing-glide);    }    &::-moz-range-track {      block-size: var(--slider-track);      border-radius: 999px;      background: var(--slider-background);      box-shadow: var(--shadow-pressed);    }    &::-moz-range-progress {      block-size: var(--slider-track);      border-radius: 999px;      background: var(--slider-color);    }    &::-moz-range-thumb {      inline-size: var(--slider-thumb);      block-size: var(--slider-thumb);      border: 0;      border-radius: 999px;      @apply --surface-thumb;      box-shadow: 0 0 0 .5px var(--color-line-strong), 0 1px 2px -.5px var(--color-shade-1), 0 2px 4px -1px var(--color-shade-2), 0 4px 8px -4px var(--color-shade-3);      transition: box-shadow var(--duration-quick) var(--easing-glide), scale var(--duration-quick) var(--easing-glide);    }    /*     * Under the pointer the thumb takes the firmer contour of a hovered control, the change its box-shadow     * transition is there for. Forced colors keep their own thumb.     */    @media (hover: hover) and (forced-colors: none) {      &:hover:not(:disabled)::-webkit-slider-thumb {        box-shadow: 0 0 0 .5px var(--color-line-mark), 0 1px 2px -.5px var(--color-shade-1), 0 2px 4px -1px var(--color-shade-2), 0 4px 8px -4px var(--color-shade-3);      }      &:hover:not(:disabled)::-moz-range-thumb {        box-shadow: 0 0 0 .5px var(--color-line-mark), 0 1px 2px -.5px var(--color-shade-1), 0 2px 4px -1px var(--color-shade-2), 0 4px 8px -4px var(--color-shade-3);      }    }    /* Held, the thumb grows a little under the finger, in either engine; each pseudo-element has a rule of its own. */    &:active:not(:disabled)::-webkit-slider-thumb {      scale: 1.08;    }    &:active:not(:disabled)::-moz-range-thumb {      scale: 1.08;    }    &:focus-visible::-webkit-slider-thumb {      outline: 2px solid var(--color-focus);      outline-offset: 2px;    }    &:focus-visible::-moz-range-thumb {      outline: 2px solid var(--color-focus);      outline-offset: 2px;    }    &:disabled {      opacity: .5;      cursor: not-allowed;    }    /*     * Upright: the inline axis runs down the page and right to left turns it, so the lowest value is at the     * foot, the arrow keys move up and down, and the fill rises from below. Its length is the height it is     * given, or the platform's own length of a range input when it is given none.     */    &.slider-vertical {      writing-mode: vertical-lr;      direction: rtl;      display: inline-block;      vertical-align: middle;      &::-webkit-slider-runnable-track {        background: linear-gradient(0deg, var(--slider-color) var(--slider-fill), transparent 0), var(--slider-background);      }    }    /*     * On touch the input reaches 44px across the track, into the room around it, so a finger has the target and     * the thumb keeps its size. Only the cross axis grows, so the slider never reaches over a control beside it.     */    @media (pointer: coarse) {      margin-block: calc((var(--slider-thumb) - 2.75rem) / 2);      padding-block: calc((2.75rem - var(--slider-thumb)) / 2);    }    @media (prefers-reduced-motion: reduce) {      &:active:not(:disabled)::-webkit-slider-thumb {        scale: none;      }      &:active:not(:disabled)::-moz-range-thumb {        scale: none;      }    }    /*     * The colors are set as the properties the tracks read, so the right-to-left and upright tracks, whose     * own rules come later, are painted in system colors too.     */    @media (forced-colors: active) {      forced-color-adjust: none;      --slider-color: Highlight;      --slider-background: Canvas;      &::-webkit-slider-runnable-track {        background: linear-gradient(90deg, var(--slider-color) var(--slider-fill), transparent 0) Canvas;        box-shadow: inset 0 0 0 1px CanvasText;      }      &.slider-vertical::-webkit-slider-runnable-track {        background: linear-gradient(0deg, var(--slider-color) var(--slider-fill), transparent 0) Canvas;      }      &::-webkit-slider-thumb {        background: ButtonText;        box-shadow: none;      }      &::-moz-range-track {        background: Canvas;        box-shadow: inset 0 0 0 1px CanvasText;      }      &::-moz-range-thumb {        background: ButtonText;        box-shadow: none;      }      /* The input opts out of forced colors, so the focus ring is given the system's own. */      &:focus-visible::-webkit-slider-thumb {        outline-color: Highlight;      }      &:focus-visible::-moz-range-thumb {        outline-color: Highlight;      }      /* Disabled, the fill and the thumb take the system's disabled ink, which the input's opt-out would keep from them. */      &:disabled {        --slider-color: GrayText;      }      &:disabled::-webkit-slider-thumb {        background: GrayText;      }      &:disabled::-moz-range-thumb {        background: GrayText;      }    }  }  /*   * The fill. The input is the scroll container the thumb is measured in, so a view timeline on the thumb   * reads how far along the track it is; the input runs a one-property animation on that timeline and the   * track paints up to it. Clipping stays outside the bleed, so the thumb's shadow and focus ring are whole.   */  @supports (view-timeline: --slider-thumb inline) and (timeline-scope: --slider-thumb) {    .slider {      overflow: hidden;      timeline-scope: --slider-thumb;      /* Longhands: a shorthand would reset the timeline, and tools that fold one into it write a value no browser reads. */      animation-name: slider-fill;      animation-timing-function: linear;      animation-fill-mode: both;      animation-timeline: --slider-thumb;      animation-range: contain;      &::-webkit-slider-thumb {        view-timeline: --slider-thumb inline;        view-timeline-inset: var(--slider-bleed);      }      /* Right to left, the value grows toward the left: the reading and the paint both turn around. */      &:dir(rtl) {        animation-direction: reverse;      }      &:dir(rtl)::-webkit-slider-runnable-track {        background: linear-gradient(270deg, var(--slider-color) var(--slider-fill), transparent 0), var(--slider-background);      }      /* Upright, the timeline runs down the input and the value up it, in a page of either direction. */      &.slider-vertical {        animation-direction: reverse;      }      &.slider-vertical::-webkit-slider-runnable-track {        background: linear-gradient(0deg, var(--slider-color) var(--slider-fill), transparent 0), var(--slider-background);      }    }  }}@keyframes slider-fill {  from {    --slider-fill: 100%;  }  to {    --slider-fill: 0%;  }}