固定div向下滚动,带有2个div

时间:2015-06-16 21:43:42

标签: javascript jquery html css css3

我试图在向下滚动时让div修复到顶部,然后一旦用户向下滚动到下一个替换第一个div等的div。

滚动到第二个div时,我似乎遇到了问题。它似乎正在扩展到页面的末尾,即使我将CSS设置为600px的高度

我在这个Codepen中运行它:

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();
});

1 个答案:

答案 0 :(得分:0)

页面尺寸较小。这就是你感受到它的原因。您不应该将其设为600px,而应该是250px

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

<强>段:

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