網站導覽

v2.1.0
表單

滑桿

原生範圍輸入控制項,搭配纖細的凹槽和珍珠般的滑塊。

本頁內容

當相對位置比精確數值更重要時,可以用滑桿從一定範圍內選值,例如調整音量、不透明度或縮放比例。將 .slider 套用到原生 <input type="range">。瀏覽器會保留鍵盤操作、表單值和觸控拖曳。這個類別會繪製纖細的凹槽,並使用與開關相同的珍珠滑塊。

滑桿

<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,讓手指有足夠的操作空間,滑桿外觀維持原樣。

#類別

類別說明
sliderrange 輸入控制項採用細長的凹槽式軌道與珠光滑塊,從軌道起點到滑塊以​​前景色填滿。
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%;  }}