Skip to content

Instantly share code, notes, and snippets.

@sosroInSpace
Created March 2, 2018 06:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sosroInSpace/6195344701b5eea13e4dc0320aae3ae6 to your computer and use it in GitHub Desktop.
Save sosroInSpace/6195344701b5eea13e4dc0320aae3ae6 to your computer and use it in GitHub Desktop.
change div position as user scrolls
<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