Skip to content

Instantly share code, notes, and snippets.

@armchair-traveller
Last active April 9, 2022 05:05
Show Gist options
  • Save armchair-traveller/144fb638dd464f5b1bda1dc2ec1e42a9 to your computer and use it in GitHub Desktop.
Save armchair-traveller/144fb638dd464f5b1bda1dc2ec1e42a9 to your computer and use it in GitHub Desktop.
// 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