鼠标移开后,页脚的导航子菜单不会保持打开状态,除非我快速将鼠标悬停并悬停在我的 ul 打开子菜单的 left/right 远处

Footer's nav submenu won't stay open after I mouse out unless I mouse over quickly and hover far left/right of my ul open submenu

我正在构建一个响应式网站,我需要一个位于屏幕中间的固定页脚导航,当鼠标悬停在该导航栏上时会激活一个 drop-up 菜单。我对所有内容都只使用百分比(对于将 width-wise 与我的 header 排成长 运行 很重要)这让我有点困惑。

每当我将鼠标悬停在 INFO 上(参见我的 JSFiddle)并尝试将鼠标向上移向子菜单时,一旦我离开 INFO,子菜单就会下降。当我从 #footer-nav ul li:hover > ul 中删除 css 属性时:positionwidthbottommargin-bottom(本质上是将 drop-up 菜单变成 drop-down 菜单),即使我将鼠标移到子菜单上,子菜单仍保持打开状态。我什至可以将 margin-bottom 更改为 20%,并且子菜单在空 space 上方仍将保持打开状态。当我将它设为 drop-up 而不是 drop-down 时,是什么让它关闭?

我还对触发悬停的位置有疑问。我可以将鼠标远移到 INFO 的 left/right 并且我的子菜单仍然弹出。我如何使用当前 div、设置百分比和居中来解决此问题?

相关代码如下:

<div id="background">
  <footer id="footer">
    <div id="footer-nav">
      <ul>
        <li id="info">INFO
          <ul>
            <li id="twitter"><a href="https://twitter.com" 
                target="_blank">TWITTER</a></li>
            <li id="instagram"><a href="https://www.instagram.com" 
                target="_blank">INSTAGRAM</a></li>
            <li id="email"><a href="mailto:email@email.com">EMAIL</a></li> 
          </ul>
        </li>
      </ul>
    </div>
  </footer>
</div>
#background {
    background-color: #FFFFFF;
    margin: 0px;
    padding: 0px;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    position: fixed;
    z-index: -1;
}

#footer {
    position: fixed;
    bottom: 0;
    width: 25%;
    text-align: center;
    margin-bottom: 1%;
    margin-left: -15%;
    left: 50%;
    margin-left: -12.5%;
}

a {
    text-decoration: none;
    color: inherit;
}

#footer-nav ul li:hover > ul {
    display: block;
    position: absolute;
    width: 100%;
    bottom: 100%;
    margin-bottom: 2%;
}

#footer-nav {
    width: 100%;
}

#footer-nav ul {
    font-family: Arial;
    font-size: 100%;
    font-weight: bold;
    color: #000000;
    list-style-type: none;
    text-align: center;
    margin: auto;
    padding-top: 0px;
    padding-right: 0px;
    padding-bottom: 0px;
    padding-left: 0px;
}

#footer-nav ul ul {
    font-family: Arial;
    font-size: 100%;
    color: #000000;
    list-style-type: none;
    font-weight: normal;
    display: none;
}

#email {
    padding-top: 2%;
    padding-bottom: 2%;
    border: 1px solid black;
    color: #000000;
}

#instagram {
    padding-top: 2%;
    padding-bottom: 2%;
    border: 1px solid black;
    color: #000000;
}

#twitter {
    padding-top: 2%;
    padding-bottom: 2%;
    border: 1px solid black;
    color: #000000;
}

问题来自 #footer-nav ul li:hover > ul 上的 margin-bottom: 2%。当您离开 Info 按钮进入此边距时,您不再将鼠标悬停在相关元素上。要更正此问题,只需将其替换为 padding-bottom: 2%:

$(document).ready(function() {    
  $("#email").hover(function() {        
    $("#email").css("background-color", "black");
  }, function() {
    $("#email").css("background-color", "white");
  });
});

$(document).ready(function() {    
  $("#email").hover(function() {        
    $("#email").css("color", "white");
  }, function() {
    $("#email").css("color", "black");
  });
});

$(document).ready(function() {    
  $("#twitter").hover(function() {        
    $("#twitter").css("background-color", "black");
  }, function() {
    $("#twitter").css("background-color", "white");
  });
});

$(document).ready(function() {    
  $("#twitter").hover(function() {        
    $("#twitter").css("color", "white");
  }, function() {
    $("#twitter").css("color", "black");
  });
});

$(document).ready(function() {    
  $("#instagram").hover(function() {        
    $("#instagram").css("background-color", "black");
  }, function() {
    $("#instagram").css("background-color", "white");
  });
});

$(document).ready(function() {    
  $("#instagram").hover(function() {        
    $("#instagram").css("color", "white");
  }, function() {
    $("#instagram").css("color", "black");
  });
});
#background {
  background-color: #FFFFFF;
  margin: 0px;
  padding: 0px;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  position: fixed;
  z-index: -1;
}

#footer {
  position: fixed;
  bottom: 0;
  width: 25%;
  text-align: center;
  margin-bottom: 1%;
  margin-left: -15%;
  left: 50%;
  margin-left: -12.5%;
}

a {
  text-decoration: none;
  color: inherit;
}

#footer-nav ul li:hover>ul {
  display: block;
  position: absolute;
  width: 100%;
  bottom: 100%;
  padding-bottom: 2%;
}

#footer-nav {
  width: 100%;
}

#footer-nav ul {
  font-family: Arial;
  font-size: 100%;
  font-weight: bold;
  color: #000000;
  list-style-type: none;
  text-align: center;
  margin: auto;
  padding-top: 0px;
  padding-right: 0px;
  padding-bottom: 0px;
  padding-left: 0px;
}

#footer-nav ul ul {
  font-family: Arial;
  font-size: 100%;
  color: #000000;
  list-style-type: none;
  font-weight: normal;
  display: none;
}

#email {
  padding-top: 2%;
  padding-bottom: 2%;
  border: 1px solid black;
  color: #000000;
}

#instagram {
  padding-top: 2%;
  padding-bottom: 2%;
  border: 1px solid black;
  color: #000000;
}

#twitter {
  padding-top: 2%;
  padding-bottom: 2%;
  border: 1px solid black;
  color: #000000;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div id="background">
  <footer id="footer">
    <div id="footer-nav">
      <ul>
        <li id="info">INFO
          <ul>
            <li id="twitter"><a href="https://twitter.com" target="_blank">TWITTER</a></li>
            <li id="instagram"><a href="https://www.instagram.com" target="_blank">INSTAGRAM</a></li>
            <li id="email"><a href="mailto:email@email.com">EMAIL</a></li>
          </ul>
        </li>
      </ul>
    </div>
  </footer>
</div>

另请注意,您可以将所有函数合并为一个 $(document).ready,同时还可以合并 .css() 规则。这将显着减少行数,提高可读性。我在以下示例中完成了此操作:

$(document).ready(function() {    
  $("#email").hover(function() {        
    $("#email").css("background-color", "black");
    $("#email").css("color", "white");
  }, function() {
    $("#email").css("background-color", "white");
    $("#email").css("color", "black");
  });

  $("#twitter").hover(function() {        
    $("#twitter").css("background-color", "black");
    $("#twitter").css("color", "white");
  }, function() {
    $("#twitter").css("background-color", "white");
    $("#twitter").css("color", "black");
  });

  $("#instagram").hover(function() {        
    $("#instagram").css("background-color", "black");
    $("#instagram").css("color", "white");
  }, function() {
    $("#instagram").css("background-color", "white");
    $("#instagram").css("color", "black");
  }); 
});
#background {
  background-color: #FFFFFF;
  margin: 0px;
  padding: 0px;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  position: fixed;
  z-index: -1;
}

#footer {
  position: fixed;
  bottom: 0;
  width: 25%;
  text-align: center;
  margin-bottom: 1%;
  margin-left: -15%;
  left: 50%;
  margin-left: -12.5%;
}

a {
  text-decoration: none;
  color: inherit;
}

#footer-nav ul li:hover>ul {
  display: block;
  position: absolute;
  width: 100%;
  bottom: 100%;
  padding-bottom: 2%;
}

#footer-nav {
  width: 100%;
}

#footer-nav ul {
  font-family: Arial;
  font-size: 100%;
  font-weight: bold;
  color: #000000;
  list-style-type: none;
  text-align: center;
  margin: auto;
  padding-top: 0px;
  padding-right: 0px;
  padding-bottom: 0px;
  padding-left: 0px;
}

