Last active
November 22, 2019 03:17
-
-
Save jasondmoss/90186d82701e8344b06e4f168e7d8912 to your computer and use it in GitHub Desktop.
Add a class to site header when the page scrolls below marker. Remove the class when the page scrolls above marker.
This file contains hidden or 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
/** | |
* Add a class to site header when the page scrolls below marker. Remove the | |
* class when the page scrolls above marker. | |
*/ | |
// jshint esversion: 6 | |
window.addEventListener("scroll", event => { | |
"use strict"; | |
event.preventDefault(); | |
let distanceY = window.pageYOffset || document.documentElement.scrollTop; | |
let siteHeader = document.querySelector("#MyHeaderElement"); | |
let marker = 300; | |
if (distanceY > marker) { | |
siteHeader.classList.add("smaller"); | |
} else if (siteHeader.classList.contains("smaller")) { | |
siteHeader.classList.remove("smaller"); | |
} | |
}); | |
/* <> */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment