【问题标题】:My drop down menu is not working properly我的下拉菜单无法正常工作
【发布时间】:2018-04-06 15:30:24
【问题描述】:

每当我将鼠标悬停在菜单上时,我希望它显示一个下拉菜单,但我得到了这个奇怪的故障效果,我的代码有问题吗?

例如,如果我将鼠标悬停在菜单上,则会出现下拉菜单,但随后主菜单也会下降,直到我停止将鼠标悬停在主菜单上。

body {
  text-align: center;
  margin: 0;
  background-color: white;
}

nav {
  position: fixed;
  width: 100%;
  height: 100px;
  background-color: rgba(0, 0, 0, 0.2);
  z-index: 99;
  color: white;
}

nav li {
  display: inline-block;
  list-style: none;
  margin-left: 40px;
  font-size: 25px;
  line-height: 80px;
}

nav a {
  color: white;
  font-family: arial;
  text-decoration: none;
}

nav a:hover {
  border-bottom-color: #29b26b;
  border-bottom-style: solid;
  border-bottom-width: 100%;
}

nav ul li ul li {
  display: none;
  background-color: black;
}

nav ul li:hover ul li {
  display: block;
}
<nav>
  <ul>
    <li><a href="#">Home</a>
      <ul>
        <li><a href="#">Button 1</a></li>
        <li><a href="#">Button 2</a></li>
      </ul>
    </li>

    <li><a href="#">About Us</a>
      <ul>
        <li><a href="#">Button 1</a></li>
        <li><a href="#">Button 2</a></li>
      </ul>
    </li>

    <li><a href="#">The Team</a>
      <ul>
        <li><a href="#">Button 1</a></li>
        <li><a href="#">Button 2</a></li>
      </ul>
    </li>
  </ul>
</nav>

【问题讨论】:

    标签: html css drop-down-menu


    【解决方案1】:

    /* Add these two classses */
    nav ul {
      position:relative;
    }
    
    nav ul li ul{
      position:absolute;
    }
    
    /* Add these two classses ------------ End */
    
    body {
      text-align: center;
      margin: 0;
      background-color: white;
    }
    
    nav {
      position: fixed;
      width: 100%;
      height: 100px;
      background-color: rgba(0, 0, 0, 0.2);
      z-index: 99;
      color: white;
    }
    
    nav li {
      display: inline-block;
      list-style: none;
      margin-left: 40px;
      font-size: 25px;
      line-height: 80px;
    }
    
    nav a {
      color: white;
      font-family: arial;
      text-decoration: none;
    }
    
    nav a:hover {
      border-bottom-color: #29b26b;
      border-bottom-style: solid;
      border-bottom-width: 100%;
    }
    
    nav ul li ul li {
      display: none;
      background-color: black;
    }
    
    nav ul li:hover ul li {
      display: block;
    }
    <nav>
      <ul>
        <li><a href="#">Home</a>
          <ul>
            <li><a href="#">Button 1</a></li>
            <li><a href="#">Button 2</a></li>
          </ul>
        </li>
    
        <li><a href="#">About Us</a>
          <ul>
            <li><a href="#">Button 1</a></li>
            <li><a href="#">Button 2</a></li>
          </ul>
        </li>
    
        <li><a href="#">The Team</a>
          <ul>
            <li><a href="#">Button 1</a></li>
            <li><a href="#">Button 2</a></li>
          </ul>
        </li>
      </ul>
    </nav>

    问题是当您在悬停时将内部 dropdowm ul 设置为display:block。它正在将剩余的li 项目向下推。

    您应该将下拉列表 ul position 设置为 absolute 并将父 ul position 设置为 relative

    【讨论】:

    • 感谢您解决问题的帮助 =D 但我不明白这一点。导航 ul { 位置:相对; } 导航 ul li ul { 位置:绝对;这到底是做什么的?
    • @JoeSmith,看看我刚刚粘贴的小提琴,当你不指定位置属性时会发生这种情况。你看不到,因为你的身体标签颜色和字体颜色是白色的。
    • 父级li的身高变长,导致兄弟姐妹被推倒。
    • 另外你们能告诉我,当我在按钮上突出显示时,为什么下拉菜单看起来有点正确,而不是在中心?我该如何解决这个问题?
    【解决方案2】:

    也许是这样的? 我已将 cmets 放入代码中。

    /* CSS */
    body
    {
        text-align: center;
        margin: 0;
        background-color: grey;
    }
    
    
    nav
    {
        position: fixed;
        width: 100%;
        height: 100px;
        background-color: rgba(0, 0, 0, 0.2);
        z-index: 99;
        color: white;
    }
    
    
    nav li
    {
        display: inline-block;
        list-style: none;
    
        margin-left: 40px;
        font-size: 25px;
        line-height: 80px;
    }
    
    nav a
    {
        color: white;
        font-family: arial;
        text-decoration: none;
    }
    
    nav a:hover
    {
        border-bottom-color: #29b26b;
        border-bottom-style: solid;
        border-bottom-width: 100%;
    }
    
    nav ul li  {
     /* Important to make sub elements be positioned as absolute*/
      position: relative;
    }
    
    
    nav ul li ul
    {
      position: absolute;
        display: none;
        background-color: black;
        /* Width is derived from width of parent nav ul, You need to set it here*/
        width: 200px; 
    
    }
    
    /* Show the whole ul part and not individual li elements*/
    nav ul li:hover ul
    {
        display: block;
    }
    <nav>
            <ul>
                <li><a href="#">Home</a>
                    <ul>
                        <li><a href="#">Button 1</a></li>
                        <li><a href="#">Button 2</a></li>
                    </ul>
                </li>
    
                <li><a href="#">About Us</a>
                    <ul>
                        <li><a href="#">Button 1</a></li>
                        <li><a href="#">Button 2</a></li>
                    </ul>
                </li>
    
                <li><a href="#">The Team</a>
                    <ul>
                        <li><a href="#">Button 1</a></li>
                        <li><a href="#">Button 2</a></li>
                    </ul>
                </li>
            </ul>
        </nav>

    【讨论】:

    • 感谢您的帮助 :) 由于我没有足够的声誉,我无法为您的答案投票。
    猜你喜欢
    • 2020-10-27
    • 1970-01-01
    • 1970-01-01
    • 2021-07-03
    • 2012-09-22
    相关资源
    最近更新 更多