網站導覽

v2.1.0
表單

範圍滑桿

在同一條軌道上選擇兩個值,例如最低與最高價格,並填色標示兩值之間的範圍。

本頁內容

範圍滑桿可在同一個刻度上選取兩個值,例如最低與最高價格,或片段的起點與終點。它將兩個原生滑桿放在同一個群組中。群組繪製一條共用軌道,並在兩個滑塊之間填色。每個輸入元素各自保有原生鍵盤操作、標籤和表單值。

範圍滑桿

<div class="flex flex-col gap-sm max-w(360) mx-auto">  <div class="flex items-baseline justify-between text-sm">    <span class="fg-text-strong" id="price">Price</span>    <output class="fg-text-muted" id="price-output">$120 to $380</output>  </div>  <div class="range" role="group" aria-labelledby="price">    <input type="range" class="slider range-start" min="0" max="500" step="10" value="120" aria-label="Lowest price" aria-valuetext="$120">    <input type="range" class="slider range-end" min="0" max="500" step="10" value="380" aria-label="Highest price" aria-valuetext="$380">  </div></div><script>(function () {var range = document.querySelector('.range')var output = document.getElementById('price-output')range.addEventListener('input', function () {  var values = Array.from(range.querySelectorAll('.slider'), function (slider) {    // A thumb says its value as the page prints it, in dollars, not as a bare number.    slider.setAttribute('aria-valuetext', '$' + slider.value)    return Number(slider.value)  })  output.textContent = '$' + Math.min.apply(null, values) + ' to $' + Math.max.apply(null, values)})})()</script>
拖曳任一滑塊,或將焦點移到滑塊後使用方向鍵。腳本只負責更新軌道上方的兩個數值。

#無須腳本即可填色

和單一滑桿的填色方式一樣,每個滑塊都在自己的輸入元素內建立檢視時間軸。群組透過 timeline-scope 取得兩條時間軸,並各自執行一段動畫,藉此取得兩個百分比。填色會從較小值延伸到較大值,所以兩個滑塊可以交錯。填色仍會落在兩者之間,範圍起點也始終是較小的值。

在表單處理程式中也依照相同方式讀取這組數值,以較小值作為下限。如果兩值不能交錯,或必須保持一定間距,請掛載行為。填色本身不受此限制影響。

瀏覽器若不支援捲動驅動動畫,兩個滑塊下方只會顯示未填色的軌道,兩個輸入元素仍可正常操作。Chromium 和 Safari 26 支援填色。

#滑塊如何共用軌道

兩個輸入元素位於同一個網格儲存格。各自隱藏自己的軌道,只在滑塊上接收指標操作。按下兩個滑塊之間的區域不會觸發任何一個滑塊,另一個輸入元素的空軌道也不會擋住滑塊。正在移動的滑塊會顯示在最上層。按下軌道空白處不會有任何效果,因為無法判斷要移動哪一個滑塊。

#保持間距

range 行為會讓兩個滑塊保持分開:range-start 永遠不會越過 range-end,設定 distance 時,兩者會在相隔這段距離處停下。停下的是正在移動的滑塊,靜止的滑塊永遠不會被推動。它會在移動滑塊所觸發的 input 事件中修正數值,搶在輸入元素本身或範圍周圍的任何元素收到事件之前,因此每個監聽器讀到的都是保持間距的一組數值,滑塊也不會顯示被拒絕的值。鍵盤操作、拖曳和表單值仍由輸入元素自己處理。

保持間距

<div class="flex flex-col gap-sm max-w(360) mx-auto">  <div class="flex items-baseline justify-between text-sm">    <span class="fg-text-strong" id="hours">Opening hours</span>    <output class="fg-text-muted" id="hours-output">9:00 to 17:00</output>  </div>  <div class="range" id="hours-range" role="group" aria-labelledby="hours">    <input type="range" class="slider range-start" min="0" max="24" value="9" aria-label="Opens at" aria-valuetext="9:00">    <input type="range" class="slider range-end" min="0" max="24" value="17" aria-label="Closes at" aria-valuetext="17:00">  </div></div><script>addEventListener('DOMContentLoaded', function () {var range = document.getElementById('hours-range')MasterUI.range(range, { distance: 2 })range.addEventListener('input', function () {  var start = range.querySelector('.range-start')  var end = range.querySelector('.range-end')  // Each thumb says its hour as a time, as the page prints it.  start.setAttribute('aria-valuetext', start.value + ':00')  end.setAttribute('aria-valuetext', end.value + ':00')  document.getElementById('hours-output').textContent = start.value + ':00 to ' + end.value + ':00'})})</script><script type="module">  import { attach } from '@master/ui/behaviors'  attach()</script>
將任一滑塊拖向另一個:兩者會在相隔兩小時處停下。

attach() 會讓範圍滑桿維持平台原本的行為,因此兩個滑塊可以交錯。若要讓它們保持分開,請對它呼叫 range,並像這個預覽一樣用 distance 指定要相隔多遠;對同一個根元素再次呼叫會取代前一次。MasterUI 是預覽載入的套件;在專案中請匯入 range:

app.js
import { range } from '@master/ui/behaviors/range.js'const detach = range(document.querySelector('.range'), { distance: 2 })

間距以輸入元素自己的單位計算。請給 step 的倍數:輸入元素會將寫入的值調整至最接近的有效步進值,行為則會選取最接近且仍符合間距要求的步進值。

#停用

停用兩個輸入元素。滑塊和填色會一起淡化。

停用

<div class="range max-w(360) mx-auto" role="group" aria-label="Seats, set by your plan">  <input type="range" class="slider range-start" value="20" aria-label="Fewest seats" disabled>  <input type="range" class="slider range-end" value="60" aria-label="Most seats" disabled></div>
<div className="range max-w(360) mx-auto" role="group" aria-label="Seats, set by your plan">  <input type="range" className="slider range-start" defaultValue="20" aria-label="Fewest seats" disabled />  <input type="range" className="slider range-end" defaultValue="60" aria-label="Most seats" disabled /></div>
<div class="range max-w(360) mx-auto" role="group" aria-label="Seats, set by your plan">  <input type="range" class="slider range-start" value="20" aria-label="Fewest seats" disabled>  <input type="range" class="slider range-end" value="60" aria-label="Most seats" disabled></div>
<template>  <div class="range max-w(360) mx-auto" role="group" aria-label="Seats, set by your plan">    <input type="range" class="slider range-start" value="20" aria-label="Fewest seats" disabled>    <input type="range" class="slider range-end" value="60" aria-label="Most seats" disabled>  </div></template>

#無障礙

  • 為群組設定 role="group" 和名稱,並為每個輸入元素設定各自的 aria-label。請用「最低價格」和「最高價格」這類具體名稱,避免只寫「起點」和「終點」。
  • 在軌道旁顯示兩個數值。滑塊本身不會顯示數字。
  • 每個滑塊都有自己的 Tab 停駐點,並支援原生滑桿的鍵盤操作。在觸控螢幕上,滑塊的尺寸為 24px:滑塊本身就是唯一的觸控區,無法延伸到邊緣之外,因此它是元件庫中唯一會放大的部分。
  • 若精確輸入比直覺調整範圍更重要,請另外提供兩個數字步進器或兩個數字欄位。

#類別

類別說明
range包住兩個滑桿的群組。兩個滑桿共用一條軌道,填色介於兩個滑塊之間。為群組設定 role="group" 和無障礙名稱
range-start用於其中一個滑桿輸入控制項,為其滑塊命名,供填色區參照。
range-end用於另一個滑桿輸入控制項,為其滑塊命名,供填色區參照。

#自訂屬性

屬性預設值說明
--range-track.375rem軌道粗細
--range-colorvar(--color-surface-inverse)填色

#原始碼

@master/ui/components/range.css
/* * Range: two values on one track: a lowest and a highest price, the start and the end of a clip. * It is two native range inputs, each a slider, laid over each other in one group. The group draws the track * and fills it with ink between the two thumbs; the inputs keep only their thumbs, and only the thumbs take * the pointer, so either one can be reached where they stand close. Each input keeps its own keys, its own * label and its own value in the form. * The fill needs no script. Each thumb is a view timeline in its own input, and the group reads both: it * paints from the lesser reading to the greater, so the thumbs may cross and the fill is still between them. * Without scroll-driven animations the track stays a plain well under the two thumbs. */@property --range-from {  syntax: '<percentage>';  inherits: true;  initial-value: 0%;}@property --range-to {  syntax: '<percentage>';  inherits: true;  initial-value: 100%;}@layer components.ui {  .range {    --range-track: .375rem;    --range-color: var(--color-surface-inverse);    /* Until a timeline says otherwise the fill is empty: both ends stand at the start. */    --range-fill-from: 0%;    --range-fill-to: 0%;    position: relative;    /* The slider in use comes forward within the range, never over the page around it. */    isolation: isolate;    display: grid;    min-width: 0;    margin: 0;    padding: 0;    border: 0;    /* The track: one well for both thumbs, filled between them. */    &::before {      content: '';      position: absolute;      inset: 50% 0 auto;      height: var(--range-track);      translate: 0 -50%;      border-radius: 999px;      background:        linear-gradient(90deg, transparent var(--range-fill-from), var(--range-color) 0, var(--range-color) var(--range-fill-to), transparent 0)        var(--color-surface-inset);      box-shadow: var(--shadow-pressed);      pointer-events: none;    }    &:dir(rtl)::before {      background:        linear-gradient(270deg, transparent var(--range-fill-from), var(--range-color) 0, var(--range-color) var(--range-fill-to), transparent 0)        var(--color-surface-inset);    }    /*     * Disabled, the range fades as one control, as a lone slider fades as one input. Were each input faded on its     * own, a thumb would turn translucent over the group's track and the fill would show through it.     */    &:has(.slider:disabled) {      opacity: .5;    }    /*     * The two sliders share one cell. Each gives up its own track and takes the pointer only on its thumb.     * The one being moved comes to the front, so a thumb that was under the other can be picked up again.     */    .slider {      --slider-color: transparent;      /* Positioned, so the thumbs stand over the group's track. */      position: relative;      grid-area: 1 / 1;      pointer-events: none;      &:is(:active, :focus-visible) {        z-index: 1;      }      /* The group does the fading, so the thumb stays solid over the track. */      &:disabled {        opacity: 1;      }      &::-webkit-slider-runnable-track {        background: transparent;        box-shadow: none;      }      &::-webkit-slider-thumb {        pointer-events: auto;      }      &::-moz-range-track {        background: transparent;        box-shadow: none;      }      &::-moz-range-progress {        background: transparent;      }      &::-moz-range-thumb {        pointer-events: auto;      }      /*       * A thumb here is its own only target, and a thumb cannot carry a pseudo-element to reach further, so on       * touch it is drawn at 24px, the least a finger is owed. It is the one part of the library that grows.       */      @media (pointer: coarse) {        --slider-thumb: 1.5rem;      }    }    @media (forced-colors: active) {      --range-color: Highlight;      &::before {        forced-color-adjust: none;        background:          linear-gradient(90deg, transparent var(--range-fill-from), var(--range-color) 0, var(--range-color) var(--range-fill-to), transparent 0)          Canvas;        box-shadow: inset 0 0 0 1px CanvasText;      }      &:dir(rtl)::before {        background:          linear-gradient(270deg, transparent var(--range-fill-from), var(--range-color) 0, var(--range-color) var(--range-fill-to), transparent 0)          Canvas;      }      /* Disabled, the fill takes the system's disabled ink, as a lone slider's does. */      &:has(.slider:disabled) {        --range-color: GrayText;      }    }  }  /*   * The fill. Each thumb names a view timeline in its input, as a slider's does, and the group makes both   * names its own, runs one animation on each and reads two percentages.   */  @supports (view-timeline: --range-start inline) and (timeline-scope: --range-start) {    .range {      --range-fill-from: min(var(--range-from), var(--range-to));      --range-fill-to: max(var(--range-from), var(--range-to));      timeline-scope: --range-start, --range-end;      animation-name: range-from, range-to;      animation-timing-function: linear;      animation-fill-mode: both;      animation-timeline: --range-start, --range-end;      animation-range: contain;      &:dir(rtl) {        animation-direction: reverse;      }      /* A slider inside a range hands its timeline to the group and runs no fill of its own. */      .slider {        timeline-scope: none;        animation-name: none;      }      .range-start::-webkit-slider-thumb {        view-timeline: --range-start inline;        view-timeline-inset: var(--slider-bleed);      }      .range-end::-webkit-slider-thumb {        view-timeline: --range-end inline;        view-timeline-inset: var(--slider-bleed);      }    }  }  /* The two ends. The classes only name the thumbs for the fill; which value is the lower is the form's to read. */  .range-start, .range-end {    min-width: 0;  }}@keyframes range-from {  from {    --range-from: 100%;  }  to {    --range-from: 0%;  }}@keyframes range-to {  from {    --range-to: 100%;  }  to {    --range-to: 0%;  }}