Skip to content

Commit b527541

Browse files
committed
fix: correct scroll position if needed
For some reason that escapes me Chrome ignores the `scroll-margin-top` property. It's possible to fix it using JS even thought that's not ideal. fixes #273
1 parent 0952c73 commit b527541

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

apps/svelte.dev/src/routes/docs/[...path]/OnThisPage.svelte

+24-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,30 @@
3232
}
3333
</script>
3434

35-
<svelte:window onscroll={update} onhashchange={() => (current = location.hash.slice(1))} />
35+
<svelte:window
36+
onscroll={update}
37+
onhashchange={() => {
38+
current = location.hash.slice(1);
39+
// Correct scroll position if needed
40+
const active = [...headings.values()].find((entry) => entry.id === current);
41+
if (active) {
42+
const { top, bottom } = active.getBoundingClientRect();
43+
const min = 100;
44+
const max = window.innerHeight - 100;
45+
if (top > max) {
46+
window.scrollBy({
47+
top: top - max,
48+
left: 0
49+
});
50+
} else if (bottom < min) {
51+
window.scrollBy({
52+
top: bottom - min,
53+
left: 0
54+
});
55+
}
56+
}
57+
}}
58+
/>
3659

3760
<aside class="on-this-page">
3861
<label>

0 commit comments

Comments
 (0)