Slider
Pick a number by dragging. It is the native <input type="range"> with the class .slider, so arrow keys, Home/End, PgUp/PgDn, forms and screen readers all work. momo.js paints the filled part and shows the value.
Basic
<input class="slider" type="range" min="0" max="100" value="40" aria-label="Volume">With a label and value
Put an <output for="id"> anywhere: it shows the value as you drag. data-prefix and data-suffix add text around the number.
<div class="slider-field">
<div class="slider-head">
<label class="form-label" for="sl-budget">Monthly budget</label>
<output class="slider-output" for="sl-budget" data-prefix="$">1,200</output>
</div>
<input class="slider" id="sl-budget" type="range" min="0" max="5000" step="100" value="1200">
</div>Steps with labels
Add a .slider-ticks row under it. Each label sits under its spot, so use one label per step.
<div class="slider-field">
<div class="slider-head">
<label class="form-label" for="sl-size">Team size</label>
<output class="slider-output" for="sl-size" data-suffix=" people">10</output>
</div>
<div>
<input class="slider" id="sl-size" type="range" min="0" max="50" step="10" value="10">
<div class="slider-ticks" aria-hidden="true"><span>0</span><span>10</span><span>20</span><span>30</span><span>40</span><span>50</span></div>
</div>
</div>Min and max
Two sliders inside .slider-range share one track, for price and date filters. The thumbs never pass each other. Give each one its own aria-label.
<div class="slider-field">
<div class="slider-head">
<span class="form-label">Price</span>
<span class="slider-output"><output for="sl-lo" data-prefix="$">20</output> to <output for="sl-hi" data-prefix="$">80</output></span>
</div>
<div class="slider-range">
<input class="slider" id="sl-lo" type="range" min="0" max="100" value="20" aria-label="Lowest price">
<input class="slider" id="sl-hi" type="range" min="0" max="100" value="80" aria-label="Highest price">
</div>
</div>Icons on both ends
<div class="slider-row">
<i class="mi mi-volume-1" aria-hidden="true"></i>
<input class="slider slider-round" type="range" min="0" max="10" value="6" aria-label="Volume">
<i class="mi mi-volume-2" aria-hidden="true"></i>
</div>Colors, shapes and sizes
.slider-<color> for any color, .slider-round for a round thumb, .slider-sm and .slider-lg. On touch screens the thumb grows so it is easy to grab.
<div class="form-grid">
<input class="slider slider-sm slider-green" type="range" value="30" aria-label="Small green">
<input class="slider slider-violet" type="range" value="50" aria-label="Violet">
<input class="slider slider-lg slider-yellow slider-round" type="range" value="70" aria-label="Large yellow">
<input class="slider" type="range" value="45" disabled aria-label="Disabled">
</div>JavaScript
Listen to the native input (while dragging) and change (when let go) events. If you set value in code, call Momo.slider.refresh(input) so the fill and the output follow. Form resets are handled for you.
const s = document.querySelector('#sl-budget');
s.value = 2500;
Momo.slider.refresh(s);
Accessibility
- Every slider needs a name: a
<label for>or anaria-label. - For values that are not plain numbers, add
aria-valuetext("Medium"). - The tick labels are hidden from screen readers; the slider already says its value.
Variables
| Variable | Default |
|---|---|
--slider-color | brand |
--slider-track | surface |
--slider-height | .75rem |
--slider-thumb | 1.5rem (1.75rem on touch) |