Skip to content

Commit dab89c9

Browse files
committed
fix TOC scrolling
1 parent e5c6f2a commit dab89c9

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

src/scripts/toc.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,21 @@ document.addEventListener("astro:page-load", () => {
1212
function doToc(toc: HTMLElement) {
1313
const titles = new Map();
1414

15-
Array.from(toc.children).forEach(node => {
15+
// ugly hack :^)
16+
// reverse children, so priority is for the last visible TOC element
17+
// but push first element so it takes priority over the 2nd element
18+
const toc_children = Array.from(toc.children);
19+
// @ts-expect-error The TOC will always have >1 elements
20+
toc_children.push(toc_children[0])
21+
toc_children.reverse();
22+
23+
toc_children.forEach(node => {
1624
if (!(node instanceof HTMLElement)) {
1725
return;
1826
}
1927

2028
const slug = node.dataset.slug;
21-
if (slug === undefined) {return;}
22-
29+
if (slug === undefined) { return; }
2330

2431
titles.set(slug, false);
2532
})
@@ -63,22 +70,25 @@ function doToc(toc: HTMLElement) {
6370
}
6471

6572
const slug = entry.target.dataset.headingId;
73+
// console.log(slug, entry.isIntersecting, entry.intersectionRatio);
74+
6675
if (slug === undefined) {
6776
return;
6877
}
6978

7079
titles.set(slug, entry.isIntersecting);
80+
// console.log(titles);
7181

7282

7383
updateTitles();
7484
})
7585
}, {
76-
threshold: 0.9,
86+
threshold: [0, 0.25, 0.5, 0.75, 1],
7787
});
7888

7989
titles.forEach((_, slug) => {
8090
const section = document.querySelectorAll(`[data-heading-id="${slug}"]`)[0];
81-
if (section === undefined) {return;}
91+
if (section === undefined) { return; }
8292

8393
observer.observe(section);
8494
});

0 commit comments

Comments
 (0)