为什么不定位:我头上的粘性工作?

时间:2017-12-30 23:36:31

标签: html css html5 css3 sticky

我正在使用position: sticky;作为我的标题,但它似乎不起作用。我向下滚动时导航栏不会保留到原位,就像设置position: sticky时一样。

* {
  margin: 0px;
}

nav {
  width: 100%;
  background-color: white;
  box-shadow: 0 0 10px 0 rgba(0, 0, 0, .32);
}

#navigation {
  list-style-type: none;
  padding: 0px;
  margin: 0px;
}

li {
  display: inline-block;
  padding: 0px;
  font-family: Antonio;
  font-size: 3em;
  padding-left: 5px;
  padding-right: 5px;
  padding-top: 2px;
  padding-bottom: 2px;
  margin-right: 2px;
}

li a {
  text-decoration: none;
  color: #33C4C4;
  transition: 0.5s;
}

li a:hover {
  color: black;
  transition: 0.5s;
}

#dropdown-other {
  display: none;
  z-index: 1;
  position: absolute;
  background-color: white;
  border: #33C4C4 1.5px solid;
  border-radius: 10px;
  margin-right: 5px;
}

#dropdown-other a {
  color: #33C4C4;
  text-decoration: none;
  display: block;
  font-size: 0.5em;
  text-align: center;
}

#dropdown-other a:hover {
  color: black;
}

#other:hover #dropdown-other {
  display: block;
}

@media (max-width: 790px) {
  .nav-section {
    display: none !important;
  }
  #expand {
    display: inline-block !important;
  }
  .overflowDropdown {
    display: block !important;
  }
}
<div>
  <nav style="position: sticky; position: -webkit-sticky; top: 0px; z-index: 10;">
    <a href="#">
      <img src="Images/logo.png" id="logo" title="LOGO" />
    </a>
    <ul align="right" id="navigation">
      <li><a class="nav-section" href="#" title="HOME PAGE">HOME</a></li>
      <li><a class="nav-section" href="#" title="SHOP PAGE">SHOP</a></li>
      <li><a class="nav-section" href="#" title="PEOPLE PAGE">PEOPLE</a></li>
      <li><a class="nav-section" href="#" title="ABOUT PAGE">ABOUT</a></li>
      <li id="other">
        <a href="#" title="OTHER PAGES">OTHER</a>
        <div id="dropdown-other">
          <div class="overflowDropdown">
            <a href="#">HOME</a>
            <a href="#">SHOP</a>
            <a href="#">PEOPLE</a>
            <a href="#">ABOUT</a>
          </div>
          <a href="#">CONTACT</a>
          <a href="#">FEED</a>
          <a href="#">BOARD</a>
          <a href="#">CHANGELOG</a>
        </div>
      </li>
    </ul>
  </nav>
</div>

<p style="margin-top: 700px;">test</p>

1 个答案:

答案 0 :(得分:3)

这是因为 div 将其围绕。你的选择:

  • 如图所示,使 父div粘贴
  • 删除 div nav 会粘(因为没有父容器)
  • 如果需要div,请将 导航 从div 中取出。

&#13;
&#13;
* {
  margin: 0px;
}

nav {
  width: 100%;
  background-color: white;
  box-shadow: 0 0 10px 0 rgba(0, 0, 0, .32);
  z-index: 10;
}

#navigation {
  list-style-type: none;
  padding: 0px;
  margin: 0px;
}

li {
  display: inline-block;
  padding: 0px;
  font-family: Antonio;
  font-size: 3em;
  padding-left: 5px;
  padding-right: 5px;
  padding-top: 2px;
  padding-bottom: 2px;
  margin-right: 2px;
}

li a {
  text-decoration: none;
  color: #33C4C4;
  transition: 0.5s;
}

li a:hover {
  color: black;
  transition: 0.5s;
}

#dropdown-other {
  display: none;
  z-index: 1;
  position: absolute;
  background-color: white;
  border: #33C4C4 1.5px solid;
  border-radius: 10px;
  margin-right: 5px;
}

#dropdown-other a {
  color: #33C4C4;
  text-decoration: none;
  display: block;
  font-size: 0.5em;
  text-align: center;
}

#dropdown-other a:hover {
  color: black;
}

#other:hover #dropdown-other {
  display: block;
}

@media (max-width: 790px) {
  .nav-section {
    display: none !important;
  }
  #expand {
    display: inline-block !important;
  }
  .overflowDropdown {
    display: block !important;
  }
}
&#13;
<div style="position: -webkit-sticky;position: sticky;top: 0px;">
  <nav>
    <a href="#">
      <img src="Images/logo.png" id="logo" title="LOGO" />
    </a>
    <ul align="right" id="navigation">
      <li><a class="nav-section" href="#" title="HOME PAGE">HOME</a></li>
      <li><a class="nav-section" href="#" title="SHOP PAGE">SHOP</a></li>
      <li><a class="nav-section" href="#" title="PEOPLE PAGE">PEOPLE</a></li>
      <li><a class="nav-section" href="#" title="ABOUT PAGE">ABOUT</a></li>
      <li id="other">
        <a href="#" title="OTHER PAGES">OTHER</a>
        <div id="dropdown-other">
          <div class="overflowDropdown">
            <a href="#">HOME</a>
            <a href="#">SHOP</a>
            <a href="#">PEOPLE</a>
            <a href="#">ABOUT</a>
          </div>
          <a href="#">CONTACT</a>
          <a href="#">FEED</a>
          <a href="#">BOARD</a>
          <a href="#">CHANGELOG</a>
        </div>
      </li>
    </ul>
  </nav>
</div>

<p style="margin-top: 700px;">test</p>
&#13;
&#13;
&#13;