简体   繁体   English

position 如何在滚动(置顶)顶部的两个引导导航栏不重叠?

[英]How to position two bootstrap navbar at top on scroll (sticky-top) without overlapping?

I used 2 navbar set to top on scroll using bootstrap 4 sticky-top class.我使用引导程序 4 置顶 class 将 2 个导航栏设置为顶部滚动。 But the problem is 2nd one goes up (overlap) over the 1st one when I scroll and that is why I can not see the 1st one.但问题是当我滚动时,第二个在第一个之上(重叠),这就是为什么我看不到第一个。 2nd One should be placed bellow of the 1st one on scroll.第二个应放在卷轴上第一个的下方。 Is there any way to do that?有没有办法做到这一点? Sample code:示例代码:

<!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>

in bootstrap css, you will find codes like bellow在引导程序 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 you want first navbar should drop down over the 2nd navbar just set the z-index value less then the 1020 like z-index: 1019; 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; So, the final code like所以,最终的代码就像

@supports ((position: -webkit-sticky) or (position: sticky)) {
  .sticky-top2 {
    position: -webkit-sticky;
    position: sticky;
    top: 100px;
    z-index: 1019;
  }
} 

Now change the HTML code where 2nd navbar has the call of .sticky-top to **现在将第二个导航栏调用.sticky-top的 HTML 代码更改为 **

.sticky-top2 .sticky-top2

** **

The problem is that both of them have top: 0;问题是它们都有 top: 0; The second navbar must be moved away from the height of the first one.第二个导航栏必须从第一个导航栏的高度移开。

If you want more detailed help, put in the code如果您需要更详细的帮助,请输入代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM