导航栏未粘贴在滚动条上



我有一个navbar react组件,它有下面的html,它使用bootstrap。我希望它一旦滚动到顶部(上面有一页内容(,就会粘在上面。我尝试将position-sticky应用于包装器div,也将sticky-top应用于nav本身,但都不起作用。它只是滚动到视野之外。

<div className="d-none d-md-block position-sticky">
<nav className="navbar sticky-top bg-dark navbar-dark navbar-expand">
<a className="navbar-brand" href="#initialImage">
name
</a>
<ul className="navbar-nav">
<li className="nav-item">
<a className="nav-link text-white font-weight-bold" href="#">
Home
</a>
</li>
<li className="nav-item">
<a className="nav-link text-white font-weight-bold" href="#">
Shop Collection One
</a>
</li>
<li className="nav-item">
<a className="nav-link text-white font-weight-bold" href="#">
Shop Collection Two
</a>
</li>
<li className="nav-item">
<a className="nav-link text-white font-weight-bold" href="#">
A Message 
</a>
</li>
<li className="nav-item">
<a className="nav-link text-white font-weight-bold" href="#">
Contact
</a>
</li>
</ul>
</nav>
</div>

您的标记中似乎有一些不正确和不必要的东西。

  1. 您不需要使用单独的div来使BS Navbar响应。BS为其提供了内置类。

  2. 其次,你的li项目不合适。当我使用你的代码时,即使有BS样式表链接,它仍然显示正常的ul链接。

我已经为您格式化了链接(取自w3schools(,并根据您的要求进行了修改。

请注意:在运行代码片段时,由于屏幕大小的原因,您可能会发现链接的一些异常行为。我建议您在web浏览器中运行此代码。使用的引导版本是4.0.

版本1:这个版本很简单。滚动时只需点击导航栏。

<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>
</head>
<body style="height:1500px">
<nav class="navbar navbar-expand-sm bg-dark navbar-dark fixed-top">
<a class="navbar-brand" href="#">My WebsiteName</a>
<ul class="navbar-nav">
<li class="nav-item">
<a class="nav-link" href="#">Shop Collection One</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Shop Collection Two</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">A Message</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Contact</a>
</li>
</ul>
</nav>
<div class="container-fluid" style="margin-top:80px">
<h3>Top Fixed Navbar</h3>
<p>A fixed navigation bar stays visible in a fixed position (top or bottom) independent of the page scroll.</p>
<h1>Scroll this page to see the effect</h1>
</div>
</body>
</html>

版本2:这个导航条是版本1的修改,最初导航条不粘,但当你滚动并达到一定的滚动量时,它会粘在导航条上。

<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>
</head>
<body style="height:1500px">
<div class="container-fluid">
<br>
<h3>Hello World</h3>
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s</p>
</div>
<nav class="navbar navbar-expand-sm bg-dark navbar-dark sticky-top">
<a class="navbar-brand" href="#">MyWebSite</a>
<ul class="navbar-nav">
<li class="nav-item">
<a class="nav-link" href="#">Home</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Shop Collection One</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Shop Collection Two</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">A Message</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Contact</a>
</li>
</ul>
</nav>
<div class="container-fluid"><br>
<p>Some example text. Some example text. Some example text. Some example text. Some example text.</p>
<p>Some example text. Some example text. Some example text. Some example text. Some example text.</p>
<p>Some example text. Some example text. Some example text. Some example text. Some example text.</p>
<p>Some example text. Some example text. Some example text. Some example text. Some example text.</p>
</div>
</body>
</html>

希望这能有所帮助。

最新更新