0

I'm trying to get a div to fix to the top when scrolling down and then once the user scrolls down to the next div that will replace the 1st div etc.

I seem to be getting an issue when scrolling to the second div. It seems to be expanding to the end of the page even when I have the CSS set to a height of 600px

I have it running in this Codepen here:

http://codepen.io/anon/pen/QbMBMY

HTML:

<p>
    I didn’t invent this, the original code is from an answer1 on Stack Overflow, and that seems to be from Stack Overflow’s code, but with a few of my own modifications. The code requires jQuery. The #sticky-anchor is for providing an offset reference, so we can compare with and know if visitor scrolls down far enough. If so, the #sticky would be applied with an CSS class, which makes it sticky to the page; if not, then remove the CSS class, so it can revert to what it should be when the page just loaded. This way make the code more cleaner. z-index is better to be applied since other elements may have that, make sure the number is high enough so #sticky would be on top of others.
</p>
<div id="sticky-anchor"></div>
<div id="sticky">red</div>

<p>
    I didn’t invent this, the original code is from an answer1 on Stack Overflow, and that seems to be from Stack Overflow’s code, but with a few of my own modifications. The code requires jQuery. The #sticky-anchor is for providing an offset reference, so we can compare with and know if visitor scrolls down far enough. If so, the #sticky would be applied with an CSS class, which makes it sticky to the page; if not, then remove the CSS class, so it can revert to what it should be when the page just loaded. This way make the code more cleaner. z-index is better to be applied since other elements may have that, make sure the number is high enough so #sticky would be on top of others.
</p>

<p>
    I didn’t invent this, the original code is from an answer1 on Stack Overflow, and that seems to be from Stack Overflow’s code, but with a few of my own modifications. The code requires jQuery. The #sticky-anchor is for providing an offset reference, so we can compare with and know if visitor scrolls down far enough. If so, the #sticky would be applied with an CSS class, which makes it sticky to the page; if not, then remove the CSS class, so it can revert to what it should be when the page just loaded. This way make the code more cleaner. z-index is better to be applied since other elements may have that, make sure the number is high enough so #sticky would be on top of others.
</p>

<p>
    I didn’t invent this, the original code is from an answer1 on Stack Overflow, and that seems to be from Stack Overflow’s code, but with a few of my own modifications. The code requires jQuery. The #sticky-anchor is for providing an offset reference, so we can compare with and know if visitor scrolls down far enough. If so, the #sticky would be applied with an CSS class, which makes it sticky to the page; if not, then remove the CSS class, so it can revert to what it should be when the page just loaded. This way make the code more cleaner. z-index is better to be applied since other elements may have that, make sure the number is high enough so #sticky would be on top of others.
</p>
<p>
    I didn’t invent this, the original code is from an answer1 on Stack Overflow, and that seems to be from Stack Overflow’s code, but with a few of my own modifications. The code requires jQuery. The #sticky-anchor is for providing an offset reference, so we can compare with and know if visitor scrolls down far enough. If so, the #sticky would be applied with an CSS class, which makes it sticky to the page; if not, then remove the CSS class, so it can revert to what it should be when the page just loaded. This way make the code more cleaner. z-index is better to be applied since other elements may have that, make sure the number is high enough so #sticky would be on top of others.
</p>


<p>
    I didn’t invent this, the original code is from an answer1 on Stack Overflow, and that seems to be from Stack Overflow’s code, but with a few of my own modifications. The code requires jQuery. The #sticky-anchor is for providing an offset reference, so we can compare with and know if visitor scrolls down far enough. If so, the #sticky would be applied with an CSS class, which makes it sticky to the page; if not, then remove the CSS class, so it can revert to what it should be when the page just loaded. This way make the code more cleaner. z-index is better to be applied since other elements may have that, make sure the number is high enough so #sticky would be on top of others.
</p>

<div id="sticky-anchor2"></div>
<div id="sticky2">blue</div>


