Created
May 17, 2019 09:19
-
-
Save adamfaux85/e99f535404e82bc113058bd8222a7d40 to your computer and use it in GitHub Desktop.
Vanilla JS: Fixed header on scroll
This file contains 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
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