Swipe Carousel
A horizontal carousel with native scroll snapping, drag with the mouse, arrows and dots.
AI prompt
Build a carousel 280x160px with four colour-block slides ("One" to "Four"), each exactly the width of the viewport. Use a flex row with overflow-x auto, scroll-snap-type x mandatory, scroll-snap-align start and hidden scrollbars. Add mouse drag: on pointerdown with pointerType mouse, disable snapping, follow the pointer by updating scrollLeft, and on release re-enable snapping and smooth-scroll to the nearest slide. Add Previous and Next buttons (disabled at the ends) and a row of four dots; the active dot (derived from scrollLeft on scroll) becomes a 18px wide pill in 200ms. Clicking a dot scrolls to its slide. The region has aria-roledescription carousel and each slide has aria-label "n of 4". Arrow keys scroll when the track is focused. Under prefers-reduced-motion use instant scrolling.About this pattern
Definition
A row of slides that scrolls horizontally and always settles with one slide aligned. Touch swipes natively, a mouse can drag, and buttons and dots provide other ways to move.
Purpose & UX principles
- Shows a set of equal items in limited space.
- CSS scroll snapping gives a native, smooth feel and keeps momentum; JavaScript only adds mouse dragging and indicators.
- Dots and arrow buttons are real buttons with labels, so keyboard users are not forced to swipe.
Interaction states
- Slide active (dot filled), dragging (cursor grabbing, snap suspended), arrows disabled at the ends, focus-visible.
Motion specification
- Native scroll-snap with
scroll-behavior: smoothfor button navigation. - Active dot widens in 200ms.
- Reduced motion: navigation jumps without smooth scrolling.
Responsive behaviour
- One slide per view on phones; slides keep a fixed ratio and fill the width.
- Arrow buttons hide on touch-only devices if space is tight.
Code
<div class="car" role="region" aria-roledescription="carousel" aria-label="Featured">
<div class="track" id="track" tabindex="0">
<div class="s s1" aria-label="1 of 4">One</div><div class="s s2" aria-label="2 of 4">Two</div><div class="s s3" aria-label="3 of 4">Three</div><div class="s s4" aria-label="4 of 4">Four</div>
</div>
<div class="ctl"><button id="prev" aria-label="Previous slide">‹</button><span class="dots" id="dots"></span><button id="next" aria-label="Next slide">›</button></div>
</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}
.car{width:min(280px,100%);display:grid;gap:10px}
.track{display:flex;overflow-x:auto;scroll-snap-type:x mandatory;scrollbar-width:none;border-radius:14px;border:1px solid var(--line);scroll-behavior:smooth;cursor:grab}
.track::-webkit-scrollbar{display:none}
.track.drag{scroll-snap-type:none;scroll-behavior:auto;cursor:grabbing;user-select:none}
.track:focus-visible{outline:2px solid var(--accent);outline-offset:2px}
.s{flex:none;width:100%;height:150px;scroll-snap-align:start;display:grid;place-items:center;font-size:22px;font-weight:600}
.s1{background:#e9e9e5}.s2{background:#dcdcd7}.s3{background:#cfcfca}.s4{background:#c2c2bd}
.ctl{display:flex;align-items:center;justify-content:space-between}
.ctl button{width:30px;height:30px;border-radius:50%;border:1px solid var(--line);background:var(--surface);font:inherit;font-size:16px;line-height:1;cursor:pointer}
.ctl button:disabled{opacity:.35;cursor:default}
.ctl button:focus-visible{outline:2px solid var(--accent);outline-offset:2px}
.dots{display:flex;gap:6px}
.dots button{width:6px;height:6px;padding:0;border-radius:3px;border:0;background:#c9c9c4;transition:width .2s,background .2s}
.dots button.on{width:18px;background:var(--accent)}
@media (prefers-reduced-motion:reduce){.track{scroll-behavior:auto}.dots button{transition:none}}const tr=document.getElementById('track'),dots=document.getElementById('dots'),prev=document.getElementById('prev'),next=document.getElementById('next');
const n=tr.children.length,w=()=>tr.clientWidth;
for(let i=0;i<n;i++){const b=document.createElement('button');b.setAttribute('aria-label','Go to slide '+(i+1));b.addEventListener('click',()=>tr.scrollTo({left:i*w()}));dots.append(b);}
const cur=()=>Math.round(tr.scrollLeft/w());
const sync=()=>{const c=cur();[...dots.children].forEach((d,i)=>d.classList.toggle('on',i===c));prev.disabled=c===0;next.disabled=c===n-1;};
tr.addEventListener('scroll',sync);sync();
prev.addEventListener('click',()=>tr.scrollTo({left:(cur()-1)*w()}));next.addEventListener('click',()=>tr.scrollTo({left:(cur()+1)*w()}));
tr.addEventListener('keydown',e=>{if(e.key==='ArrowRight'){e.preventDefault();next.click();}if(e.key==='ArrowLeft'){e.preventDefault();prev.click();}});
let down=false,sx=0,sl=0;
tr.addEventListener('pointerdown',e=>{if(e.pointerType!=='mouse')return;down=true;sx=e.clientX;sl=tr.scrollLeft;tr.classList.add('drag');tr.setPointerCapture(e.pointerId);});
tr.addEventListener('pointermove',e=>{if(down)tr.scrollLeft=sl-(e.clientX-sx);});
const end=()=>{if(!down)return;down=false;tr.classList.remove('drag');tr.scrollTo({left:cur()*w()});};
tr.addEventListener('pointerup',end);tr.addEventListener('pointercancel',end);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>Swipe Carousel — 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}}
.car{width:min(280px,100%);display:grid;gap:10px}
.track{display:flex;overflow-x:auto;scroll-snap-type:x mandatory;scrollbar-width:none;border-radius:14px;border:1px solid var(--line);scroll-behavior:smooth;cursor:grab}
.track::-webkit-scrollbar{display:none}
.track.drag{scroll-snap-type:none;scroll-behavior:auto;cursor:grabbing;user-select:none}
.track:focus-visible{outline:2px solid var(--accent);outline-offset:2px}
.s{flex:none;width:100%;height:150px;scroll-snap-align:start;display:grid;place-items:center;font-size:22px;font-weight:600}
.s1{background:#e9e9e5}.s2{background:#dcdcd7}.s3{background:#cfcfca}.s4{background:#c2c2bd}
.ctl{display:flex;align-items:center;justify-content:space-between}
.ctl button{width:30px;height:30px;border-radius:50%;border:1px solid var(--line);background:var(--surface);font:inherit;font-size:16px;line-height:1;cursor:pointer}
.ctl button:disabled{opacity:.35;cursor:default}
.ctl button:focus-visible{outline:2px solid var(--accent);outline-offset:2px}
.dots{display:flex;gap:6px}
.dots button{width:6px;height:6px;padding:0;border-radius:3px;border:0;background:#c9c9c4;transition:width .2s,background .2s}
.dots button.on{width:18px;background:var(--accent)}
@media (prefers-reduced-motion:reduce){.track{scroll-behavior:auto}.dots button{transition:none}}
</style></head>
<body><div class="stage">
<div class="car" role="region" aria-roledescription="carousel" aria-label="Featured">
<div class="track" id="track" tabindex="0">
<div class="s s1" aria-label="1 of 4">One</div><div class="s s2" aria-label="2 of 4">Two</div><div class="s s3" aria-label="3 of 4">Three</div><div class="s s4" aria-label="4 of 4">Four</div>
</div>
<div class="ctl"><button id="prev" aria-label="Previous slide">‹</button><span class="dots" id="dots"></span><button id="next" aria-label="Next slide">›</button></div>
</div>
</div>
<script>
const tr=document.getElementById('track'),dots=document.getElementById('dots'),prev=document.getElementById('prev'),next=document.getElementById('next');
const n=tr.children.length,w=()=>tr.clientWidth;
for(let i=0;i<n;i++){const b=document.createElement('button');b.setAttribute('aria-label','Go to slide '+(i+1));b.addEventListener('click',()=>tr.scrollTo({left:i*w()}));dots.append(b);}
const cur=()=>Math.round(tr.scrollLeft/w());
const sync=()=>{const c=cur();[...dots.children].forEach((d,i)=>d.classList.toggle('on',i===c));prev.disabled=c===0;next.disabled=c===n-1;};
tr.addEventListener('scroll',sync);sync();
prev.addEventListener('click',()=>tr.scrollTo({left:(cur()-1)*w()}));next.addEventListener('click',()=>tr.scrollTo({left:(cur()+1)*w()}));
tr.addEventListener('keydown',e=>{if(e.key==='ArrowRight'){e.preventDefault();next.click();}if(e.key==='ArrowLeft'){e.preventDefault();prev.click();}});
let down=false,sx=0,sl=0;
tr.addEventListener('pointerdown',e=>{if(e.pointerType!=='mouse')return;down=true;sx=e.clientX;sl=tr.scrollLeft;tr.classList.add('drag');tr.setPointerCapture(e.pointerId);});
tr.addEventListener('pointermove',e=>{if(down)tr.scrollLeft=sl-(e.clientX-sx);});
const end=()=>{if(!down)return;down=false;tr.classList.remove('drag');tr.scrollTo({left:cur()*w()});};
tr.addEventListener('pointerup',end);tr.addEventListener('pointercancel',end);
</script>
</body></html>