<p>
    I didn’t invent this, the original code is from an answer1 on Stack Overflow, and that seems to be from Stack Overflow’s code, but with a few of my own modifications. The code requires jQuery. The #sticky-anchor is for providing an offset reference, so we can compare with and know if visitor scrolls down far enough. If so, the #sticky would be applied with an CSS class, which makes it sticky to the page; if not, then remove the CSS class, so it can revert to what it should be when the page just loaded. This way make the code more cleaner. z-index is better to be applied since other elements may have that, make sure the number is high enough so #sticky would be on top of others.
</p>


<p>
    I didn’t invent this, the original code is from an answer1 on Stack Overflow, and that seems to be from Stack Overflow’s code, but with a few of my own modifications. The code requires jQuery. The #sticky-anchor is for providing an offset reference, so we can compare with and know if visitor scrolls down far enough. If so, the #sticky would be applied with an CSS class, which makes it sticky to the page; if not, then remove the CSS class, so it can revert to what it should be when the page just loaded. This way make the code more cleaner. z-index is better to be applied since other elements may have that, make sure the number is high enough so #sticky would be on top of others.
</p>


<p>
    I didn’t invent this, the original code is from an answer1 on Stack Overflow, and that seems to be from Stack Overflow’s code, but with a few of my own modifications. The code requires jQuery. The #sticky-anchor is for providing an offset reference, so we can compare with and know if visitor scrolls down far enough. If so, the #sticky would be applied with an CSS class, which makes it sticky to the page; if not, then remove the CSS class, so it can revert to what it should be when the page just loaded. This way make the code more cleaner. z-index is better to be applied since other elements may have that, make sure the number is high enough so #sticky would be on top of others.
</p>

<p>
    I didn’t invent this, the original code is from an answer1 on Stack Overflow, and that seems to be from Stack Overflow’s code, but with a few of my own modifications. The code requires jQuery. The #sticky-anchor is for providing an offset reference, so we can compare with and know if visitor scrolls down far enough. If so, the #sticky would be applied with an CSS class, which makes it sticky to the page; if not, then remove the CSS class, so it can revert to what it should be when the page just loaded. This way make the code more cleaner. z-index is better to be applied since other elements may have that, make sure the number is high enough so #sticky would be on top of others.
</p>



<p>
    I didn’t invent this, the original code is from an answer1 on Stack Overflow, and that seems to be from Stack Overflow’s code, but with a few of my own modifications. The code requires jQuery. The #sticky-anchor is for providing an offset reference, so we can compare with and know if visitor scrolls down far enough. If so, the #sticky would be applied with an CSS class, which makes it sticky to the page; if not, then remove the CSS class, so it can revert to what it should be when the page just loaded. This way make the code more cleaner. z-index is better to be applied since other elements may have that, make sure the number is high enough so #sticky would be on top of others.
</p>


<p>
    I didn’t invent this, the original code is from an answer1 on Stack Overflow, and that seems to be from Stack Overflow’s code, but with a few of my own modifications. The code requires jQuery. The #sticky-anchor is for providing an offset reference, so we can compare with and know if visitor scrolls down far enough. If so, the #sticky would be applied with an CSS class, which makes it sticky to the page; if not, then remove the CSS class, so it can revert to what it should be when the page just loaded. This way make the code more cleaner. z-index is better to be applied since other elements may have that, make sure the number is high enough so #sticky would be on top of others.
</p>

<p>
    I didn’t invent this, the original code is from an answer1 on Stack Overflow, and that seems to be from Stack Overflow’s code, but with a few of my own modifications. The code requires jQuery. The #sticky-anchor is for providing an offset reference, so we can compare with and know if visitor scrolls down far enough. If so, the #sticky would be applied with an CSS class, which makes it sticky to the page; if not, then remove the CSS class, so it can revert to what it should be when the page just loaded. This way make the code more cleaner. z-index is better to be applied since other elements may have that, make sure the number is high enough so #sticky would be on top of others.
</p>


<p>
    I didn’t invent this, the original code is from an answer1 on Stack Overflow, and that seems to be from Stack Overflow’s code, but with a few of my own modifications. The code requires jQuery. The #sticky-anchor is for providing an offset reference, so we can compare with and know if visitor scrolls down far enough. If so, the #sticky would be applied with an CSS class, which makes it sticky to the page; if not, then remove the CSS class, so it can revert to what it should be when the page just loaded. This way make the code more cleaner. z-index is better to be applied since other elements may have that, make sure the number is high enough so #sticky would be on top of others.
</p>


<p>
    I didn’t invent this, the original code is from an answer1 on Stack Overflow, and that seems to be from Stack Overflow’s code, but with a few of my own modifications. The code requires jQuery. The #sticky-anchor is for providing an offset reference, so we can compare with and know if visitor scrolls down far enough. If so, the #sticky would be applied with an CSS class, which makes it sticky to the page; if not, then remove the CSS class, so it can revert to what it should be when the page just loaded. This way make the code more cleaner. z-index is better to be applied since other elements may have that, make sure the number is high enough so #sticky would be on top of others.
</p>

<p>
    I didn’t invent this, the original code is from an answer1 on Stack Overflow, and that seems to be from Stack Overflow’s code, but with a few of my own modifications. The code requires jQuery. The #sticky-anchor is for providing an offset reference, so we can compare with and know if visitor scrolls down far enough. If so, the #sticky would be applied with an CSS class, which makes it sticky to the page; if not, then remove the CSS class, so it can revert to what it should be when the page just loaded. This way make the code more cleaner. z-index is better to be applied since other elements may have that, make sure the number is high enough so #sticky would be on top of others.
</p>

<p>
    I didn’t invent this, the original code is from an answer1 on Stack Overflow, and that seems to be from Stack Overflow’s code, but with a few of my own modifications. The code requires jQuery. The #sticky-anchor is for providing an offset reference, so we can compare with and know if visitor scrolls down far enough. If so, the #sticky would be applied with an CSS class, which makes it sticky to the page; if not, then remove the CSS class, so it can revert to what it should be when the page just loaded. This way make the code more cleaner. z-index is better to be applied since other elements may have that, make sure the number is high enough so #sticky would be on top of others.
</p>


<p>
    I didn’t invent this, the original code is from an answer1 on Stack Overflow, and that seems to be from Stack Overflow’s code, but with a few of my own modifications. The code requires jQuery. The #sticky-anchor is for providing an offset reference, so we can compare with and know if visitor scrolls down far enough. If so, the #sticky would be applied with an CSS class, which makes it sticky to the page; if not, then remove the CSS class, so it can revert to what it should be when the page just loaded. This way make the code more cleaner. z-index is better to be applied since other elements may have that, make sure the number is high enough so #sticky would be on top of others.
</p>

<p>
    I didn’t invent this, the original code is from an answer1 on Stack Overflow, and that seems to be from Stack Overflow’s code, but with a few of my own modifications. The code requires jQuery. The #sticky-anchor is for providing an offset reference, so we can compare with and know if visitor scrolls down far enough. If so, the #sticky would be applied with an CSS class, which makes it sticky to the page; if not, then remove the CSS class, so it can revert to what it should be when the page just loaded. This way make the code more cleaner. z-index is better to be applied since other elements may have that, make sure the number is high enough so #sticky would be on top of others.
</p>

<p>
    I didn’t invent this, the original code is from an answer1 on Stack Overflow, and that seems to be from Stack Overflow’s code, but with a few of my own modifications. The code requires jQuery. The #sticky-anchor is for providing an offset reference, so we can compare with and know if visitor scrolls down far enough. If so, the #sticky would be applied with an CSS class, which makes it sticky to the page; if not, then remove the CSS class, so it can revert to what it should be when the page just loaded. This way make the code more cleaner. z-index is better to be applied since other elements may have that, make sure the number is high enough so #sticky would be on top of others.
</p>


<p>
    I didn’t invent this, the original code is from an answer1 on Stack Overflow, and that seems to be from Stack Overflow’s code, but with a few of my own modifications. The code requires jQuery. The #sticky-anchor is for providing an offset reference, so we can compare with and know if visitor scrolls down far enough. If so, the #sticky would be applied with an CSS class, which makes it sticky to the page; if not, then remove the CSS class, so it can revert to what it should be when the page just loaded. This way make the code more cleaner. z-index is better to be applied since other elements may have that, make sure the number is high enough so #sticky would be on top of others.
</p>

