File tree Expand file tree Collapse file tree 1 file changed +12
-4
lines changed
apps/svelte.dev/src/routes/docs/[...path] Expand file tree Collapse file tree 1 file changed +12
-4
lines changed Original file line number Diff line number Diff line change 10
10
afterNavigate (() => {
11
11
current = location .hash .slice (1 );
12
12
headings = content .querySelectorAll (' h2' );
13
+ update (); // Ensure active link is set correctly on navigation
13
14
});
14
15
16
+ // Update function to activate the correct section link
15
17
function update() {
16
- // a section is 'active' when it crosses the threshold
17
18
const threshold = (innerHeight * 1 ) / 3 ;
19
+ let found = false ;
18
20
19
- for (let i = 0 ; i < headings .length ; i += 1 ) {
21
+ for (let i = 0 ; i < headings .length ; i ++ ) {
20
22
const heading = headings [i ];
21
23
const next = headings [i + 1 ];
22
24
25
+ // If the current heading is above the threshold and the next heading is below it
23
26
if (
24
- next &&
25
27
heading .getBoundingClientRect ().top < threshold &&
26
- next .getBoundingClientRect ().top > threshold
28
+ ( ! next || next .getBoundingClientRect ().top > threshold )
27
29
) {
28
30
current = heading .id ;
31
+ found = true ;
29
32
break ;
30
33
}
31
34
}
35
+
36
+ // Handle case when scrolled to the top of the page
37
+ if (! found && scrollY === 0 ) {
38
+ current = ' ' ;
39
+ }
32
40
}
33
41
</script >
34
42
You can’t perform that action at this time.
0 commit comments