【问题标题】:Vertical submenu when moushover-ing on the horizontal main menu鼠标悬停在水平主菜单上时的垂直子菜单
【发布时间】:2014-10-25 04:57:13
【问题描述】:

我的菜单是水平的.. 我正在尝试制作包含“预订 n 约会”和“取消约会”的子菜单,当鼠标悬停在“预订/取消约会”上时会出现,但它不能正常工作。这里是我的代码:

.sousMenu:hover ul {
  display: block;
}
.sousMenu ul {
  top: 40px;
  display: none;
  list-style-type: none;
}
#nav {
  height: 25px;
  width: 744px;
  background-color: #fffacd;
  text-align: center;
  display: block;
  border-radius: 1em;
  position: relative;
  left: 160px;
  padding: 0;
  display: block;
}
#nav li {
  display: inline;
}
#nav li a {
  font-family: "Josefin Slab", "Helvetica Neue", Helvetica, Arial, sans-serif;
  font-size: 15px;
  text-decoration: none;
  /*float:center;*/
  padding: 10px;
  color: #a68b8b;
  font-family: arial;
}
#nav li:hover {
  background-color: #fff0d0;
  color: #dae067;
}
#nav li:active {
  background-color: #FFFFFF;
}
.nav {
  padding-left: 0;
  margin-bottom: 0;
  list-style: none;
}
.nav > li {
  position: relative;
  display: block;
}
.nav > li > a {
  position: relative;
  display: block;
  padding: 10px 15px;
}
.nav > li > a:hover,
.nav > li > a:focus {
  text-decoration: none;
  background-color: #eee;
}
.nav > li.disabled > a {
  color: #777;
}
.nav > li.disabled > a:hover,
.nav > li.disabled > a:focus {
  color: #777;
  text-decoration: none;
  cursor: not-allowed;
  background-color: transparent;
}
.nav .open > a,
.nav .open > a:hover,
.nav .open > a:focus {
  background-color: #eee;
  border-color: #428bca;
}
.nav .nav-divider {
  height: 1px;
  margin: 9px 0;
  overflow: hidden;
  background-color: #e5e5e5;
}
.nav > li > a > img {
  max-width: none;
}
<ul id="nav">
  <li>
    <a href="index.html">Home</a>

  </li>
  <li class="sousMenu"><a>Book/Cancel an appointment</a>
    <ul>
      <li><a href="book.html">Book an appointment</a>
      </li>
      <li><a href="cancel.html">Cancel an appointment</a>
      </li>
    </ul>
  </li>
  <li>
    <a href="onlineshop.html">Online Shop</a>
  </li>
  <li>
    <a href="viewcatalogue.html">View Catalogue</a>
  </li>
  <li>
    <a href="aboutus.html">About us</a>
  </li>
  <li>
    <a href="contactus.html">Contact us</a>
  </li>
  <li>
    <a href="managerlogin.html">Manager log-in</a>
  </li>
</ul>
</div>

【问题讨论】:

  • 当我将鼠标悬停在“book/cancel”上时,子菜单会出现,但菜单中的其他元素会消失,除了“book/cancel”和“Home”。

标签: html css web submenu


【解决方案1】:

根据您的代码,您需要将子菜单设置为absolute positioned,这样您就可以避免影响其他元素。试试这个:

.sousMenu {
  position:relative;
  }
.sousMenu:hover ul {
  display: block;
  opacity:1;
}
.sousMenu ul {
  transition:1s ease;
  opacity:0;
  position:Absolute;
  top: 100%
  display: none;
  list-style-type: none;
}
#nav {
  height: 25px;
  width: 744px;
  background-color: #fffacd;
  text-align: center;
  display: block;
  border-radius: 1em;
  position: relative;
  left: 160px;
  padding: 0;
  display: block;
}
#nav li {
  display: inline;
}
#nav li a {
  font-family: "Josefin Slab", "Helvetica Neue", Helvetica, Arial, sans-serif;
  font-size: 15px;
  text-decoration: none;
  /*float:center;*/
  padding: 10px;
  color: #a68b8b;
  font-family: arial;
}
#nav li:hover {
  background-color: #fff0d0;
  color: #dae067;
}
#nav li:active {
  background-color: #FFFFFF;
}
.nav {
  padding-left: 0;
  margin-bottom: 0;
  list-style: none;
}
.nav > li {
  position: relative;
  display: block;
}
.nav > li > a {
  position: relative;
  display: block;
  padding: 10px 15px;
}
.nav > li > a:hover,
.nav > li > a:focus {
  text-decoration: none;
  background-color: #eee;
}
.nav > li.disabled > a {
  color: #777;
}
.nav > li.disabled > a:hover,
.nav > li.disabled > a:focus {
  color: #777;
  text-decoration: none;
  cursor: not-allowed;
  background-color: transparent;
}
.nav .open > a,
.nav .open > a:hover,
.nav .open > a:focus {
  background-color: #eee;
  border-color: #428bca;
}
.nav .nav-divider {
  height: 1px;
  margin: 9px 0;
  overflow: hidden;
  background-color: #e5e5e5;
}
.nav > li > a > img {
  max-width: none;
}
<ul id="nav">
  <li>
    <a href="index.html">Home</a>

  </li>
  <li class="sousMenu"><a>Book/Cancel an appointment</a>
    <ul>
      <li><a href="book.html">Book an appointment</a>
      </li>
      <li><a href="cancel.html">Cancel an appointment</a>
      </li>
    </ul>
  </li>
  <li>
    <a href="onlineshop.html">Online Shop</a>
  </li>
  <li>
    <a href="viewcatalogue.html">View Catalogue</a>
  </li>
  <li>
    <a href="aboutus.html">About us</a>
  </li>
  <li>
    <a href="contactus.html">Contact us</a>
  </li>
  <li>
    <a href="managerlogin.html">Manager log-in</a>
  </li>
</ul>
</div>

【讨论】:

  • 非常感谢,现在可以正常使用了,但是没有出现其他选项“取消”。我认为这是因为我的站点中有一个容器,并且它下面有其他选项。有没有办法让它出现在容器上,或者至少将我的子菜单更改为水平的?
  • 我可以使用其他标签代替
      来使子菜单水平吗?
    • 在 li 项上使用 inline-block,在 ul 父项上使用 white-space:nowrap @user3419062
    • 对不起,我不明白你的意思。你给我看的代码是在代码中编辑的好吗?
    • 我将 z-index 值指定为 10,现在它工作正常。非常感谢
    猜你喜欢
    • 2015-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-27
    • 2012-12-16
    相关资源
    最近更新 更多