Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 2 additions & 17 deletions src/lib/components/Carousel.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,16 @@
}

const { size = 'default', gap = 32, header, children }: Props = $props();
let scroll = 0;

function calculateScrollAmount(prev = false) {
const direction = prev ? -1 : 1;
const carouselSize = carousel?.clientWidth;
const childSize = (carousel.childNodes[0] as HTMLUListElement)?.clientWidth + gap;

scroll = scroll || carouselSize;

const numberOfItems = Math.floor(carouselSize / childSize);
const overflow = scroll % childSize;
const amount = numberOfItems * childSize - overflow * direction;
scroll += amount * direction;
return amount * direction;
}

function next() {
carousel.scrollBy({
left: calculateScrollAmount(),
left: carousel.clientWidth,
behavior: 'smooth'
});
}
function prev() {
carousel.scrollBy({
left: calculateScrollAmount(true),
left: -carousel.clientWidth,
behavior: 'smooth'
});
}
Expand Down