【问题标题】:Sticky navbar not fixed after scrolling滚动后粘性导航栏未修复
【发布时间】:2019-04-21 19:32:21
【问题描述】:

我遵循了一个关于使用纯 css 没有 javascript 的粘性导航栏的教程。问题是当我向下滚动时,粘性导航栏未固定在页面顶部。在这个粘性导航栏下,我还有 3 个其他部分。每次我向下滚动导航栏在部分下的页面并且不再工作时。在标题中,我有一个全角图像,上面有一些文本和一个按钮。

这是我的 HTML 导航栏代码:

header {
  margin: auto;
  position: relative;
  width: 100%;
  height: 100vh;
  background: linear-gradient(black, transparent, black), url(images/architecture2.jpg);
  background-size: cover;
  background-position: center;
  display: table;
  top: 0;
}

nav {
  position: absolute;
  width: 100%;
  height: 50px;
  background: rgba(0, 0, 0, .1);
  position: sticky;
  top: 0;
}

nav ul {
  display: flex;
  margin: 0;
  padding: 0 100px;
  float: right;
}

nav ul li {
  list-style: none;
}

nav ul li a {
  display: block;
  color: #fff;
  padding: 0 15px;
  text-decoration: none;
  text-transform: capitalize;
  font-weight: bold;
  line-height: 90px;
}

.intro .inner {
  margin-top: 200px;
  position: relative;
  display: flex;
  justify-content: center;
  align-items: center;
  text-align: center;
  z-index: 10;
  color: #fff;
}

.content h1 {
  font-weight: 800;
  color: #fff;
  text-transform: uppercase;
  font-size: 3.5rem;
}

.content p {
  padding: 0;
  margin-top: -35px;
  margin-left: -1px;
  color: #fff;
  font-size: 2.2rem;
  padding-bottom: 35px;
}

.btnD1 {
  border: 2px solid #fff;
  color: #000;
  background: #fff;
  border-radius: 50px;
  padding: 16.5px 50px;
  font-size: 1.15rem;
  font-weight: 500;
  text-decoration: none;
}
<html>

<head>
</head>

<body>
  <div class="wrapper">
    <header>
      <nav>
        <div class="menu">
          <ul>
            <li><a href="#">Acasa</a></li>
            <li><a href="#despre">Despre Noi</a></li>
            <li><a href="#calatorii">Calatorii</a></li>
            <li><a href="#contact">Contact</a></li>
            <li><a href="#">SignIn</a></li>
          </ul>
        </div>
      </nav>

      <div class="intro">
        <div class="inner">
          <div class="content">
            <h1>C&#259l&#259tore&#351te cu noi &#238n jurul lumii</h1>
            <p>Destina&#355ia visat&#259 este la un click distan&#355&#259!</p>
            <a class="btnD1" href="#">Rezerv&#259 acum</a>
          </div>
        </div>
      </div>
    </header>
  </div>
</body>

</html>

对不起,我的英语不好。

谢谢!

【问题讨论】:

  • 粘性元素相对于它的直接父元素起作用,如果您将nav 设置为粘性但header 会出现在屏幕上,那么nav 也会随之出现在屏幕上。改为将header 设为粘性。

标签: html css


【解决方案1】:

之所以如此,是因为在.intro .inner 类中使用了z-index。您需要在 .nav 类上设置 z-index: 11; 以使其显示在所有内容之上。

header {
  margin: auto;
  position: relative;
  width: 100%;
  height: 100vh;
  background: linear-gradient(black, transparent, black), url(images/architecture2.jpg);
  background-size: cover;
  background-position: center;
  display: table;
  top: 0;
}

nav {
  position: absolute;
  width: 100%;
  height: 50px;
  background: rgba(0, 0, 0, .8);
  position: sticky;
  top: 0;
  z-index: 11;
}

nav ul {
  display: flex;
  margin: 0;
  padding: 0 100px;
  float: right;
}

nav ul li {
  list-style: none;
}

nav ul li a {
  display: block;
  color: #fff;
  padding: 0 15px;
  text-decoration: none;
  text-transform: capitalize;
  font-weight: bold;
  line-height: 90px;
}

.intro .inner {
  margin-top: 200px;
  position: relative;
  display: flex;
  justify-content: center;
  align-items: center;
  text-align: center;
  z-index: 10;
  color: #fff;
}

.content h1 {
  font-weight: 800;
  color: #fff;
  text-transform: uppercase;
  font-size: 3.5rem;
}

.content p {
  padding: 0;
  margin-top: -35px;
  margin-left: -1px;
  color: #fff;
  font-size: 2.2rem;
  padding-bottom: 35px;
}

.btnD1 {
  border: 2px solid #fff;
  color: #000;
  background: #fff;
  border-radius: 50px;
  padding: 16.5px 50px;
  font-size: 1.15rem;
  font-weight: 500;
  text-decoration: none;
}
<html>

<head>
</head>

<body>
  <div class="wrapper">
    <header>
      <nav>
        <div class="menu">
          <ul>
            <li><a href="#">Acasa</a></li>
            <li><a href="#despre">Despre Noi</a></li>
            <li><a href="#calatorii">Calatorii</a></li>
            <li><a href="#contact">Contact</a></li>
            <li><a href="#">SignIn</a></li>
          </ul>
        </div>
      </nav>

      <div class="intro">
        <div class="inner">
          <div class="content">
            <h1>C&#259l&#259tore&#351te cu noi &#238n jurul lumii</h1>
            <p>Destina&#355ia visat&#259 este la un click distan&#355&#259!</p>
            <a class="btnD1" href="#">Rezerv&#259 acum</a>
          </div>
        </div>
      </div>
    </header>
  </div>
</body>

</html>

如果您不确定要使用哪些其他 z-index 值。大多数人将其设置为999,以表示应该始终出现在所有内容之上的内容。

【讨论】:

  • 它不起作用。它停在第一部分的末尾
  • 当你说第一部分;您指的是div.intro 还是header 部分?
【解决方案2】:

nav 具有粘性,但仅限于其父级(即.header)内。要解决此问题,只需将 .header 设为粘性 - 这样只要它的父级(即 .wrapper)正在滚动,它就会保持粘性。

并且还给它一个比它的兄弟姐妹更高的 z-index(.intro innerz-index: 10),或者当滚动传递它时它会在 intro inner 下方:

header {
    position: sticky;
    top: 0;
    z-index: 11;
}

【讨论】:

  • 它不会像你想象的那样工作,因为div.intro 也在同一个块内。因此,您最终仍会像现在一样将 nav 放在内容下方
【解决方案3】:

问题在于这种粘稠的礼节。我从导航中删除了它,并添加了一个过渡和 z-index。

nav{
    position: fixed;
    width: 100%;
    height: 50px;
    background: rgba(0,0,0,.2);
    top: 0;
    z-index:999;
    transition: 0.5s;
}

nav ul{
    display: flex;
    margin: 0;
    padding: 0 100px;
    float:right;
}

nav ul li{
    list-style: none;
}

nav ul li a{
    display: block;
    color: #fff;
    padding: 0 15px;
    text-decoration: none;
    text-transform: capitalize;
    font-weight: bold;
    line-height: 90px;
}

谢谢大家!

【讨论】:

    【解决方案4】:

    使包装器的高度自动。这将解决您的问题。

    【讨论】:

    • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
    猜你喜欢
    • 1970-01-01
    • 2019-04-10
    • 2019-03-08
    • 1970-01-01
    • 1970-01-01
    • 2013-07-15
    • 2021-08-16
    • 1970-01-01
    • 2019-05-16
    相关资源
    最近更新 更多