To create an infinite scroll with a centered selected item, you'll need to implement a carousel pattern with cloned items and scroll position management. Here's how to modify your component:
<script lang="ts">
import { onMount, tick } from 'svelte';
let data = $props();
let activeIndex = $state(0);
let scrollContainer: HTMLElement;
let isScrolling = false;
let itemWidth = 0;
let containerWidth = 0;
// Create extended array with clones for infinite scroll
$: lands = data?.lands || [];
$: extendedLands = lands.length > 0 ? [...lands, ...lands, ...lands] : [];
$: centerIndex = lands.length; // Start at the middle set
function selectLand(index: number) {
activeIndex = index;
scrollToIndex(centerIndex + index);
}
async function scrollToIndex(index: number) {
if (!scrollContainer || isScrolling) return;
isScrolling = true;
const targetScroll = index * (itemWidth + 24); // 24px is the gap
scrollContainer.scrollTo({
left: targetScroll,
behavior: 'smooth'
});
// Wait for scroll to complete
setTimeout(() => {
isScrolling = false;
}, 300);
}
function handleScroll() {
if (!scrollContainer || isScrolling || lands.length === 0) return;
const scrollLeft = scrollContainer.scrollLeft;
const currentIndex = Math.round(scrollLeft / (itemWidth + 24));
// Update active index based on current position
const newActiveIndex = currentIndex % lands.length;
if (newActiveIndex !== activeIndex) {
activeIndex = newActiveIndex;
}
// Handle infinite scroll wrapping
const totalItems = extendedLands.length;
const firstSetEnd = lands.length * (itemWidth + 24);
const lastSetStart = (lands.length * 2) * (itemWidth + 24);
if (scrollLeft <= 0) {
// Scrolled to beginning, jump to middle set
scrollContainer.scrollLeft = lastSetStart;
} else if (scrollLeft >= (totalItems - 1) * (itemWidth + 24)) {
// Scrolled to end, jump to middle set
scrollContainer.scrollLeft = firstSetEnd;
}
}
function updateDimensions() {
if (!scrollContainer) return;
const firstItem = scrollContainer.querySelector('.land-card');
if (firstItem) {
itemWidth = firstItem.getBoundingClientRect().width;
containerWidth = scrollContainer.getBoundingClientRect().width;
}
}
onMount(async () => {
await tick();
updateDimensions();
// Initial scroll to center the first item
if (scrollContainer && lands.length > 0) {
const initialScroll = centerIndex * (itemWidth + 24);
scrollContainer.scrollLeft = initialScroll;
}
// Add resize listener
const resizeObserver = new ResizeObserver(updateDimensions);
if (scrollContainer) {
resizeObserver.observe(scrollContainer);
}
return () => {
resizeObserver.disconnect();
};
});
// Update main content when activeIndex changes
$: selectedLand = lands[activeIndex];
$effect(() => console.log(data));
</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('${selectedLand?.posterMobileImage?.externalPath}') lightgray 50% / cover no-repeat;`}
>
<div class="land-content">
<ul class="land-features">
{#each selectedLand?.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 extendedLands as land, index}
<li
class="land-card"
class:active={index % lands.length === activeIndex}
on:click={() => selectLand(index % lands.length)}
>
<div
class="land-card-content"
style="background-image: url({land?.thumbnailImage?.externalPath}); border: 1px solid #fff;"
>
<img class="land-card-logo" src={land?.logoImage?.externalPath} alt={land.name} />
</div>
</li>
{/each}
</ul>
</div>
</div>
<style>
.lands-map-mobile {
width: 100%;
height: 100vh;
overflow: hidden;
background-color: var(--sf-color-primary-base);
padding: 24px;
position: relative;
& .lands-map-container {
--bottom-spacing: 26px;
height: 100%;
width: 100%;
border-radius: var(--sf-spacing-16px);
display: flex;
flex-direction: column;
justify-content: end;
background-position: center;
background-size: cover;
padding-bottom: var(--bottom-spacing);
& .land-content {
& .land-features {
display: flex;
flex-wrap: wrap;
align-items: center;
color: white;
gap: var(--sf-spacing-12px);
justify-content: center;
padding-bottom: var(--sf-spacing-24px);
}
& .land-cta {
color: white;
text-align: center;
padding-bottom: calc(var(--bottom-spacing) + var(--sf-spacing-32px));
}
}
& .lands-grid {
position: absolute;
left: 0;
right: 0;
bottom: var(--bottom-spacing);
width: 100%;
display: flex;
flex-direction: row;
gap: var(--sf-spacing-24px);
overflow-x: scroll;
scrollbar-width: none;
-ms-overflow-style: none;
scroll-snap-type: x mandatory;
padding-inline: var(--sf-spacing-24px);
&::-webkit-scrollbar {
display: none;
}
& .land-card {
min-width: 75px;
min-height: 40px;
flex-shrink: 0;
scroll-snap-align: center;
cursor: pointer;
transition: transform 0.2s ease, opacity 0.2s ease;
&.active {
transform: scale(1.1);
opacity: 1;
}
&:not(.active) {
opacity: 0.7;
}
& .land-card-content {
border-radius: var(--sf-border-radius-xs);
background-position: center;
background-size: cover;
width: 100%;
height: 100%;
display: flex;
align-items: center;
justify-content: center;
& .land-card-logo {
font-size: 12px;
max-width: 80%;
max-height: 80%;
object-fit: contain;
}
}
}
}
}
}
</style>