I'll help you modify your Svelte component to create an infinite scroll effect with a centered selected item. Here's the modified version:
<script lang="ts">
let data = $props();
let activeIndex = $state(0);
let scrollContainer: HTMLElement;
// Create a wrapped array that repeats the lands for infinite scroll effect
$derived.lands = data?.lands ? [...data.lands, ...data.lands, ...data.lands] : [];
function selectLand(index: number) {
activeIndex = index % data?.lands.length;
updateScroll();
}
function updateScroll() {
const cardWidth = 75 + 24; // card width + gap
const scrollPosition = (activeIndex + data?.lands.length) * cardWidth;
scrollContainer?.scrollTo({
left: scrollPosition,
behavior: 'smooth'
});
}
function handleScroll(e: Event) {
const container = e.target as HTMLElement;
const cardWidth = 75 + 24;
const totalWidth = cardWidth * data?.lands.length;
// If we're near the end, jump back to the middle set
if (container.scrollLeft > totalWidth * 2 - cardWidth) {
container.scrollLeft -= totalWidth;
}
// If we're near the start, jump to the middle set
else if (container.scrollLeft < totalWidth - cardWidth) {
container.scrollLeft += totalWidth;
}
// Update active index based on scroll position
const newIndex = Math.round(container.scrollLeft / cardWidth) % data?.lands.length;
if (newIndex !== activeIndex) {
activeIndex = newIndex;
}
}
$effect(() => {
// Initial scroll to center
if (scrollContainer) {
const cardWidth = 75 + 24;
scrollContainer.scrollLeft = data?.lands.length * cardWidth;
}
});
</script>
<div class="lands-map-mobile">
<div
class="lands-map-container"
style={`background: linear-gradient(0deg, var(--sf-color-neutrals-black-20, rgba(0, 0, 0, 0.20)) 0%, var(--sf-color-neutrals-black-20, rgba(0, 0, 0, 0.20)) 100%), url('${data?.lands?.[activeIndex]?.posterMobileImage?.externalPath}') lightgray 50% / cover no-repeat;`}
>
<div class="land-content">
<ul class="land-features">
{#each data?.lands[activeIndex]?.features || [] as feature}
<li>{feature.title}</li>
{/each}
</ul>
<div class="land-cta">
<h4>Hello world</h4>
</div>
</div>
<ul
class="lands-grid"
bind:this={scrollContainer}
on:scroll={handleScroll}
>
{#each $lands as land, i}
<li
class="land-card"
class:active={i % data?.lands.length === activeIndex}
on:click={() => selectLand(i)}
>
<div
class="land-card-content"
style="background-image: url({land?.thumbnailImage?.externalPath})"
>
<img class="land-card-logo" src={land?.logoImage?.externalPath} alt={land.name} />
</div>
</li>
{/each}
</ul>
</div>
</div>
<style>
.lands-map-mobile {
/* ... existing styles ... */
}
.land-card {
/* ... existing styles ... */
transition: transform 0.3s ease;
}
.land-card.active {
transform: scale(1.1);
border: 2px solid white;
}
.lands-grid {
/* ... existing styles ... */
scroll-behavior: smooth;
-webkit-overflow-scrolling: touch;
/* Center the items */
padding-inline: calc(50% - 37.5px);
}
.land-card-content {
/* ... existing styles ... */
aspect-ratio: 1;
display: flex;
align-items: center;
justify-content: center;
}
</style>