Created
March 2, 2018 06:18
-
-
Save sosroInSpace/6195344701b5eea13e4dc0320aae3ae6 to your computer and use it in GitHub Desktop.
change div position as user scrolls
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
<style> | |
#altContainer { | |
position:absolute; | |
min-height:500px; | |
width:400px | |
} | |
#alternateLinks { | |
position:absolute; | |
height:200px; | |
width:100%; | |
} | |
<style> | |
<div id="altContainer"> | |
<div id="alternateLinks"> | |
</div> | |
</div> | |
<script> | |
var container = document.getElementById("altContainer"); | |
var scroller = document.getElementById("alternateLinks"); | |
// Scroll function for repositioning 'scrollerr'. | |
function scroll() { | |
var cY = container.getBoundingClientRect().top; | |
var cH = container.getBoundingClientRect().height; | |
var sH = scroller.getBoundingClientRect().height; | |
var top = 300; | |
scroller.style.top = Math.min(Math.max(-cY + top, 0), cH - sH) + 'px'; | |
}; | |
// Init scrolling. | |
window.addEventListener("scroll", scroll); | |
scroll(); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment