垂直下拉菜单,在悬停时显示子菜单

时间:2016-04-19 21:42:58

标签: html css menu dropdown

我想创建一个具有以下结构的垂直下拉菜单(对于每个"按钮"一个不同的"下拉" div)。这个想法是当我将鼠标悬停在"按钮& #34;,"下拉"在"按钮"上显示具有相同的尺寸("下拉"缩小到"按钮"尺寸相等的高度" subnav1")或显示左边"下拉" (再次以相同的高度" subnav1")。然而,问题在于我无法做出#34;下拉" 显示:阻止开启"按钮"悬停,在第二种情况下,"按钮"选择" subnav1"元件。 的 HTML:

<div class="nav">
    <ul class="nav1">
        <li class="button"><div class="subnav">BUTTON1</div></li>
        <li class="button"><div class="subnav">BUTTON2</div></li>
        <li class="button"><div class="subnav">BUTTON3</div><li>
        <li class="button"><div class="subnav">BUTTON4</div></li>
        <li class="button"><div class="subnav">BUTTON5</div></li>
    </ul></div>
    <div class="dropdown">
        <A href="#" class="subnav1">SUBBUTTON1</a>
        <A href="#" class="subnav1">SUBBUTTON1</a>
        <A href="#" class="subnav1">SUBBUTTON1</a>
    </div>

CSS:

.nav {
    width: 180px;
    padding: 1vm;
    float:left;
    height:450px;
    position:relative;
    text-align:center;
    background-color:#a56d3b;
}
ul{
    list-style:none;
    margin: 0;
    padding: 0;
    text-align:right;
}
.nav a{
    text-decoration:none;

}
.subnav{
    font-weight:bold;
    text-align:center;
    color:#FCFBE3;
    font-family: 'PT Serif', serif;
    font-size:1.3em;
    display:block;
}
.button{
    border-bottom:1PX DOTTED #FFECBA;
    border-right:1PX DOTTED #FFECBA;
    height:90px;
    width:100%;
    padding:2em 0 0 0 ;
    cursor:pointer;
}
.button:last-child{
    border-bottom:0px;
}
.button:hover{
    background-color:rgba(0,0,0,0.5);
}
.dropdown{
    height:89px;
    float:left;
    width:auto;
    min-width:100px;
    text-align:center;
    margin:0 180px;
    position:absolute;
    cursor:pointer;
    border:1px solid black;

}

.subnav1{
    height:30px;
    width:180px;
    padding:5px 0;
    color:black;
    font-family: 'PT Serif', serif;
    font-size:1em;
    color:white;
    text-decoration:none;
    background-color:#D8A06E;
        DISPLAY:BLOCK;
}
.subnav1:hover{
    background-color:rgba(0,0,0,0.5);
}
.nav.nav1 .button:hover .dropdown{
    display:block;
}

2 个答案:

答案 0 :(得分:0)

您使用的是HTML导航的绝对错误标记,正确有效的结构是:

<nav class="nav">
  <ul>
    <li>
      <a href="">1st Level</a>
      <ul>
        <li><a href="">2nd Level</a></li>
        <li><a href="">2nd Level</a></li>
        <li><a href="">2nd Level</a></li>
      </ul>
    </li>
    <li>
      <a href="">1st Level</a>
      <ul>
        <li><a href="">2nd Level</a></li>
        <li><a href="">2nd Level</a></li>
        <li><a href="">2nd Level</a></li>
      </ul>
    </li>
    <li>
      <a href="">1st Level</a>
      <ul>
        <li><a href="">2nd Level</a></li>
        <li><a href="">2nd Level</a></li>
        <li><a href="">2nd Level</a></li>
      </ul>
    </li>
  </ul>
</nav>

然后它就像馅饼一样容易,只是造型:

.nav {
  display: block;
  font-family: sans-serif;
}
.nav ul {
  padding: 0;
  list-style: none;
}
.nav ul li {
  display: inline-block;
  position: relative;
  white-space: nowrap;
}
.nav ul li a {
  display: block;
  padding: 8px 16px;
  text-decoration: none;
  color: #000000;
}
.nav ul li ul {
  display: none;
  position: absolute;
  top: 100%;
  left: 0;
  border: 1px solid #000000;
}
.nav ul li ul li {
  display: block;
}
.nav ul li:hover ul {
  display: block;
}

请参阅此小提琴https://jsfiddle.net/vtt0qnpt/

享受

答案 1 :(得分:0)

我认为这就是你想要的。如果这不是您正在寻找的结果,请告诉我

&#13;
&#13;
.dropbtn {
    background-color: #E61094;
    color: white;
    padding: 16px;
    font-size: 16px;
    border: none;
    cursor: pointer;
}

.nav {
    position: relative;
    display: inline-block;
}

.dropdown-content {
    display: none;
    position: absolute;
    background-color: #f9f9f9;
    min-width: 115px;
    box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
}

.dropdown-content a{
    color: black;
    padding: 12px 16px;
    text-decoration: none;
    display: block;
}

.dropdown-content a:hover {
	background-color: #9E1192
}

.nav:hover .dropdown-content {
    display: block;
}

.nav:hover .dropbtn {
    background-color: #F576EA;
}
&#13;
<div class="nav">
  <button class="dropbtn">Dropdown 1</button>
  <div class="dropdown-content">
   <a href="#">Subbutton 1</a>
   <a href="#">Subbutton 2</a>
   <a href="#">Subbutton 3</a>
  </div>
</div>

<div class="nav">
  <butoon class="dropbtn">Dropdown 2</button>
  <div class="dropdown-content">
   <a href="#">Subbutton 1</a>
   <a href="#">Subbutton 2</a>
   <a href="#">Subbutton 3</a>
  </div>
</div>

<div class="nav">
  <butoon class="dropbtn">Dropdown 3</button>
  <div class="dropdown-content">
   <a href="#">Subbutton 1</a>
   <a href="#">Subbutton 2</a>
   <a href="#">Subbutton 3</a>
  </div>
</div>

<div class="nav">
  <butoon class="dropbtn">Dropdown 4</button>
  <div class="dropdown-content">
   <a href="#">Subbutton 1</a>
   <a href="#">Subbutton 2</a>
   <a href="#">Subbutton 3</a>
  </div>
</div>

<div class="nav">
  <butoon class="dropbtn">Dropdown 5</button>
  <div class="dropdown-content">
   <a href="#">Subbutton 1</a>
   <a href="#">Subbutton 2</a>
   <a href="#">Subbutton 3</a>
  </div>
</div>
&#13;
&#13;
&#13;