Horizontal snap carousel

CSS scroll-snap is excellent until you need to know which slide you landed on, or want easing other than the browser default. scrollSnap keeps the drag feel but adds a threshold you control, custom easing, and an onSnap callback — which is what the dot indicators in this example are wired to, rather than polling scroll position.

24scrollSnap · horizontal · onSnap

The code

A boutique holiday-rentals site for country houses in Portugal and Spain. A horizontal scrollSnap carousel holds one house at a time, and onSnap moves the counter, dots and details panel to the house that landed while its painting inks in.

Hero.tsx18 lines
import { scrollSnap } from 'svg-scroll-draw/snap';

const track = document.querySelector('.houses-track');
const slides = track.querySelectorAll('.house');

const snap = scrollSnap(slides, {
  direction: 'horizontal',
  scrollContainer: track,
  duration: 700,
  easing: 'ease-in-out',
  threshold: 0.18,
  onSnap: (i) => {
    counter.textContent = `${i + 1} / ${slides.length}`;
    showHouse(HOUSES[i]); // name, region, sleeps, price per night
  },
});

nextButton.onclick = () => snap.snapTo(snap.getCurrentIndex() + 1);
Casa Verde, holiday rentals in portugal and spain: the first screen of its demo site

Holiday rentals in Portugal and Spain

Casa Verde

Live site

Related examples

Make your own in the CSS scroll snap generator: set it up, watch it run, copy the code.

MIT licensed, zero runtime dependencies. Every API is a separate entry point, so you only ship what you import. Full API reference →