Sliders that fight back
What a slider should do when you drag past its limit.
The hard stop
A slider is a promise: move your hand and the value follows. That promise breaks at both ends. Your hand keeps moving, the thumb stops dead, and nothing tells you why.
value: 70Drag it past the end and watch. It works perfectly, and it feels like hitting a wall. This article builds a slider that answers at the limit, one layer at a time, and the value stays exactly as accurate as the native one.
Keep hold of the drag
Before adding any feel, the drag has to be reliable. Pointer events are sent to whatever is under the pointer, so a fast hand that slips off a thin track simply loses the slider.
Drags lost by the left slider: 0setPointerCapture tells the browser to send every later pointer event to this element until the press ends. The drag now survives leaving the track, the window, and even the browser itself.
</> Three lines make the drag reliable.Show code
slider.addEventListener('pointerdown', event => {
slider.setPointerCapture(event.pointerId);
moveTo(event.clientX);
});
slider.addEventListener('pointermove', event => {
if (slider.hasPointerCapture(event.pointerId)) moveTo(event.clientX);
});Give the limit some resistance
Instead of stopping, the thumb can keep following your hand while moving less and less. The further you pull, the harder it pushes back. Your hand now feels the edge.
Drag the thumb past either end.The dashed line is what the thumb would do with no resistance. The curve sits below it from the first pixel, because the thumb only covers part of each move. It then flattens towards a ceiling that the thumb can never pass.
</> The rubber band is one small function.Show code
// over: how far the pointer is past the end, in pixels // k: how quickly resistance builds // room: the most the thumb is ever allowed to travel const rubber = (over, k, room) => (1 - 1 / (over * k / room + 1)) * room;
Stretch, do not escape
A thumb that slides past the end of its track looks like it has broken free. The limit is still there, so the track should show it. When the track grows with the pull and the thumb flattens against it, the whole control is under tension.
Both ends of the thumb stay perfectly round because we change its real width and height. Scaling it would distort the curve. The change is tiny, and it only happens while you are past the limit.
Let go like a spring
Releasing a stretched slider is the most satisfying moment in the whole gesture. If the thumb teleports home, all that tension vanishes. A spring lets it travel back, carrying the speed of your hand.
One level deeper. CSS transitions always start from zero speed, so they cannot continue a moving hand. A few lines of script can: measure the pointer speed at release, then step the spring forward on each frame until it settles.
</> A spring needs only position, velocity and a target.Show code
function step(dt) {
const force = -stiffness * (x - target) - damping * velocity;
velocity += force * dt;
x += velocity * dt;
}Feel every step
Some sliders choose between a few fixed levels. The thumb should still follow your hand, but lean towards the nearest level and jump when it gets closer to the next one. Each level becomes a small detent you can almost feel.
Level: MediumThe value only changes at each level, so the display never flickers between numbers. The tick lights up at the exact moment the value changes, which confirms the change without any extra text.
Still a real slider
All of this motion is decoration. Underneath, it has to remain a slider for everyone. Focus the one below and use the arrow keys, Page Up, Page Down, Home and End.
role="slider" aria-valuenow="40" aria-valuetext="40 percent"The value is always clamped between the minimum and maximum, however far the thumb stretches. Screen readers hear the real number, and people who ask for reduced motion get the same slider with no spring.
Use a native slider when you can. <input type="range"> gives you dragging, keyboard control and screen reader support for free. Build a custom one only when the feel is worth the extra responsibility.