<p>
    I didn’t invent this, the original code is from an answer1 on Stack Overflow, and that seems to be from Stack Overflow’s code, but with a few of my own modifications. The code requires jQuery. The #sticky-anchor is for providing an offset reference, so we can compare with and know if visitor scrolls down far enough. If so, the #sticky would be applied with an CSS class, which makes it sticky to the page; if not, then remove the CSS class, so it can revert to what it should be when the page just loaded. This way make the code more cleaner. z-index is better to be applied since other elements may have that, make sure the number is high enough so #sticky would be on top of others.
</p>

CSS:

p {
    width: 275px;
    padding-right: 15px;
    padding-left: 5px;
}

#sticky {
    background-color: red;
    color: #fff;
    font-size: 2em;
    width: 300px;
    height: 250px;
}

#sticky.stick {
    position: fixed;
    top: 0;
    z-index: 10000;
}

#sticky2 {
    background-color: blue;
    color: #fff;
    font-size: 2em;
    width: 300px;
    height: 600px;
}

#sticky2.stick2 {
    position: fixed;
    top: 0;
    z-index: 10000;
}

jQuery:

function sticky_relocate() {
    var window_top = $(window).scrollTop();
    var div_top = $('#sticky-anchor').offset().top;
    if (window_top > div_top) {
        $('#sticky').addClass('stick');
    } else {
        $('#sticky').removeClass('stick');
    }
}

$(function() {
    $(window).scroll(sticky_relocate);
    sticky_relocate();
});


function sticky_blue() {
    var window_top = $(window).scrollTop();
    var div_top = $('#sticky-anchor2').offset().top;
    if (window_top > div_top) {
        $('#sticky2').addClass('stick2');
    } else {
        $('#sticky2').removeClass('stick2');
    }
}

$(function() {
    $(window).scroll(sticky_blue);
    sticky_blue();
});
3
  • It doesn't seem to be expanding to the bottom of the page. Do you see it happening if you make it 100px high? Jun 16, 2015 at 21:47
  • 1
    It reaches the bottom of the page for me, but only because it is 600px and the page is smaller (with default sizes) than 600px. Changing the second div to 250px (the same size as the first) seems to make it react normally.
    – user1189882
    Jun 16, 2015 at 21:50
  • 1
    It is 600px! And does the expected behaviour! Jun 16, 2015 at 21:51

1 Answer 1

0

The page size is smaller. That's why you feel it. You shouldn't have it as 600px instead it should be 250px.

#sticky2 {
  background-color: blue;
  color: #fff;
  font-size: 2em;
  width: 300px;
  height: 250px;
}

Snippet:

function sticky_relocate() {
  var window_top = $(window).scrollTop();
  var div_top = $('#sticky-anchor').offset().top;
  if (window_top > div_top) {
    $('#sticky').addClass('stick');
  } else {
    $('#sticky').removeClass('stick');
  }
}

$(function() {
  $(window).scroll(sticky_relocate);
  sticky_relocate();
});


function sticky_blue() {
  var window_top = $(window).scrollTop();
  var div_top = $('#sticky-anchor2').offset().top;
  if (window_top > div_top) {
    $('#sticky2').addClass('stick2');
  } else {
    $('#sticky2').removeClass('stick2');
  }
}

$(function() {
  $(window).scroll(sticky_blue);
  sticky_blue();
});
p {
  width: 275px;
  padding-right: 15px;
  padding-left: 5px;
}

#sticky {
  background-color: red;
  color: #fff;
  font-size: 2em;
  width: 300px;
  height: 250px;
}
#sticky.stick {
  position: fixed;
  top: 0;
  z-index: 10000;
}



#sticky2 {
  background-color: blue;
  color: #fff;
  font-size: 2em;
  width: 300px;
  height: 250px;
}
#sticky2.stick2 {
  position: fixed;
  top: 0;
  z-index: 10000;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<p>
  I didn’t invent this, the original code is from an answer1 on Stack Overflow, and that seems to be from Stack Overflow’s code, but with a few of my own modifications. The code requires jQuery.

  The #sticky-anchor is for providing an offset reference, so we can compare with and know if visitor scrolls down far enough. If so, the #sticky would be applied with an CSS class, which makes it sticky to the page; if not, then remove the CSS class, so it can revert to what it should be when the page just loaded. This way make the code more cleaner.

  z-index is better to be applied since other elements may have that, make sure the number is high enough so #sticky would be on top of others.
