Skip to content

Instantly share code, notes, and snippets.

@adamfaux85
Created May 17, 2019 09:19
Show Gist options
  • Save adamfaux85/e99f535404e82bc113058bd8222a7d40 to your computer and use it in GitHub Desktop.
Save adamfaux85/e99f535404e82bc113058bd8222a7d40 to your computer and use it in GitHub Desktop.
Vanilla JS: Fixed header on scroll
let scrollpos = window.scrollY
const headerElem = document.querySelector(".dress-details")
const headerElemHeight = headerElem.offsetHeight
const add_class_on_scroll = () => headerElem.classList.add("fixed")
const remove_class_on_scroll = () => headerElem.classList.remove("fixed")
window.addEventListener('scroll', function() {
scrollpos = window.scrollY;
if (scrollpos >= headerElemHeight) { add_class_on_scroll() }
else { remove_class_on_scroll() }
console.log(scrollpos)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment