· 2 min read
A scroll-spy that can't fall out of step
Rewrote the section nav's scroll-spy (SectionNav.svelte). It used an IntersectionObserver on a 1px line a third of the way down the screen. Now every scroll…
What we did
- Rewrote the section nav's scroll-spy (
SectionNav.svelte). It used an IntersectionObserver on a 1px line a third of the way down the screen. Now every scroll works out the current section from where the sections are. - The landing and
/aboutboth use it.
What I learned
- An IntersectionObserver only reports changes: a section starting or stopping to touch the line. A fast scroll could stop with the line in the gap between two sections; nothing touched it, no event came, and the old section stayed highlighted.
- Worked out from positions instead: the current section is the last one whose top (
getBoundingClientRect().top) is above the line. Gaps don't matter. - Two edges: before the first section reaches the line, it's already current (on
/aboutthe logo strip pushed it below the line, and nothing was highlighted at the top); at the very bottom, the last one is current, because a short last section never reaches the line. $effect(update)runs it once the page is in the DOM, so a reload halfway down is right too. Then<svelte:window onscroll onresize>.- Measuring ten sections per scroll is cheap, and browsers fire
scrollat most once per frame. An observer only works when something changes, but this can't miss a change.
Problems & fixes
- Testing with
scrollTo()read the old section: the site has smooth scrolling, so the page was still moving. Tests setscroll-behavior: autofirst.
Links
Next
- Nothing here. Back to
/about's content.