Skip to content

Commit 73be9e0

Browse files
author
Sebastian Flick
committed
add synchronic scrolling
1 parent daff911 commit 73be9e0

File tree

2 files changed

+28
-6
lines changed

2 files changed

+28
-6
lines changed

src/routes/fassungen/[thirties]/+page.svelte

+11-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<script>
22
import { base } from '$app/paths';
33
import FassungenContent from './FassungenContent.svelte';
4+
import { SlideToggle } from '@skeletonlabs/skeleton';
45
56
/** @type {{data: import('./$types').PageData}} */
67
let { data } = $props();
@@ -15,8 +16,6 @@
1516
thirties = [];
1617
1718
fetchPage = async (/** @type {string} */ page) => {
18-
// console.log('fetching', page);
19-
// console.log(Number(page), !this.thirties.length);
2019
if (!this.thirties.length) {
2120
let [d, m, G, T] = await fetch(`${base}/fassungen/data/${Number(page) - 1}`).then((r) =>
2221
r.json()
@@ -70,20 +69,29 @@
7069
$effect(() => {
7170
localPages.fetchPage(data.thirties);
7271
});
72+
let scrolltop = $state(0);
73+
let synchro = $state(true);
7374
</script>
7475

7576
<section class="w-full">
7677
<h1 class="h1 my-4">Fassungsansicht</h1>
7778
<div class="grid gap-6 md:grid-cols-2 md:my-8">
7879
<p>Einstellungen und Links zu den Textzeugen.</p>
80+
<SlideToggle active="bg-primary-500" name="synchro" bind:checked={synchro}>
81+
Synchrones scrollen
82+
</SlideToggle>
7983
</div>
8084
<div
8185
class="grid grid-cols-[repeat(auto-fit,minmax(400px,1fr))] gap-4 bg-surface-active-token my-4 py-4 px-8 rounded-xl"
8286
>
8387
{#each localPages.pages as pages}
8488
{#if pages.length >= 3}
8589
<!-- when at least 3 pages are loaded, the one for the currect thirties should be loaded aswell -->
86-
<FassungenContent {pages} />
90+
{#if synchro}
91+
<FassungenContent {pages} bind:scrolltop />
92+
{:else}
93+
<FassungenContent {pages} />
94+
{/if}
8795
{/if}
8896
{/each}
8997
</div>

src/routes/fassungen/[thirties]/FassungenContent.svelte

+17-3
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44
import { goto } from '$app/navigation';
55
import { base } from '$app/paths';
66
7-
/** @type {{pages: any}} */
8-
let { pages } = $props();
7+
let { pages, scrolltop = $bindable() } = $props();
98
109
let scrollContainer = $state();
1110
/**
@@ -56,9 +55,24 @@
5655
};
5756
});
5857
};
58+
59+
const setSyncedScroll = (/** @type {HTMLDivElement} */ node) => {
60+
$effect(() => {
61+
if (scrolltop) {
62+
node.scrollTo({ top: scrolltop, behavior: 'auto' });
63+
}
64+
});
65+
};
5966
</script>
6067
61-
<div class="max-h-[70vh] overflow-y-auto" bind:this={scrollContainer}>
68+
<div
69+
class="max-h-[70vh] overflow-y-auto"
70+
bind:this={scrollContainer}
71+
onscroll={(o) => {
72+
scrolltop = o?.target?.scrollTop;
73+
}}
74+
use:setSyncedScroll
75+
>
6276
{#each pages as page (page[0])}
6377
<div class="thirty" use:addToObserver>
6478
{@html page[1]}

0 commit comments

Comments
 (0)