Bootstrap如何在向下滚动网页时将导航栏粘在顶部

时间:2017-01-10 04:03:16

标签: html twitter-bootstrap

我正在使用bootstrap-3.3.7-dist。有人知道如何在向下滚动网页时将导航栏粘在上面吗?请帮忙。

这是HTML代码。

<div class="collapse navbar-collapse" id="myNavbar">
  <ul class="nav navbar-nav">
    <li><a href="index.html">Home Page</a></li>
    <li><a href="#business_register">Register</a></li>
    <li class="dropdown">

      <a class="dropdown-toggle" data-toggle="dropdown" href="#">Management <span class="caret"></span></a>
      <ul class="dropdown-menu">
        <li><a href="#upload_product">Upload</a></li>
        <li><a href="#sort_product">Sorting</a></li>
        <li><a href="#permission_management">Permission</a></li>
        <li><a href="#communicate">Communication</a></li>
        <li><a href="#basic_information">Information</a></li>
      </ul>
    </li>

  </ul>

</div>

2 个答案:

答案 0 :(得分:0)

这是你想要的吗?只需滚动内容,但菜单。

#content{
  height: 500px;
  overflow: scroll;  
}

body{
  overflow: hidden;
}
      
<html>
  <head>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
  </head>
<body>
<nav class="navbar navbar-inverse">
  <div class="container-fluid">
    <div class="navbar-header">
      <a class="navbar-brand" href="#">WebSiteName</a>
    </div>
    <ul class="nav navbar-nav">
      <li class="active"><a href="#">Home</a></li>
      <li class="dropdown"><a class="dropdown-toggle" data-toggle="dropdown" href="#">Page 1 <span class="caret"></span></a>
        <ul class="dropdown-menu">
          <li><a href="#">Page 1-1</a></li>
          <li><a href="#">Page 1-2</a></li>
          <li><a href="#">Page 1-3</a></li>
        </ul>
      </li>
      <li><a href="#">Page 2</a></li>
      <li><a href="#">Page 3</a></li>
    </ul>
  </div>
</nav>
<div id="content">
<p>sadsad</p>
<p>sadsad</p>
<p>sadsad</p>
<p>sadsad</p>
<p>sadsad</p>
<p>sadsad</p>
<p>sadsad</p>
<p>sadsad</p>
<p>sadsad</p>
<p>sadsad</p>
<p>sadsad</p>
<p>sadsad</p><p>sadsad</p>
<p>sadsad</p>
<p>sadsad</p>
<p>sadsad</p>
<p>sadsad</p>
<p>sadsad</p><p>sadsad</p>
<p>sadsad</p>
<p>sadsad</p>
<p>sadsad</p>
<p>sadsad</p>
<p>sadsad</p>
</div>
</body>
</html>

答案 1 :(得分:0)

尝试使用id#navbar-static-top并确保模板嵌套bootstrap需要它

getbootstrap.com/components/#navbar-static-top