Skip to content

Instantly share code, notes, and snippets.

@jasondmoss
Last active November 22, 2019 03:17
Show Gist options
  • Save jasondmoss/90186d82701e8344b06e4f168e7d8912 to your computer and use it in GitHub Desktop.
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.
/**
* 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