</p>
<div id="sticky-anchor"></div>
<div id="sticky">red</div>

<p>
  I didn’t invent this, the original code is from an answer1 on Stack Overflow, and that seems to be from Stack Overflow’s code, but with a few of my own modifications. The code requires jQuery.

  The #sticky-anchor is for providing an offset reference, so we can compare with and know if visitor scrolls down far enough. If so, the #sticky would be applied with an CSS class, which makes it sticky to the page; if not, then remove the CSS class, so it can revert to what it should be when the page just loaded. This way make the code more cleaner.

  z-index is better to be applied since other elements may have that, make sure the number is high enough so #sticky would be on top of others.
</p>


<p>
  I didn’t invent this, the original code is from an answer1 on Stack Overflow, and that seems to be from Stack Overflow’s code, but with a few of my own modifications. The code requires jQuery.

  The #sticky-anchor is for providing an offset reference, so we can compare with and know if visitor scrolls down far enough. If so, the #sticky would be applied with an CSS class, which makes it sticky to the page; if not, then remove the CSS class, so it can revert to what it should be when the page just loaded. This way make the code more cleaner.

  z-index is better to be applied since other elements may have that, make sure the number is high enough so #sticky would be on top of others.
</p>

<p>
  I didn’t invent this, the original code is from an answer1 on Stack Overflow, and that seems to be from Stack Overflow’s code, but with a few of my own modifications. The code requires jQuery.

  The #sticky-anchor is for providing an offset reference, so we can compare with and know if visitor scrolls down far enough. If so, the #sticky would be applied with an CSS class, which makes it sticky to the page; if not, then remove the CSS class, so it can revert to what it should be when the page just loaded. This way make the code more cleaner.

  z-index is better to be applied since other elements may have that, make sure the number is high enough so #sticky would be on top of others.
</p>


<p>
  I didn’t invent this, the original code is from an answer1 on Stack Overflow, and that seems to be from Stack Overflow’s code, but with a few of my own modifications. The code requires jQuery.

  The #sticky-anchor is for providing an offset reference, so we can compare with and know if visitor scrolls down far enough. If so, the #sticky would be applied with an CSS class, which makes it sticky to the page; if not, then remove the CSS class, so it can revert to what it should be when the page just loaded. This way make the code more cleaner.

  z-index is better to be applied since other elements may have that, make sure the number is high enough so #sticky would be on top of others.
</p>


<p>
  I didn’t invent this, the original code is from an answer1 on Stack Overflow, and that seems to be from Stack Overflow’s code, but with a few of my own modifications. The code requires jQuery.

  The #sticky-anchor is for providing an offset reference, so we can compare with and know if visitor scrolls down far enough. If so, the #sticky would be applied with an CSS class, which makes it sticky to the page; if not, then remove the CSS class, so it can revert to what it should be when the page just loaded. This way make the code more cleaner.

  z-index is better to be applied since other elements may have that, make sure the number is high enough so #sticky would be on top of others.
</p>




<div id="sticky-anchor2"></div>
<div id="sticky2">blue</div>


<p>
  I didn’t invent this, the original code is from an answer1 on Stack Overflow, and that seems to be from Stack Overflow’s code, but with a few of my own modifications. The code requires jQuery.

  The #sticky-anchor is for providing an offset reference, so we can compare with and know if visitor scrolls down far enough. If so, the #sticky would be applied with an CSS class, which makes it sticky to the page; if not, then remove the CSS class, so it can revert to what it should be when the page just loaded. This way make the code more cleaner.
  z-index is better to be applied since other elements may have that, make sure the number is high enough so #sticky would be on top of others.
</p>


<p>
  I didn’t invent this, the original code is from an answer1 on Stack Overflow, and that seems to be from Stack Overflow’s code, but with a few of my own modifications. The code requires jQuery.

  The #sticky-anchor is for providing an offset reference, so we can compare with and know if visitor scrolls down far enough. If so, the #sticky would be applied with an CSS class, which makes it sticky to the page; if not, then remove the CSS class, so it can revert to what it should be when the page just loaded. This way make the code more cleaner.

  z-index is better to be applied since other elements may have that, make sure the number is high enough so #sticky would be on top of others.
