Forked from ohiosveryown/change-class-on-scroll.html
Created
December 11, 2017 15:43
-
-
Save juanfernandes/f94aa47dfa45affbbf9ec1fd42446e99 to your computer and use it in GitHub Desktop.
Vanilla JS – change/add class based on scroll position.
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
<div id="header"></div> | |
<style> | |
#header { | |
position: fixed; | |
background: pink; | |
height: 72px; | |
width: 100%; | |
opacity: 0.2; | |
transition: all 300ms ease; | |
} | |
#header.fade-in { | |
opacity: 1; | |
} | |
</style> | |
<script> | |
var scrollpos = window.scrollY; | |
var header = document.getElementById("header"); | |
function add_class_on_scroll() { | |
header.classList.add("fade-in"); | |
} | |
function remove_class_on_scroll() { | |
header.classList.remove("fade-in"); | |
} | |
window.addEventListener('scroll', function(){ | |
scrollpos = window.scrollY; | |
if(scrollpos > 60){ | |
add_class_on_scroll(); | |
} | |
else { | |
remove_class_on_scroll(); | |
} | |
console.log(scrollpos); | |
}); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment