Bootstrap,滚动后让导航栏变粘?

时间:2013-07-16 21:17:18

标签: javascript jquery html css twitter-bootstrap

我有一个位于标题图片下方的导航栏。当我向下滚动时,我希望导航栏粘在网页的顶部。我想不到所需的jQuery或CSS,因为导航栏似乎粘在标题图像的正下方,留下了一些空隙。

<div class="headerwrap pull-center">
<div  class="container">
    <div id="header" class="row-fluid">
        <div class="span5" id="phones">
            <img class="phone" src="img/white.png" alt="">
        </div>
        <div class="span7" id="mm-logo">
            <img class="mm-log" src="img/logo.png" alt="">
        </div>
    </div>
</div>
</div>


<div class="navbar navbar-inner" id="border-stuff">
    <div class="span12">

        <!-- .btn-navbar is used as the toggle for collapsed navbar content -->
<a class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</a>

<!-- Everything you want hidden at 768px or less, place within here -->
<div class="nav-collapse collapse" id="center-nav">
<ul class="nav" >
    <li><a href="#h1"><h3>Heading1</h3></a></li><li class="divider-vertical"></li>
    <li><a href="#h2"><h3>Heading2</h3></a></li><li      class="divider-vertical"></li>
    <li><a href="#h3"><h3>Heading3</h3></a></li><li class="divider-vertical">  </li>
    <li><a href="#h4"><h3>Heading4</h3></a></li><li class="divider-vertical"></li>
    <li><a href="#h5"><h3>Heading4</h3></a></li>
</ul>
</div>

</div>
</div>

2 个答案:

答案 0 :(得分:19)

如果您希望导航栏只在标题图像滚动后保持固定在屏幕顶部,您可以使用the bootstrap affix plugin

答案 1 :(得分:4)

您只需将Jquery代码替换为:

$('#sidebar').affix({
  offset: {
    top: 50
  }
});

将顶部调整到最适合的位置。