如何使滚动折叠导航导航栏固定顶部?

Posted

技术标签:

【中文标题】如何使滚动折叠导航导航栏固定顶部?【英文标题】:How to make scrolling collapsing navigation navbar-fixed-top? 【发布时间】:2017-01-18 17:28:49 【问题描述】:

如何使滚动折叠导航navbar-fixed-topbootstrap4

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
	<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
	<script src="https://cdnjs.cloudflare.com/ajax/libs/tether/1.2.0/js/tether.min.js"></script>
	<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-alpha.3/css/bootstrap.min.css">
	<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.3/js/bootstrap.min.js"></script>
</head>
<body>
	<div class="container-fluid">
		<nav class="navbar navbar-fixed-top navbar-light bg-faded">
			<div class="container">
				<a class="navbar-brand" href="#">Fixed top</a>
				<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#exCollapsingNavbar" aria-controls="exCollapsingNavbar" aria-expanded="false" aria-label="Toggle navigation">
			    &#9776;
			  </button>
			  <div class="collapse" id="exCollapsingNavbar">
			    <div class="p-a-1">
			      <ul class="nav navbar-nav nav-pills nav-stacked">
					  <li class="nav-item">
					    <a class="nav-link active" href="#">Active</a>
					  </li>
					  <li class="nav-item">
					    <a class="nav-link" href="#">Link</a>
					  </li>
					  <li class="nav-item">
					    <a class="nav-link" href="#">Link</a>
					  </li>
					  <li class="nav-item">
					    <a class="nav-link disabled" href="#">Disabled</a>
					  </li>
					  <li class="nav-item">
					    <a class="nav-link disabled" href="#">Disabled</a>
					  </li>
					  <li class="nav-item">
					    <a class="nav-link disabled" href="#">Disabled</a>
					  </li>
					  <li class="nav-item">
					    <a class="nav-link disabled" href="#">Disabled</a>
					  </li>
					  <li class="nav-item">
					    <a class="nav-link disabled" href="#">Disabled</a>
					  </li>
					  <li class="nav-item">
					    <a class="nav-link disabled" href="#">Disabled</a>
					  </li>
					  <li class="nav-item">
					    <a class="nav-link disabled" href="#">Disabled</a>
					  </li>
					  <li class="nav-item">
					    <a class="nav-link disabled" href="#">Disabled</a>
					  </li>
					  <li class="nav-item">
					    <a class="nav-link disabled" href="#">Disabled</a>
					  </li>
					  <li class="nav-item">
					    <a class="nav-link disabled" href="#">Disabled</a>
					  </li>
					  <li class="nav-item">
					    <a class="nav-link disabled" href="#">Disabled</a>
					  </li>
					  <li class="nav-item">
					    <a class="nav-link disabled" href="#">Disabled</a>
					  </li>
					  <li class="nav-item">
					    <a class="nav-link disabled" href="#">Disabled</a>
					  </li>
					  <li class="nav-item">
					    <a class="nav-link disabled" href="#">Disabled</a>
					  </li>
					  <li class="nav-item">
					    <a class="nav-link disabled" href="#">Disabled</a>
					  </li>
					</ul>
			    </div>
			  </div>
			</div>
		</nav>
	</div>
</body>
</html>

【问题讨论】:

你能展示你的期望吗?我不太明白你的问题。 @EdvinTenovimas,想和这里一样(你需要缩小浏览器窗口)-output.jsbin.com/dovil,但只有折叠的内容不应该显示在导航栏中。 如果不希望内容显示在导航栏中,应该放在哪里? @EdvinTenovimas &lt;div class="collapse" id="exCollapsingNavbar"&gt;..content...&lt;/div&gt; 但该内容在技术上位于导航栏中(此内容上方有父 nav)。 【参考方案1】:

在 4 alpha 中,您添加一些 CSS 以使导航栏在像 3.x 一样折叠时垂直堆叠..

.navbar .navbar-nav>.nav-item 
    float: none;
    margin-left: .1rem;

.navbar .navbar-nav 
    float:none !important;
    max-height: 240px;
    overflow: auto;

.navbar .collapse.in, .navbar .collapsing  
    clear:both;

演示: http://www.codeply.com/go/zeSsTQr8S9

【讨论】:

以上是关于如何使滚动折叠导航导航栏固定顶部?的主要内容,如果未能解决你的问题,请参考以下文章

Bootstrap 4 固定顶部导航和固定侧边栏

如何创建滚动后固定在顶部的粘性导航栏

HTML5如何才能让导航栏固定顶部不动,且!且!且!不遮挡住下面的DIV???

单击时使导航栏折叠/关闭

如何在滚动时隐藏导航栏,除非在屏幕顶部

如何在引导程序 3 中向下滚动带有固定标题(导航栏)的表格行时将表格标题(标题)粘贴在顶部?