导航

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范围输入控件,细窄的凹槽配有珍珠色滑块,滑块之前的部分以墨色填充
slider-vertical纵向放置,最小值在底部:writing-mode: vertical-lr 和 direction: rtl 将原生 input 转为纵向,因此 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%;  }}