我的下拉菜单无法正常工作

时间:2018-04-06 07:20:38

标签: html css drop-down-menu

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

例如,如果我将鼠标悬停在菜单上,则会出现下拉菜单,但主菜单也会向下移动,直到我停止在主菜单上停留。



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>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:0)

&#13;
&#13;
/* 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;
}
&#13;
<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>
&#13;
&#13;
&#13;

问题这是当您在悬停时将内部下拉列表ul设为display:block时。它正在推动剩余的li项目。

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

答案 1 :(得分:0)

也许是这样的? 我在代码中添加了注释。

&#13;
&#13;
/* 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;
}
&#13;
<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>
&#13;
&#13;
&#13;