如何让div在向下滚动一点后固定在页面上

21

我想在滚动页面时,当第二个 div 遇到顶部边界时将其固定。当它被固定时,它应该随着其他页面一起滚动。我该如何实现这个效果?

#settings{
    width:100%;
    background:#383838;
    height:60px;
}
#menu{
    width:100%;
    position:relative;
    height:100px;
    background:#aaa;
}
#body-content{
    height:900px;
    position:relative;
}

而HTML

<body>
<div id="top">
    <div id="settings">
    </div>
    <div id="menu">
    </div>
</div>
<div id="body-content">
</div>

</body>

在这个例子中http://jsfiddle.net/WBur3/,第二个 div 元素应该在页面滚动时保持固定位置。当我们向上滚动时,它应该回到原来的状态。请帮忙。


所以,如果我没弄错,您想让“菜单”保持固定位置。但是,它必须在页面向下滚动时立即移动到页面顶部? - Bart
是的...当我们向下滚动时,只有“菜单”必须固定在顶部。当我们向上滚动到最大值时,“设置”和“菜单”必须显示。 - user1012181
4个回答

54
你可以使用 jquery 实现此效果。
$(function(){
        // Check the initial Poistion of the Sticky Header
        var stickyHeaderTop = $('#stickyheader').offset().top;

        $(window).scroll(function(){
                if( $(window).scrollTop() > stickyHeaderTop ) {
                        $('#stickyheader').css({position: 'fixed', top: '0px'});
                        $('#stickyalias').css('display', 'block');
                } else {
                        $('#stickyheader').css({position: 'static', top: '0px'});
                        $('#stickyalias').css('display', 'none');
                }
        });
  });

这里是演示

注意:不要忘记在您的页面中包含jquery库链接(假设您是初学者)

<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>

但我不想在<div>中使用表格,有没有其他的方法? - user1012181
3
@user1012181那只是一个例子,你可以添加任何你想要的内容。请查看此链接http://jsfiddle.net/uFq2k/1/ - Sowmya
1
有没有办法让这个 jQuery 脚本在智能手机上也能工作?在智能手机上测试时,菜单无法固定? - Eirik
2
我不知道你是否注意到了,当它被“固定”时,内容会跳到创建的空白区域。你能给出解决方案吗? - Arin Chakraborty

4

我想在这里添加评论,但是我的声望不够。我在一个项目中需要类似的东西,所以我想分享一下我对@Sowmya答案的修改。我稍微整理了一下代码,并使滚动效果更加流畅。JSfiddle

$(function () {
    // Check the initial Poistion of the Sticky Header
    var stickyHeaderTop = $('#stickyheader').offset().top;

    $(window).scroll(function () {
        if ($(window).scrollTop() > stickyHeaderTop) {
            $('#stickyheader').css({
                position: 'fixed',
                top: '0px'
            });
            $('#othercontent').css('margin-top', $('#stickyheader').outerHeight(true) + parseInt($('#unstickyheader').css('marginBottom')));
        } else {
            $('#stickyheader').css({
                position: 'static',
                top: '0px'
            });
            $('#othercontent').css('margin-top', '0px');
        }
    });
});
body {
    font: 13px sans-serif;
}
#stickyheader {
    width: 100%;
    height: 40px;
    background:black;
    color:white;
    margin-bottom: 10px;
}
#unstickyheader {
    margin-bottom: 15px;
}


0
给菜单div分配一个id:
 <div id ="menuContainer">

在项目和页面中包含jQuery:
<script src="Scripts/jquery-1.11.3.min.js" type="text/javascript"></script>

以下是实现所需效果的代码:
<script type="text/javascript">

    $("document").ready(function ($) {

        // On document load get the position of the div u want to stick on certain position
        var offsets = document.getElementById('menuContainer').getBoundingClientRect();

        // Get position from top of browser
                var topoffsets = offsets.top;

                // Binding fuction to windows scroll event
                $(window).bind('scroll', function () {

                    if ($(window).scrollTop() > topoffsets) {

                               $("#menuContainer").css({ top: 0, position: 'fixed' });

                            } else {
                               $("#menuContainer").css({ top: '', position: '' });
                            }
                });
    });


0

没有 `

    <!DOCTYPE html>
    <html>
    <head>
    <title>Bootstrap Example</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
    <style>
    /* Note: Try to remove the following lines to see the effect of CSS positioning */
    .affix {
      top: 0;
      width: 100%;
    }
    .affix + .container-fluid {
      padding-top: 70px;
    }
    </style>
    </head>
    <body>
    <div class="container-fluid" style="background-color:#F44336;color:#fff;height:200px;">
    <h1>Bootstrap Affix Example</h1>
    <h3>Fixed (sticky) navbar on scroll</h3>
    <p>Scroll this page to see how the navbar behaves with data-spy="affix".</p>
    <p>The navbar is attached to the top of the page after you have scrolled a specified amount of pixels.</p>
    </div>

    <nav class="navbar navbar-inverse" data-spy="affix" data-offset-top="197">
    <ul class="nav navbar-nav">
    <li class="active"><a href="#">Basic Topnav</a></li>
    <li><a href="#">Page 1</a></li>
    <li><a href="#">Page 2</a></li>
    <li><a href="#">Page 3</a></li>
    </ul>
    </nav>
    <div class="container-fluid" style="height:1000px">
    <h1>Some text to enable scrolling</h1>
    <h1>Some text to enable scrolling</h1>
    <h1>Some text to enable scrolling</h1>
    <h1>Some text to enable scrolling</h1>
    <h1>Some text to enable scrolling</h1>
    <h1>Some text to enable scrolling</h1>
    <h1>Some text to enable scrolling</h1>
    <h1>Some text to enable scrolling</h1>
    <h1>Some text to enable scrolling</h1>
    <h1>Some text to enable scrolling</h1>
    <h1>Some text to enable scrolling</h1>
    </div>
    </body>
    </html>

我们可以通过使用上述代码来实现这一点。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接