【问题标题】:How to make a navbar sticky?如何使导航栏具有粘性?
【发布时间】:2021-06-20 19:08:46
【问题描述】:

我制作了一个可以工作的导航栏,但我不能让它变得粘稠。我试过position: fixedtop: 0 但他们做不到!帮助。他们使情况变得更糟。有人可以帮忙把它粘起来吗?

html {
  scroll-behavior: smooth;
  overflow-x: hidden;
}

body {
  overflow-x: hidden;
  background: linear-gradient(rgba(0, 0, 0, 0.4), rgba(0, 0, 0, 0.3)), url("background.png");
  background-position: center top;
  position: relative;
  z-index: 1;
  background-size: auto;
}

#first {
  backdrop-filter: blur(20px);
  width: 100%;
  height: 100vh;
}

a:hover,
a:visited,
a:link,
a:active {
  text-decoration: none;
  color: white;
}

nav {
  display: flex;
  justify-content: space-between;
  align-items: center;
  min-height: 8vh;
  font-family: 'Arial', sans-serif;
  background-color: rgba(0, 0, 0, 0.5);
}

.logo {
  color: rgb(212, 212, 212);
  text-transform: lowercase;
  letter-spacing: 5px;
  font-size: 30px;
  margin-left: 5vh;
  font-family: "Minecraft Regular", sans-serif;
}

.nav-links {
  display: flex;
  justify-content: space-between;
  width: 35%;
  list-style: none;
}

.nav-links a {
  color: rgb(212, 212, 212);
  text-transform: uppercase;
  text-decoration: none;
  letter-spacing: 1px;
  font-weight: bold;
  font-size: 20px;
}

#register {
  background-image: linear-gradient(to right, #6c7575, #5e8b94);
  padding: 1.5vh;
  border-radius: 25px;
  margin-right: 5vh;
}
<nav>
  <div class="logo">
    <h4 id="logo-text">de_ducks2</h4>
  </div>
  <ul class="nav-links">
    <li><a href="#first">Kezdőlap</a></li>
    <li><a href="#ninth">Kapcsolat</a></li>
    <li><a href="i.html" id="register">Regisztráció</a></li>
  </ul>
  <div class="randomfaszaanimation">
    <div class="line1"></div>
    <div class="line2"></div>
    <div class="line3"></div>
  </div>
</nav>

其他部分用于移动视图。如果它们可以对粘性工作产生影响,我也会发送它们。整个网站都可以在这里找到:https://zsombitech.github.io/DeDucks2-Website/

感谢您的帮助!

【问题讨论】:

    标签: html css navbar sticky


    【解决方案1】:

    试试position: sticky; top: 0; 这是一个内置的css规则,它根据滚动位置在固定和静态之间波动元素的位置。这是一个例子:

    .navbar {
      position: sticky;
      top: 0;
      width: 100%;
      background: #ccc;
      outline: 3px solid #000;
    }
    
    .content {
      padding-bottom: 500px;
      background: #afa;
    }
    <div>
      
      <div class="navbar">This is the navbar</div>
      <div class="content">Lorem ipsum..... Scroll down... Watch the navbar stay in one place... <br><br><br><br><br>Here is some more content.</div>
      
    </div>

    在您的情况下,这不起作用,因为正文具有 css 规则 overflow-x: hidden。因此,您可以将其注入控制台:document.body.style.overflowX = "initial";,然后position: sticky; top: 0px; 应该适用于nav 元素。确保 nav 元素直接在正文中,否则一旦滚动经过 section one,它就会隐藏。

    【讨论】:

    • 那么问题将与其他一些样式有关。请给我一分钟时间阅读您的 css
    • 谢谢,完整的 CSS 代码可通过网站获得(如果需要)
    • 嗯位置固定似乎有效,但粘性不起作用
    • 是的,我刚刚在控制台输入了这个:document.querySelector(".logo").parentElement.setAttribute("style","position: fixed; top: 0px; left: 50%; transform: translateX(-50%); width: 100%;");
    • 但是 .logo 是角落上的标志,当我尝试(我将属性添加到标志元素)时它不起作用。您还有其他建议吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-02
    • 1970-01-01
    • 2014-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多