Skip to content

Commit ad27643

Browse files
committed
fix: Fixed photoslider on mobile
1 parent 5b20845 commit ad27643

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

src/routes/PhotoSliderSection.svelte

+20-4
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,19 @@
2626
let animationFrame = 0;
2727
let lastFrameTime = Date.now();
2828
let translateSpeed = 0.9; // Adjust for slower or faster animation
29+
2930
let imageContainer: HTMLElement;
30-
$: imageContainerWidth = imageContainer?.clientWidth ?? imageModules.length * 2 * 500;
31-
$: maxScroll = -imageContainerWidth + (imageContainer?.parentElement?.clientWidth ?? 1000);
31+
let imageContainerWidth: number;
32+
let maxScroll: number;
33+
$: {
34+
recalculateSizes(imageContainer);
35+
}
36+
37+
function recalculateSizes(imageContainer: HTMLElement) {
38+
imageContainerWidth = imageContainer?.clientWidth ?? imageModules.length * 2 * 500;
39+
maxScroll = -imageContainerWidth + (imageContainer?.parentElement?.clientWidth ?? 1000);
40+
boundsCheck();
41+
}
3242
3343
function animate() {
3444
const now = Date.now();
@@ -64,6 +74,10 @@
6474
6575
const scrollWidth = imageContainer.children[0].clientWidth;
6676
translateX = direction === 'left' ? translateX + scrollWidth : translateX - scrollWidth;
77+
boundsCheck();
78+
}
79+
80+
function boundsCheck() {
6781
if (translateX > 0) {
6882
translateX = 0;
6983
} else if (translateX < maxScroll) {
@@ -82,23 +96,25 @@
8296
});
8397
</script>
8498

99+
<svelte:window on:resize={() => recalculateSizes(imageContainer)} />
85100
<div
86-
class="flex-center relative w-full flex-col bg-fountain px-5 py-32 text-black"
101+
class="flex-center relative w-full flex-col bg-fountain px-5 py-16 text-black"
87102
id="past-events"
88103
>
89104
<div class="text-4xl">Past Events</div>
90105
<div class="m-auto flex w-full overflow-hidden text-xl">
91106
<div class="flex min-w-[min-content] gap-2" bind:this={imageContainer}>
92107
{#each [...imageModules, ...imageModules] as module}
93108
<div
94-
class="picture-container flex aspect-[4/3] w-full items-center justify-center object-cover md:w-96"
109+
class="picture-container flex aspect-[4/3] w-[80vw] items-center justify-center object-cover md:w-96"
95110
class:pause={!isPlaying}
96111
style={`transform: translateX(${translateX}px);`}
97112
>
98113
<enhanced:img
99114
class="h-full w-full object-cover"
100115
src={module.default}
101116
alt="A past event"
117+
sizes="(min-width:1920px) 1280px, (min-width:1080px) 640px, (min-width:768px) 400px"
102118
/>
103119
</div>
104120
{/each}

0 commit comments

Comments
 (0)