Layout · 13 of 23
Carousel Snap
Horizontal snap scrolling
<div class="carousel-snap">
<div class="c-item">1</div>
<div class="c-item">2</div>
<div class="c-item">3</div>
<div class="c-item">4</div>
</div>
.carousel-snap {
display: flex;
gap: 1rem;
overflow-x: auto;
width: 100%;
max-width: 400px;
padding-bottom: 1rem;
scroll-snap-type: x mandatory; /* native CSS snapping */
cursor: grab;
user-select: none;
/* Windows/Chrome renders a white native scrollbar here otherwise */
scrollbar-width: thin;
scrollbar-color: #7c5cff rgba(255, 255, 255, 0.08);
}
.carousel-snap::-webkit-scrollbar {
height: 6px;
}
.carousel-snap::-webkit-scrollbar-track {
background: rgba(255, 255, 255, 0.08);
border-radius: 3px;
}
.carousel-snap::-webkit-scrollbar-thumb {
background: #7c5cff;
border-radius: 3px;
}
.carousel-snap::-webkit-scrollbar-thumb:hover {
background: #a78bfa;
}
.carousel-snap::-webkit-scrollbar-button {
display: none;
width: 0;
height: 0;
}
.carousel-snap.dragging {
cursor: grabbing;
scroll-behavior: auto;
}
.c-item {
min-width: 80%;
height: 120px;
background: #333;
border-radius: 8px;
display: flex;
align-items: center;
justify-content: center;
font-size: 2rem;
color: #555;
scroll-snap-align: center;
}
// The CSS scroll-snap works on its own with touch / trackpad.
// This JS adds mouse-wheel paging and click-drag scrolling.
var carousel = document.querySelector('.carousel-snap');
var items = carousel.querySelectorAll('.c-item');
var carouselIndex = 0;
function snapTo(index) {
carouselIndex = Math.max(0, Math.min(items.length - 1, index));
var item = items[carouselIndex];
var left = item.offsetLeft - (carousel.clientWidth - item.offsetWidth) / 2;
carousel.scrollTo({ left: left, behavior: 'smooth' });
}
// Vertical wheel scrolls advance one card at a time
carousel.addEventListener('wheel', function (e) {
e.preventDefault();
snapTo(carouselIndex + ((e.deltaY || e.deltaX) > 0 ? 1 : -1));
}, { passive: false });
// Drag to scroll (mouse drag does nothing on overflow containers by default)
var dragStartX = 0;
var dragStartScroll = 0;
var isDragging = false;
carousel.addEventListener('pointerdown', function (e) {
isDragging = true;
dragStartX = e.clientX;
dragStartScroll = carousel.scrollLeft;
carousel.setPointerCapture(e.pointerId);
carousel.style.scrollSnapType = 'none'; // let the drag move freely
carousel.style.scrollBehavior = 'auto';
carousel.classList.add('dragging');
});
carousel.addEventListener('pointermove', function (e) {
if (!isDragging) return;
carousel.scrollLeft = dragStartScroll - (e.clientX - dragStartX);
});
function endDrag() {
if (!isDragging) return;
isDragging = false;
carousel.classList.remove('dragging');
carousel.style.scrollBehavior = '';
// Snap to whichever item is closest to the center
var center = carousel.scrollLeft + carousel.clientWidth / 2;
var best = 0;
var bestDist = Infinity;
items.forEach(function (item, i) {
var d = Math.abs(item.offsetLeft + item.offsetWidth / 2 - center);
if (d < bestDist) { bestDist = d; best = i; }
});
carousel.style.scrollSnapType = 'none'; // keep off so smooth scrollTo isn't hijacked
snapTo(best);
}
carousel.addEventListener('pointerup', endDrag);
carousel.addEventListener('pointercancel', endDrag);