javascript - 使用 2 个 div 向下滚动固定 div

标签 javascript jquery html css

我试图让一个 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();
});

最佳答案

页面尺寸较小。这就是为什么你有这种感觉。你不应该将它设为 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>

代码笔:http://codepen.io/anon/pen/ZGJMpB

关于javascript - 使用 2 个 div 向下滚动固定 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30878799/

相关文章:

javascript - 使用 jest 和 enzyme 测试高阶组件

php - 第一次访问索引后启动屏幕消失

javascript - ajax- 将 .php 调用(通过 url)放在主页中

javascript - 如何根据 JS 中的条件传递算术运算符?对 'add' 说 +,对 'times' 说 *

php - 使用 PHP、javascript 或 jQuery 使图像中的所有相对路径 URL 成为完整路径

jquery - 更改多个元素的类

javascript - 正好两个或三个单词的正则表达式

html - 带有标题和自动内容的 CSS 对话框

asp.net - 在我的 asp :menu 中更改链接的 CSS

Javascript:获取对象文字中当前对象属性的名称