From 0ab54c38de9a3729cb7d0269575a3c63e32a3071 Mon Sep 17 00:00:00 2001 From: Daniel Stock Date: Fri, 2 Oct 2026 21:55:41 +0200 Subject: [PATCH] Change naming for scroll-direction and fixed a bug where all control would reference the first carousel on the page. --- index.html | 23 ++++++++++++++++++++--- src/snappy.css | 2 +- src/snappy.js | 10 +++++----- 3 files changed, 26 insertions(+), 9 deletions(-) diff --git a/index.html b/index.html index b9d8538..df08c9b 100644 --- a/index.html +++ b/index.html @@ -34,7 +34,7 @@ /* Colors from Open color (https://yeun.github.io/open-color) */ .snappy-carousel { .scroll-container { - max-height: 300px; + max-height: 400px; gap: 0.5rem; } @@ -140,8 +140,8 @@ See https://developer.mozilla.org/en-US/docs/Web/CSS/Guides/Scroll_snap - data-direction="x" - data-direction="y" + data-scroll-direction="x" + data-scroll-direction="y" data-snap-stop="always" data-snap-stop="normal" @@ -167,6 +167,23 @@ + + diff --git a/src/snappy.css b/src/snappy.css index 42e9726..bdef2fd 100644 --- a/src/snappy.css +++ b/src/snappy.css @@ -32,7 +32,7 @@ /* Options */ - &[data-direction="y"] .scroll-container { + &[data-scroll-direction="y"] .scroll-container { flex-direction: column; overflow-x: hidden; overflow-y: auto; diff --git a/src/snappy.js b/src/snappy.js index 8a8e3ed..1bdaebd 100644 --- a/src/snappy.js +++ b/src/snappy.js @@ -28,7 +28,7 @@ class Carousel { paginationDots = []; // Behavioral constants - inertia = 0.90; // Decay rate (0.9 = slower stop, 0.99 = longer slide) + inertia = 0.92; // Decay rate (0.9 = slower stop, 0.99 = longer slide) velocityFrameCount = 5; // State variables @@ -54,12 +54,12 @@ class Carousel { */ constructor(element) { this.scrollContainer = element.querySelector('.scroll-container'); - this.direction = element.dataset.direction === 'y' ? 'y' : 'x'; + this.direction = element.dataset.scrollDirection === 'y' ? 'y' : 'x'; this.slides = this.scrollContainer.querySelectorAll('.slide'); - this.prevButton = document.querySelector(`.prev`); - this.nextButton = document.querySelector(`.next`); - this.pagination = document.querySelector(`.pagination`); + this.prevButton = element.querySelector(`.prev`); + this.nextButton = element.querySelector(`.next`); + this.pagination = element.querySelector(`.pagination`); this.scrollContainer.addEventListener('mousedown', this.startDrag.bind(this)); this.scrollContainer.addEventListener('mousemove', this.drag.bind(this));