</p>


<p>
  I didn’t invent this, the original code is from an answer1 on Stack Overflow, and that seems to be from Stack Overflow’s code, but with a few of my own modifications. The code requires jQuery.

  The #sticky-anchor is for providing an offset reference, so we can compare with and know if visitor scrolls down far enough. If so, the #sticky would be applied with an CSS class, which makes it sticky to the page; if not, then remove the CSS class, so it can revert to what it should be when the page just loaded. This way make the code more cleaner.

  z-index is better to be applied since other elements may have that, make sure the number is high enough so #sticky would be on top of others.
</p>

<p>
  I didn’t invent this, the original code is from an answer1 on Stack Overflow, and that seems to be from Stack Overflow’s code, but with a few of my own modifications. The code requires jQuery.

  The #sticky-anchor is for providing an offset reference, so we can compare with and know if visitor scrolls down far enough. If so, the #sticky would be applied with an CSS class, which makes it sticky to the page; if not, then remove the CSS class, so it can revert to what it should be when the page just loaded. This way make the code more cleaner.

  z-index is better to be applied since other elements may have that, make sure the number is high enough so #sticky would be on top of others.
</p>



<p>
  I didn’t invent this, the original code is from an answer1 on Stack Overflow, and that seems to be from Stack Overflow’s code, but with a few of my own modifications. The code requires jQuery.

  The #sticky-anchor is for providing an offset reference, so we can compare with and know if visitor scrolls down far enough. If so, the #sticky would be applied with an CSS class, which makes it sticky to the page; if not, then remove the CSS class, so it can revert to what it should be when the page just loaded. This way make the code more cleaner.

  z-index is better to be applied since other elements may have that, make sure the number is high enough so #sticky would be on top of others.
</p>


<p>
  I didn’t invent this, the original code is from an answer1 on Stack Overflow, and that seems to be from Stack Overflow’s code, but with a few of my own modifications. The code requires jQuery.

  The #sticky-anchor is for providing an offset reference, so we can compare with and know if visitor scrolls down far enough. If so, the #sticky would be applied with an CSS class, which makes it sticky to the page; if not, then remove the CSS class, so it can revert to what it should be when the page just loaded. This way make the code more cleaner.

  z-index is better to be applied since other elements may have that, make sure the number is high enough so #sticky would be on top of others.
</p>

<p>
  I didn’t invent this, the original code is from an answer1 on Stack Overflow, and that seems to be from Stack Overflow’s code, but with a few of my own modifications. The code requires jQuery.

  The #sticky-anchor is for providing an offset reference, so we can compare with and know if visitor scrolls down far enough. If so, the #sticky would be applied with an CSS class, which makes it sticky to the page; if not, then remove the CSS class, so it can revert to what it should be when the page just loaded. This way make the code more cleaner.

  z-index is better to be applied since other elements may have that, make sure the number is high enough so #sticky would be on top of others.
</p>


<p>
  I didn’t invent this, the original code is from an answer1 on Stack Overflow, and that seems to be from Stack Overflow’s code, but with a few of my own modifications. The code requires jQuery.

  The #sticky-anchor is for providing an offset reference, so we can compare with and know if visitor scrolls down far enough. If so, the #sticky would be applied with an CSS class, which makes it sticky to the page; if not, then remove the CSS class, so it can revert to what it should be when the page just loaded. This way make the code more cleaner.

  z-index is better to be applied since other elements may have that, make sure the number is high enough so #sticky would be on top of others.
</p>


<p>
  I didn’t invent this, the original code is from an answer1 on Stack Overflow, and that seems to be from Stack Overflow’s code, but with a few of my own modifications. The code requires jQuery.

  The #sticky-anchor is for providing an offset reference, so we can compare with and know if visitor scrolls down far enough. If so, the #sticky would be applied with an CSS class, which makes it sticky to the page; if not, then remove the CSS class, so it can revert to what it should be when the page just loaded. This way make the code more cleaner.

  z-index is better to be applied since other elements may have that, make sure the number is high enough so #sticky would be on top of others.
