Rubber Band Drag Resistance
Drag a card past its limit and feel the resistance grow, then see it spring back when released.
AI prompt
Illustration sequence of a horizontal slider handle dragged past the right end of its track, three frames, the first with the handle at the end of the track, the second with the handle pulled beyond the end and the track stretching like a thin elastic band with a faint resistance curve above it, the third with the handle snapping back, soft blue palette on off-white, minimal flat vector style. Negative: gradients, shadows, text errors, clutter. Reduced motion: static sequence.About this pattern
Definition
Rubber banding gives drag a physical feel at the edge of a range. Inside the limits the element follows the finger one to one. Beyond them it moves less and less per pixel dragged, as if stretching a rubber band, and springs back when released. It tells people they have reached the end without a hard stop.
Purpose & UX principles
- Inside the range: position equals pointer movement.
- Beyond the range: apply damping, for example distance times 0.4 divided by one plus distance over a constant.
- On release, animate back to the limit with a spring or ease-out.
- Use pointer events and touch-action so the drag works on touch devices.
Interaction states
- A draggable handle on a track with a range of 0 to 100, damping presets and a reset.
Motion specification
- The spring-back takes about 400ms.
- Reduced motion: no spring; it snaps to the limit.
Responsive behaviour
- Keep the stretch small, around 30 to 60 pixels, so nothing looks broken.
- Offer button alternatives for people who cannot drag precisely.
Code
<div class="bx"><div class="pre" id="pre" role="group" aria-label="Resistance"><button data-p="0" aria-pressed="true">Soft</button><button data-p="1" aria-pressed="false">Medium</button><button data-p="2" aria-pressed="false">Stiff</button></div><div class="tk" id="tk"><i class="rg" id="rg"></i><b class="hd" id="hd" tabindex="0" role="slider" aria-valuemin="0" aria-valuemax="100" aria-valuenow="30" aria-label="Value"></b></div><p class="out" id="out"></p></div>:root{--bg:#F1F1EF;--surface:#fff;--surface-2:#F7F7F5;--line:#E0E0DC;--text:#171717;--muted:#6f6f6f;--accent:#171717;--accent-ink:#fff;--danger:#d92d20}
*{box-sizing:border-box;margin:0}
button{font:inherit;color:inherit;background:none;border:0;padding:0;cursor:pointer}a{color:inherit;text-decoration:none}ul,ol,p,h4{margin:0;padding:0}.sr-only{position:absolute;width:1px;height:1px;overflow:hidden;clip:rect(0 0 0 0);white-space:nowrap}
.pre{display:flex;gap:5px;flex-wrap:wrap}.pre button{height:28px;padding:0 11px;border-radius:99px;border:1px solid var(--line);background:var(--surface);font-size:11px;cursor:pointer}
.pre button[aria-pressed=true]{background:var(--accent);color:var(--accent-ink);border-color:var(--accent)}
.bx{width:min(330px,100%);display:grid;gap:9px}.rw{display:grid;gap:3px;font-size:11.5px;color:var(--muted)}.rw b{color:var(--text);font-weight:600}input[type=range]{width:100%;accent-color:#171717}
.out{font-size:12px;color:var(--muted);line-height:1.4}.out b{color:var(--text)}
button:focus-visible,input:focus-visible{outline:2px solid var(--accent);outline-offset:2px}
.tk{position:relative;height:84px;border-radius:12px;border:1px solid var(--line);background:var(--surface);margin:2px 0;touch-action:none}.tk::before{content:"";position:absolute;left:40px;right:40px;top:50%;height:4px;margin-top:-2px;background:#dcdcd8;border-radius:2px}.rg{position:absolute;left:40px;top:50%;height:4px;margin-top:-2px;background:#171717;border-radius:2px;width:0}.hd{position:absolute;top:50%;width:26px;height:26px;margin:-13px 0 0 -13px;border-radius:50%;background:#fff;border:2px solid #171717;box-shadow:0 2px 6px rgba(0,0,0,.2);cursor:grab;touch-action:none}.hd:active{cursor:grabbing}.out{font-size:12px;color:var(--muted)}
@media (prefers-reduced-motion:reduce){*{transition:none!important;animation:none!important}}const tk=document.getElementById('tk'),hd=document.getElementById('hd'),rg=document.getElementById('rg'),out=document.getElementById('out'),bs=[...document.querySelectorAll('#pre button')],calm=matchMedia('(prefers-reduced-motion:reduce)').matches,K=[90,45,22];let p=1,val=30,drag=false,off=0,raf=0;
function L(){return tk.clientWidth-80;}
function place(v){const px=v/100*L();hd.style.left=(40+px)+'px';rg.style.width=Math.max(0,Math.min(L(),px))+'px';}
function damp(d){const c=K[p];return Math.sign(d)*c*(1-Math.exp(-Math.abs(d)/c));}
function setFromPointer(x){const r=tk.getBoundingClientRect(),raw=(x-r.left-40)/L()*100;let v=raw;if(raw<0||raw>100){const d=(raw<0?raw:raw-100)/100*L(),s=damp(d)/L()*100;v=raw<0?s:100+s;out.textContent='Past the end: resistance grows the further you pull.';}else out.textContent='Inside the range: the handle follows your pointer.';place(v);if(raw>=0&&raw<=100){val=raw;hd.setAttribute('aria-valuenow',Math.round(val));}}
function release(){const target=Math.max(0,Math.min(100,parseFloat(hd.style.left)>0?(parseFloat(hd.style.left)-40)/L()*100:val));if(calm){place(target);val=target;return;}const from=(parseFloat(hd.style.left)-40)/L()*100,t0=performance.now();cancelAnimationFrame(raf);(function f(n){const k=Math.min(1,(n-t0)/400),e=1-Math.pow(1-k,3);place(from+(target-from)*e);if(k<1)raf=requestAnimationFrame(f);else{val=target;out.textContent='Released: it springs back to the limit.';}})(t0);}
hd.addEventListener('pointerdown',e=>{drag=true;hd.setPointerCapture(e.pointerId);cancelAnimationFrame(raf);});hd.addEventListener('pointermove',e=>{if(drag)setFromPointer(e.clientX);});['pointerup','pointercancel'].forEach(t=>hd.addEventListener(t,()=>{if(drag){drag=false;release();}}));
hd.addEventListener('keydown',e=>{if(e.key==='ArrowRight')val=Math.min(100,val+5);else if(e.key==='ArrowLeft')val=Math.max(0,val-5);else return;e.preventDefault();hd.setAttribute('aria-valuenow',Math.round(val));place(val);});
bs.forEach(b=>b.addEventListener('click',()=>{p=+b.dataset.p;bs.forEach(k=>k.setAttribute('aria-pressed',String(k===b)));}));bs.forEach(b=>b.setAttribute('aria-pressed',String(+b.dataset.p===p)));out.textContent='Drag the handle past either end.';place(val);addEventListener('resize',()=>place(val));One complete HTML file. Save it and open it in a browser.
<!doctype html>
<html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Rubber Band Drag Resistance — demo</title>
<style>
:root{--bg:#F1F1EF;--surface:#fff;--surface-2:#F7F7F5;--line:#E0E0DC;--text:#171717;--muted:#6f6f6f;--accent:#171717;--accent-ink:#fff;--danger:#d92d20}
*{box-sizing:border-box;margin:0}
html,body{height:100%}body{background:var(--bg);color:var(--text);font:15px/1.5 -apple-system,BlinkMacSystemFont,'Segoe UI',Inter,Roboto,sans-serif;-webkit-font-smoothing:antialiased}.stage{min-height:100%;display:grid;place-items:center;padding:24px}button,input,select,textarea{font-family:inherit}@media (prefers-reduced-motion:reduce){html{scroll-behavior:auto}}
button{font:inherit;color:inherit;background:none;border:0;padding:0;cursor:pointer}a{color:inherit;text-decoration:none}ul,ol,p,h4{margin:0;padding:0}.sr-only{position:absolute;width:1px;height:1px;overflow:hidden;clip:rect(0 0 0 0);white-space:nowrap}
.pre{display:flex;gap:5px;flex-wrap:wrap}.pre button{height:28px;padding:0 11px;border-radius:99px;border:1px solid var(--line);background:var(--surface);font-size:11px;cursor:pointer}
.pre button[aria-pressed=true]{background:var(--accent);color:var(--accent-ink);border-color:var(--accent)}
.bx{width:min(330px,100%);display:grid;gap:9px}.rw{display:grid;gap:3px;font-size:11.5px;color:var(--muted)}.rw b{color:var(--text);font-weight:600}input[type=range]{width:100%;accent-color:#171717}
.out{font-size:12px;color:var(--muted);line-height:1.4}.out b{color:var(--text)}
button:focus-visible,input:focus-visible{outline:2px solid var(--accent);outline-offset:2px}
.tk{position:relative;height:84px;border-radius:12px;border:1px solid var(--line);background:var(--surface);margin:2px 0;touch-action:none}.tk::before{content:"";position:absolute;left:40px;right:40px;top:50%;height:4px;margin-top:-2px;background:#dcdcd8;border-radius:2px}.rg{position:absolute;left:40px;top:50%;height:4px;margin-top:-2px;background:#171717;border-radius:2px;width:0}.hd{position:absolute;top:50%;width:26px;height:26px;margin:-13px 0 0 -13px;border-radius:50%;background:#fff;border:2px solid #171717;box-shadow:0 2px 6px rgba(0,0,0,.2);cursor:grab;touch-action:none}.hd:active{cursor:grabbing}.out{font-size:12px;color:var(--muted)}
@media (prefers-reduced-motion:reduce){*{transition:none!important;animation:none!important}}
</style></head>
<body><div class="stage">
<div class="bx"><div class="pre" id="pre" role="group" aria-label="Resistance"><button data-p="0" aria-pressed="true">Soft</button><button data-p="1" aria-pressed="false">Medium</button><button data-p="2" aria-pressed="false">Stiff</button></div><div class="tk" id="tk"><i class="rg" id="rg"></i><b class="hd" id="hd" tabindex="0" role="slider" aria-valuemin="0" aria-valuemax="100" aria-valuenow="30" aria-label="Value"></b></div><p class="out" id="out"></p></div>
</div>
<script>
const tk=document.getElementById('tk'),hd=document.getElementById('hd'),rg=document.getElementById('rg'),out=document.getElementById('out'),bs=[...document.querySelectorAll('#pre button')],calm=matchMedia('(prefers-reduced-motion:reduce)').matches,K=[90,45,22];let p=1,val=30,drag=false,off=0,raf=0;
function L(){return tk.clientWidth-80;}
function place(v){const px=v/100*L();hd.style.left=(40+px)+'px';rg.style.width=Math.max(0,Math.min(L(),px))+'px';}
function damp(d){const c=K[p];return Math.sign(d)*c*(1-Math.exp(-Math.abs(d)/c));}
function setFromPointer(x){const r=tk.getBoundingClientRect(),raw=(x-r.left-40)/L()*100;let v=raw;if(raw<0||raw>100){const d=(raw<0?raw:raw-100)/100*L(),s=damp(d)/L()*100;v=raw<0?s:100+s;out.textContent='Past the end: resistance grows the further you pull.';}else out.textContent='Inside the range: the handle follows your pointer.';place(v);if(raw>=0&&raw<=100){val=raw;hd.setAttribute('aria-valuenow',Math.round(val));}}
function release(){const target=Math.max(0,Math.min(100,parseFloat(hd.style.left)>0?(parseFloat(hd.style.left)-40)/L()*100:val));if(calm){place(target);val=target;return;}const from=(parseFloat(hd.style.left)-40)/L()*100,t0=performance.now();cancelAnimationFrame(raf);(function f(n){const k=Math.min(1,(n-t0)/400),e=1-Math.pow(1-k,3);place(from+(target-from)*e);if(k<1)raf=requestAnimationFrame(f);else{val=target;out.textContent='Released: it springs back to the limit.';}})(t0);}
hd.addEventListener('pointerdown',e=>{drag=true;hd.setPointerCapture(e.pointerId);cancelAnimationFrame(raf);});hd.addEventListener('pointermove',e=>{if(drag)setFromPointer(e.clientX);});['pointerup','pointercancel'].forEach(t=>hd.addEventListener(t,()=>{if(drag){drag=false;release();}}));
hd.addEventListener('keydown',e=>{if(e.key==='ArrowRight')val=Math.min(100,val+5);else if(e.key==='ArrowLeft')val=Math.max(0,val-5);else return;e.preventDefault();hd.setAttribute('aria-valuenow',Math.round(val));place(val);});
bs.forEach(b=>b.addEventListener('click',()=>{p=+b.dataset.p;bs.forEach(k=>k.setAttribute('aria-pressed',String(k===b)));}));bs.forEach(b=>b.setAttribute('aria-pressed',String(+b.dataset.p===p)));out.textContent='Drag the handle past either end.';place(val);addEventListener('resize',()=>place(val));
</script>
</body></html>