Last active
August 22, 2016 06:08
-
-
Save openopen114/b5e1769c51f6d9b179eb4776e8dbd789 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 class="div1"></div> | |
<div class="div2" id="Header"></div> | |
<div class="div3" id="someDiv"></div> | |
*/ | |
<script> | |
window.addEventListener('scroll', function(ev) { | |
var someDiv = document.getElementById('someDiv'); | |
var distanceToTop = someDiv.getBoundingClientRect().top; | |
if(distanceToTop < 50){ | |
document.getElementById("Header").style.position = "fixed"; | |
document.getElementById("Header").style.top = "0"; | |
console.log("fixed") | |
}else{ | |
document.getElementById("Header").style.position = "relative"; | |
console.log("relative") | |
} | |
console.log(distanceToTop); | |
}); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment