[英]How to position two bootstrap navbar at top on scroll (sticky-top) without overlapping?
我使用引导程序 4 置顶 class 将 2 个导航栏设置为顶部滚动。 但问题是当我滚动时,第二个在第一个之上(重叠),这就是为什么我看不到第一个。 第二个应放在卷轴上第一个的下方。 有没有办法做到这一点? 示例代码:
<!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.5.2/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.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.5.2/js/bootstrap.min.js"></script>
</head>
<body style="height:1500px">
<div class="container-fluid">
<br>
<h3>Sticky Navbar</h3>
<p>A sticky navigation bar stays fixed at the top of the page when you scroll past it.</p>
<p>Scroll this page to see the effect. <strong>Note:</strong> sticky-top does not work in IE11 and earlier.</p>
</div>
<nav class="navbar navbar-expand-sm bg-dark navbar-dark sticky-top">
<a class="navbar-brand" href="#">Logo</a>
<ul class="navbar-nav">
<li class="nav-item">
<a class="nav-link" href="#">Link</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Link</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>
<nav class="navbar navbar-expand-sm bg-dark navbar-dark sticky-top sticky-top-2">
<a class="navbar-brand" href="#">Logo 2</a>
<ul class="navbar-nav">
<li class="nav-item">
<a class="nav-link" href="#">Link</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Link</a>
</li>
</ul>
</nav>
</body>
</html>
在引导程序 css 中,您会发现如下代码
@supports ((position: -webkit-sticky) or (position: sticky)) {
.sticky-top {
position: -webkit-sticky;
position: sticky;
top: 0px;
z-index: 1020;
}
}
for the 2nd navbar, you can copy the code and paste in additional css or custom css and paste with renaming the class like .sticky-top-2 and set the top: 100px or the height value you set for the 1st navbar and then if您希望第一个导航栏应该下拉到第二个导航栏上,只需将 z-index 值设置为小于 1020,例如 z-index: 1019; 所以,最终的代码就像
@supports ((position: -webkit-sticky) or (position: sticky)) {
.sticky-top2 {
position: -webkit-sticky;
position: sticky;
top: 100px;
z-index: 1019;
}
}
现在将第二个导航栏调用.sticky-top的 HTML 代码更改为 **
.sticky-top2
**
问题是它们都有 top: 0; 第二个导航栏必须从第一个导航栏的高度移开。
如果您需要更详细的帮助,请输入代码
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.