小编Mon*_*moE的帖子

如何在一定高度后固定滚动div并在到达其他div后停止?

我想在达到某个div后使固定滚动停止.我找到了其他教程,将子div放在父div中.但我不希望这样.

请在下面的代码片段中查看我的解释.我希望它在到达其他div后停止滚动.

任何帮助真的很感激...谢谢.

$(document).scroll(function () {
    //stick nav to top of page
    var y = $(this).scrollTop();
    var navWrap = $('#navWrap').offset().top;
    if (y > navWrap) {
        $('nav').addClass('sticky');
    } else {
        $('nav').removeClass('sticky');
    } 
});
Run Code Online (Sandbox Code Playgroud)
body {
    height:1600px;
    margin:0;
}
#navWrap {
    height:70px
}
nav {
    height: 70px;
    background:gray;
}
.sticky {
    position: fixed;
    top:0;
}

h1 {
    margin: 0;
}
Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<p>Zombie ipsum reversus ab viral inferno, nam rick grimes malum cerebro. De carne lumbering animata corpora quaeritis. Summus brains sit …
Run Code Online (Sandbox Code Playgroud)

html javascript jquery

6
推荐指数
1
解决办法
1万
查看次数

标签 统计

html ×1

javascript ×1

jquery ×1