</p>

<p>
  I didn’t invent this, the original code is from an answer1 on Stack Overflow, and that seems to be from Stack Overflow’s code, but with a few of my own modifications. The code requires jQuery.

  The #sticky-anchor is for providing an offset reference, so we can compare with and know if visitor scrolls down far enough. If so, the #sticky would be applied with an CSS class, which makes it sticky to the page; if not, then remove the CSS class, so it can revert to what it should be when the page just loaded. This way make the code more cleaner.

  z-index is better to be applied since other elements may have that, make sure the number is high enough so #sticky would be on top of others.
</p>


<p>
  I didn’t invent this, the original code is from an answer1 on Stack Overflow, and that seems to be from Stack Overflow’s code, but with a few of my own modifications. The code requires jQuery.

  The #sticky-anchor is for providing an offset reference, so we can compare with and know if visitor scrolls down far enough. If so, the #sticky would be applied with an CSS class, which makes it sticky to the page; if not, then remove the CSS class, so it can revert to what it should be when the page just loaded. This way make the code more cleaner.

  z-index is better to be applied since other elements may have that, make sure the number is high enough so #sticky would be on top of others.
</p>


<p>
  I didn’t invent this, the original code is from an answer1 on Stack Overflow, and that seems to be from Stack Overflow’s code, but with a few of my own modifications. The code requires jQuery.

  The #sticky-anchor is for providing an offset reference, so we can compare with and know if visitor scrolls down far enough. If so, the #sticky would be applied with an CSS class, which makes it sticky to the page; if not, then remove the CSS class, so it can revert to what it should be when the page just loaded. This way make the code more cleaner.

  z-index is better to be applied since other elements may have that, make sure the number is high enough so #sticky would be on top of others.
</p>

<p>
  I didn’t invent this, the original code is from an answer1 on Stack Overflow, and that seems to be from Stack Overflow’s code, but with a few of my own modifications. The code requires jQuery.

  The #sticky-anchor is for providing an offset reference, so we can compare with and know if visitor scrolls down far enough. If so, the #sticky would be applied with an CSS class, which makes it sticky to the page; if not, then remove the CSS class, so it can revert to what it should be when the page just loaded. This way make the code more cleaner.

  z-index is better to be applied since other elements may have that, make sure the number is high enough so #sticky would be on top of others.
</p>

<p>
  I didn’t invent this, the original code is from an answer1 on Stack Overflow, and that seems to be from Stack Overflow’s code, but with a few of my own modifications. The code requires jQuery.

  The #sticky-anchor is for providing an offset reference, so we can compare with and know if visitor scrolls down far enough. If so, the #sticky would be applied with an CSS class, which makes it sticky to the page; if not, then remove the CSS class, so it can revert to what it should be when the page just loaded. This way make the code more cleaner.

  z-index is better to be applied since other elements may have that, make sure the number is high enough so #sticky would be on top of others.
</p>


<p>
  I didn’t invent this, the original code is from an answer1 on Stack Overflow, and that seems to be from Stack Overflow’s code, but with a few of my own modifications. The code requires jQuery.

  The #sticky-anchor is for providing an offset reference, so we can compare with and know if visitor scrolls down far enough. If so, the #sticky would be applied with an CSS class, which makes it sticky to the page; if not, then remove the CSS class, so it can revert to what it should be when the page just loaded. This way make the code more cleaner.

  z-index is better to be applied since other elements may have that, make sure the number is high enough so #sticky would be on top of others.
</p>

<p>
  I didn’t invent this, the original code is from an answer1 on Stack Overflow, and that seems to be from Stack Overflow’s code, but with a few of my own modifications. The code requires jQuery.

  The #sticky-anchor is for providing an offset reference, so we can compare with and know if visitor scrolls down far enough. If so, the #sticky would be applied with an CSS class, which makes it sticky to the page; if not, then remove the CSS class, so it can revert to what it should be when the page just loaded. This way make the code more cleaner.

  z-index is better to be applied since other elements may have that, make sure the number is high enough so #sticky would be on top of others.
</p>

CodePen: http://codepen.io/anon/pen/ZGJMpB

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.