Skip to content

Commit 341e97e

Browse files
authored
Buxfix for startIndex (#324)
* bugfix: to fix the start index in the pagination and simplefied the `#handleScrollStart()` so that it also works in Safari * Using requestAnimationFrame allows you to calculate the scroll position after the browser has finished processing all layout and style updates, ensuring that offsetLeft returns the correct value.
1 parent 2a247d6 commit 341e97e

File tree

2 files changed

+14
-22
lines changed

2 files changed

+14
-22
lines changed

carousel/carousel.css

+6-11
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ NICE TO HAVE:
1818
--_carousel-gutters: max(4rem, calc((100% - var(--_carousel-item-size)) / 2));
1919
--_carousel-scrollbar-gutter: var(--size-6);
2020
--_carousel-pagination-size: var(--size-8);
21-
21+
2222
display: grid;
2323
grid-template-columns: [carousel-gutter] var(--_carousel-gutters) 1fr [carousel-gutter] var(--_carousel-gutters);
24-
grid-template-rows:
25-
[carousel-scroller] 1fr
24+
grid-template-rows:
25+
[carousel-scroller] 1fr
2626
[carousel-pagination] var(--_carousel-pagination-size);
2727

2828
&:focus-visible {
@@ -78,13 +78,13 @@ NICE TO HAVE:
7878
:where(.gui-carousel--scroller) {
7979
grid-row: 1;
8080
grid-column: 1/-1;
81-
81+
8282
display: grid;
8383
grid-auto-columns: 100%;
8484
grid-auto-flow: column;
8585
align-items: center;
8686
gap: var(--_carousel-gutters);
87-
87+
8888
padding-block: var(--size-2) var(--_carousel-scrollbar-gutter);
8989
overflow-x: auto;
9090
overscroll-behavior-x: contain;
@@ -201,7 +201,7 @@ NICE TO HAVE:
201201
:where(.gui-carousel--pagination) {
202202
grid-column: 1/-1;
203203
place-self: center;
204-
204+
205205
display: grid;
206206
grid-auto-flow: column;
207207
gap: var(--size-2);
@@ -253,9 +253,4 @@ NICE TO HAVE:
253253
@keyframes gui-carousel--control-keypress {
254254
0% { outline-offset: 5px }
255255
50% { outline-offset: 0; }
256-
}
257-
258-
@keyframes carousel-scrollstart {
259-
from { scroll-snap-align: center }
260-
to { scroll-snap-align: unset }
261256
}

carousel/carousel.js

+8-11
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ export default class Carousel {
193193
// each snap needs some a11y love
194194
this.elements.snaps.forEach((snapChild, index) => {
195195
this.hasIntersected.add({
196-
isIntersecting: index === 0,
196+
isIntersecting: index === startIndex,
197197
target: snapChild,
198198
})
199199

@@ -210,17 +210,14 @@ export default class Carousel {
210210
const itemIndex = this.elements.root.getAttribute('carousel-start')
211211
const startElement = this.elements.snaps[itemIndex - 1]
212212

213-
this.elements.snaps.forEach(snap =>
214-
snap.style.scrollSnapAlign = 'unset')
213+
requestAnimationFrame(() => {
214+
const scrollPos = startElement.offsetLeft
215215

216-
startElement.style.scrollSnapAlign = null
217-
startElement.style.animation = 'carousel-scrollstart 1ms'
218-
219-
startElement.addEventListener('animationend', e => {
220-
startElement.style.animation = null
221-
this.elements.snaps.forEach(snap =>
222-
snap.style.scrollSnapAlign = null)
223-
}, {once: true})
216+
this.elements.scroller.scrollTo({
217+
left: scrollPos,
218+
behavior: 'instant',
219+
})
220+
})
224221
}
225222
}
226223

0 commit comments

Comments
 (0)