Skip to content

Instantly share code, notes, and snippets.

@oliverll1
Created July 22, 2019 18:11
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 oliverll1/0943a8abccf52b9407e56960acfdf403 to your computer and use it in GitHub Desktop.
Save oliverll1/0943a8abccf52b9407e56960acfdf403 to your computer and use it in GitHub Desktop.
Change header background on scroll
function runOnScroll() {
const header = document.getElementById("header");
const bodyScroll = document.getElementsByTagName("body")[0].scrollTop;
// Chrome
const htmlScroll = document.getElementsByTagName("html")[0].scrollTop;
if ( bodyScroll >= 400 || htmlScroll >= 400) {
header.style.background = "#615e5d";
}
else{
header.style.background = "none";
}
};
window.addEventListener("scroll", runOnScroll);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment