Last active
April 9, 2022 05:05
-
-
Save armchair-traveller/144fb638dd464f5b1bda1dc2ec1e42a9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Courtesy of: https://www.smashingmagazine.com/2021/07/dynamic-header-intersection-observer/ | |
// * A little weird when scrolling up on high height dimensions, but more performant. A complete solution would require significantly more logic see: https://benfrain.com/building-a-table-of-contents-with-active-indicator-using-javascript-intersection-observers/ | |
let curContSect = ''; | |
onMount(() => { | |
const observer = new IntersectionObserver(([entry]) => | |
entry.isIntersecting && (curContSect = entry.target.querySelector('.section-heading').id), { threshold: 0 }) | |
for (let el of [...document.querySelectorAll('.section-heading')]) observer.observe(el.closest('section')) | |
return () => observer.disconnect(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment