Skip to content

Instantly share code, notes, and snippets.

@amcgregor
Created March 28, 2022 05:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amcgregor/5b479dbf167a244454780434a12885be to your computer and use it in GitHub Desktop.
Save amcgregor/5b479dbf167a244454780434a12885be to your computer and use it in GitHub Desktop.
A relatively light-weight "scroll spy" to pivot scrolling events from edge → level.
{ // Detect scrolling of the viewport away from the absolute top of the document.
function scrollHandler(e) {
if ( window.scrollY == 0 && document.body.scrollTop == 0 ) document.body.classList.remove('offset')
else document.body.classList.add('offset')
}
// Watch for the page being scrolled.
window.addEventListener('scroll', scrollHandler)
document.body.addEventListener('scroll', scrollHandler)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment