在特定 DIV 上停止固定 div

Stop fixed div on specific DIV

我有这段代码可以让我的侧边栏在向下滚动时卡住。但是,这会让边栏滚动到页面底部,与网站上的其他元素重叠。

$(function () {
    var counter = 0;
    var s = $("#counter");
    var pos = s.position();
    $(window).scroll(function () {
        var windowpos = $(window).scrollTop();
        if (windowpos >= pos.top) {
            s.addClass("stick");
        } else {
            s.removeClass("stick");
        }
    });

});

<div id="sidebar"> 
  <div id="counter">
   * php dynamic_sidebar( 'Sidebar' ); *
  <div class="clear"></div>
  </div>  
</div>

我现在已经搜索了一个小时,没有找到任何解决方案。当滚动到达 div 页脚 .

时,我希望侧边栏停止被粘住

您将不得不使用 css 来解决这个问题。 只需从底部添加等于底部元素高度的填充。

假设你有一个高度为 50px.Then 的页脚,添加 padding-bottom:50px 到侧边栏并给页脚一些 z-index 也像 z-index:9 或 z-index:99。

如果有帮助请告诉我。

谢谢