#footer-nav ul ul {
  font-family: Arial;
  font-size: 100%;
  color: #000000;
  list-style-type: none;
  font-weight: normal;
  display: none;
}

#email {
  padding-top: 2%;
  padding-bottom: 2%;
  border: 1px solid black;
  color: #000000;
}

#instagram {
  padding-top: 2%;
  padding-bottom: 2%;
  border: 1px solid black;
  color: #000000;
}

#twitter {
  padding-top: 2%;
  padding-bottom: 2%;
  border: 1px solid black;
  color: #000000;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div id="background">
  <footer id="footer">
    <div id="footer-nav">
      <ul>
        <li id="info">INFO
          <ul>
            <li id="twitter"><a href="https://twitter.com" target="_blank">TWITTER</a></li>
            <li id="instagram"><a href="https://www.instagram.com" target="_blank">INSTAGRAM</a></li>
            <li id="email"><a href="mailto:email@email.com">EMAIL</a></li>
          </ul>
        </li>
      </ul>
    </div>
  </footer>
</div>

默认情况下,#info 元素将扩展以适合其容器的大小,因为它是块级元素。为了防止它能够悬停在文本的边缘之外,您需要将 #info 设置为 display: inline-block

这会缩小元素的宽度,并使弹出窗口更靠右。为了解决这个问题,您需要在 calc(-50% + 18.67px)#info > ul 上设置 margin-left-50%是继承的左对齐,18.67px是文本的默认宽度INFO。如果您在 #info 上设置 widthcalc() 中的值应该更新以匹配。

这可以在下面看到:

$(document).ready(function() {    
  $("#email").hover(function() {        
    $("#email").css("background-color", "black");
    $("#email").css("color", "white");
  }, function() {
    $("#email").css("background-color", "white");
    $("#email").css("color", "black");
  });

  $("#twitter").hover(function() {        
    $("#twitter").css("background-color", "black");
    $("#twitter").css("color", "white");
  }, function() {
    $("#twitter").css("background-color", "white");
    $("#twitter").css("color", "black");
  });

  $("#instagram").hover(function() {        
    $("#instagram").css("background-color", "black");
    $("#instagram").css("color", "white");
  }, function() {
    $("#instagram").css("background-color", "white");
    $("#instagram").css("color", "black");
  }); 
});
#background {
  background-color: #FFFFFF;
  margin: 0px;
  padding: 0px;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  position: fixed;
  z-index: -1;
}

#footer {
  position: fixed;
  bottom: 0;
  width: 25%;
  text-align: center;
  margin-bottom: 1%;
  margin-left: -15%;
  left: 50%;
  margin-left: -12.5%;
}

a {
  text-decoration: none;
  color: inherit;
}

#footer-nav ul li:hover>ul {
  display: block;
  position: absolute;
  width: 100%;
  bottom: 100%;
  padding-bottom: 2%;
}

#footer-nav {
  width: 100%;
}

#footer-nav ul {
  font-family: Arial;
  font-size: 100%;
  font-weight: bold;
  color: #000000;
  list-style-type: none;
  text-align: center;
  margin: auto;
  padding-top: 0px;
  padding-right: 0px;
  padding-bottom: 0px;
  padding-left: 0px;
}

#footer-nav ul ul {
  font-family: Arial;
  font-size: 100%;
  color: #000000;
  list-style-type: none;
  font-weight: normal;
  display: none;
}

#email {
  padding-top: 2%;
  padding-bottom: 2%;
  border: 1px solid black;
  color: #000000;
}

#instagram {
  padding-top: 2%;
  padding-bottom: 2%;
  border: 1px solid black;
  color: #000000;
}

#twitter {
  padding-top: 2%;
  padding-bottom: 2%;
  border: 1px solid black;
  color: #000000;
}

#info {
  display: inline-block;
}

#info > ul {
  margin-left: calc(-50% + 18.67px);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div id="background">
  <footer id="footer">
    <div id="footer-nav">
      <ul>
        <li id="info">INFO
          <ul>
            <li id="twitter"><a href="https://twitter.com" target="_blank">TWITTER</a></li>
            <li id="instagram"><a href="https://www.instagram.com" target="_blank">INSTAGRAM</a></li>
            <li id="email"><a href="mailto:email@email.com">EMAIL</a></li>
          </ul>
        </li>
      </ul>
    </div>
  </footer>
</div>