Dual-Thumb Interval Slider
双滑块区间选择
Two independently focusable thumbs define one bounded interval without crossing.
Where to use it
Choose price, date, or frequency intervals in a compact control. Label the lower and upper bounds separately so users can adjust either end without losing track of the range.
Practical example
A campsite finder puts two thumbs on a nightly price rail. Sliding the left thumb to ¥200 and the right one to ¥500 filters the same list to stays in that range.
AI prompt
Create a dual-thumb interval slider for [filter]. Keep handles in fixed focus order, constrain each thumb using its neighbor, and expose the current value and valid bounds independently.
Example code
Save the standalone HTML as a file and open it in your browser. It uses the same code as the preview.
View HTML
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Dual-Thumb Interval Slider</title>
<style>
:root {
color-scheme: dark;
--demo-bg: #171629;
--demo-ink: #f8f5ff;
--demo-accent: #b9a6ff;
--demo-muted: #bfbbd3;
font-family: system-ui, sans-serif;
color: var(--demo-ink);
background: var(--demo-bg);
}
*, *::before, *::after { box-sizing: border-box; }
body { margin: 0; min-width: 0; }
button, input { font: inherit; }
.demo {
display: grid;
place-items: center;
align-content: center;
min-height: 100vh;
padding: 14px;
overflow: hidden;
background: radial-gradient(ellipse at 50% 24%, #28233f, var(--demo-bg) 78%);
}
.demo-button {
border: 1px solid #ffffff3d;
border-radius: 7px;
background: #ffffff12;
color: var(--demo-ink);
padding: 6px 11px;
font-size: 12px;
cursor: pointer;
}
:focus-visible {
outline: 2px solid var(--demo-accent);
outline-offset: 3px;
}
.ui { width: min(100%, 238px); display: grid; gap: 8px; position: relative; font-size: 12px; line-height: 1.35; }
.ui .surface { min-width: 0; border: 1px solid #676591; border-radius: 10px; background: #282744; padding: 8px; }
.ui .bar { display: flex; align-items: center; justify-content: space-between; gap: 6px; }
.ui .actions { display: flex; align-items: center; justify-content: center; flex-wrap: wrap; gap: 5px; }
.ui .muted { color: #cecae9; font-size: 11px; }
.ui .center { text-align: center; }
.ui .mono { font: 11px ui-monospace, monospace; font-variant-numeric: tabular-nums; }
.ui p { margin: 0; }
.ui .button { min-height: 30px; padding: 4px 7px; border-radius: 7px; border: 1px solid #79779f; background: #3d3a60; color: #f9f5ff; font-size: 11px; font-weight: 650; cursor: pointer; }
.ui .button:hover { background: #4e4976; }
.ui .primary { background: #c5eeec; border-color: #dafaee; color: #17213b; }
.ui .primary:hover { background: #e3fff9; }
.ui .button:disabled { opacity: .6; cursor: default; }
.ui .field { min-width: 0; width: 100%; border: 1px solid #79779f; border-radius: 6px; background: #1b1a31; color: #faf5ff; padding: 5px 7px; font-size: 12px; }
.ui .field::placeholder { color: #c2bcdd; }
.ui input[type="range"] { accent-color: #b8ebe0; width: 100%; min-width: 0; margin: 0; }
.ui button { font: inherit; }
.ui [hidden] { display: none !important; }
.ui progress { accent-color: #b8ebe0; }
@media (prefers-reduced-motion: reduce) {
.ui *, .ui *::after, .ui *::before { transition-duration: 0s !important; animation-duration: 0s !important; scroll-behavior: auto !important; }
}
.interval{padding:10px 12px}.track{position:relative;height:39px;margin:0 8px;--low:28%;--high:72%}.rail{position:absolute;left:0;right:0;top:18px;height:5px;border-radius:4px;background:linear-gradient(to right,#5c5881 var(--low),#b6e9e3 var(--low) var(--high),#5c5881 var(--high))}.thumb{width:20px;height:24px;position:absolute;top:9px;left:var(--low);translate:-50% 0;border:1px solid #fff7;border-radius:6px;background:#b6e9e3;cursor:ew-resize;touch-action:none}.thumb.high{left:var(--high);background:#e1ceff}
</style>
</head>
<body>
<main class="demo">
<div class="ui"><div class="surface interval"><div class="bar"><strong>Frequency band</strong><span class="mono" role="status">28 – 72 Hz</span></div><div class="track"><div class="rail"></div><button class="thumb low" type="button" role="slider" aria-label="Minimum"></button><button class="thumb high" type="button" role="slider" aria-label="Maximum"></button></div><div class="bar muted"><span>0 Hz</span><span>100 Hz</span></div></div></div>
</main>
<script>
function dragOn(surface, handlers) {
let pointer = null;
let origin = null;
function finish(event, canceled) {
if (event.pointerId !== pointer) return;
const delta = { x: event.clientX - origin.x, y: event.clientY - origin.y };
pointer = null;
handlers.end(delta, canceled);
}
surface.addEventListener('pointerdown', (event) => {
if (pointer !== null || event.button !== 0) return;
pointer = event.pointerId;
origin = { x: event.clientX, y: event.clientY };
surface.setPointerCapture(pointer);
handlers.start?.(event);
});
surface.addEventListener('pointermove', (event) => {
if (event.pointerId === pointer) {
handlers.move({ x: event.clientX - origin.x, y: event.clientY - origin.y }, event);
}
});
surface.addEventListener('pointerup', (event) => finish(event, false));
surface.addEventListener('pointercancel', (event) => finish(event, true));
surface.addEventListener('lostpointercapture', (event) => finish(event, true));
}
const clamp = (value, low, high) => Math.min(high, Math.max(low, value));
const track = document.querySelector('.track');
const thumbs = [...track.querySelectorAll('.thumb')];
const values = [28, 72];
function set(index, value) {
values[index] = clamp(Math.round(value), index === 0 ? 0 : values[0] + 4, index === 1 ? 100 : values[1] - 4);
for (const [i, thumb] of thumbs.entries()) {
thumb.setAttribute('aria-valuemin', i === 0 ? 0 : values[0] + 4);
thumb.setAttribute('aria-valuemax', i === 1 ? 100 : values[1] - 4);
thumb.setAttribute('aria-valuenow', values[i]);
}
track.style.setProperty('--low', values[0] + '%'); track.style.setProperty('--high', values[1] + '%');
document.querySelector('[role=status]').textContent = values.join(' – ') + ' Hz';
}
for (const [index, thumb] of thumbs.entries()) {
let initial;
dragOn(thumb, { start() { initial = values[index]; }, move(delta) { set(index, initial + delta.x / track.clientWidth * 100); }, end(delta, canceled) { if (canceled) set(index, initial); } });
thumb.addEventListener('keydown', (event) => { const next = { ArrowLeft: values[index] - 1, ArrowDown: values[index] - 1, ArrowRight: values[index] + 1, ArrowUp: values[index] + 1, Home: 0, End: 100 }; if (Object.hasOwn(next, event.key)) { event.preventDefault(); set(index, next[event.key]); } });
}
set(0, values[0]);
</script>
</body>
</html>Original example by UI Skills