带有子菜单的下拉菜单

时间:2018-09-29 14:14:20

标签: html css drop-down-menu menu jmenuitem

我正在做一个项目,我正在为一件作品而苦苦挣扎。这可能很简单,但是我显然在寻找一些东西。我的目标是将鼠标悬停在特定图像上,并有一个菜单和子菜单。当您将鼠标悬停在图片上时,您会看到“类别”,而在右侧则看到该类别的“子菜单”。我可以使用第一个类别,但是无法使用任何后续类别来将“子菜单”更改为右侧。我已包含以下代码。任何帮助将不胜感激-我正努力使这项工作

菜单示例: Menu Example

这是我的样式CSS:

/* Dropdown Button */
.dropbtn {
    border: none;
}

/* The container <div> - needed to position the dropdown content */
.dropdown {
    position: relative;
    display: inline-block;
    z-index: 1;
}

/* Dropdown Content (Hidden by Default) */
.dropdown-content {
    display: none;
    float: right;
    position: absolute;
    background-image: url(../images/Framework-and-Navigation_03.png);
    min-width: 100px;
    box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
    z-index: 1;
}

.dropdown-content2{
display: block;
    position: relative;
    z-index: 1;
        color: black;
    padding: 12px 16px;
    text-decoration: none;
}
.dropdown-content3{
display: block;
    position: relative;
    z-index: 1;
        color: black;
    padding: 12px 16px;
    text-decoration: none;

}
/* Links inside the dropdown */
.dropdown-content a {
    color: black;
    padding: 12px 16px;
    text-decoration: none;
    display: block;
}
.dropdown-content-sub1 {
    color: black;
    text-decoration: none;
    display: none;
    z-index: 1;
}

.dropdown-content-sub2 {
    color: black;
    text-decoration: none;
    display: none;
}

.dropdown-content-sub3 {
    color: black;
    text-decoration: none;
    display: none;
}



/* Show the dropdown menu on hover */
.dropdown:hover .dropdown-content {display: inline-block;}

/* Change the sub menu */
.dropdown-content1:hover .dropdown-content-sub1 {display: block;}

.dropdown-content2:hover .dropdown-content-sub2 {display: inline-block;}

这是我的菜单/子菜单测试:

<div class="dropdown">
      <center>
        <input type="image" class="dropbtn" src="images/menu_07.png"/>
      </center>
<div class="dropdown-content">
<table width="500" border="0" cellspacing="0" cellpadding="0">
  <tbody>
    <tr>
      <td width="158">    
         <div class="dropdown-content1"><a href="">Link 1</a></div>
         <div><a href="#">Link 2</a></div>
         <div><a href="#">Link 3</a></div>   
      </td>
      <td class="dropdown-content-sub1" valign="top"><div>
         <a href="#">Test</a>
         </div>
      </td>
    </tr>
  </tbody>
</table>
</div>
</div>

1 个答案:

答案 0 :(得分:1)

看看这是否可以解决您的问题:)

/* Dropdown Button */
html,
body {
  font-family: arial;
  padding: 0;
  margin: 0;
}

a {
  padding: 10px;
  text-decoration: none;
  display: block;
}

.menu-container {
  width: 615px;
  height: 200px;
  background: #eee url("https://images.unsplash.com/photo-1538098269808-2ace9a4d9680?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=2a48defb8057de5b3f79b49b85c173bc&auto=format&fit=crop&w=500&q=60") no-repeat 200px 0;
  display: none;
  position: absolute;
  top: 50px;
  left: 0;
  box-shadow: 0 0 8px 8px rgba(0, 0, 0, 0.3);
}

ul.menu,
ul.menu ul {
  width: 200px;
  padding: 0;
  margin: 0;
  display: none;
  flex-direction: column;
}
ul.menu li,
ul.menu ul li {
  background: rgba(0, 0, 0, 0.5);
  height: 30px;
  display: flex;
  align-items: center;
}
ul.menu li a,
ul.menu ul li a {
  color: white;
  margin-bottom: 1px;
}
ul.menu li:hover ul,
ul.menu ul li:hover ul {
  display: flex;
}

ul.menu ul {
  position: absolute;
  margin: 0 0 0 200px;
  top: 0;
  z-index: 2;
}

/* The container <div> - needed to position the dropdown content */
.nav {
  background-color: black;
  display: flex;
  flex-direction: row;
}
.nav > .nav-item {
  height: 30px;
  padding: 10px;
  margin-right: 2px;
  background-color: rgba(255, 255, 255, 0.4);
  color: white;
  display: flex;
  align-items: center;
}
.nav > .nav-item:hover > .menu-container,
.nav > .nav-item:hover > .menu-container > .menu {
  display: flex;
}
<div class="nav">
  <div class="nav-item" href="">Nav item 1
    <div class="menu-container">
      <ul class="menu">
        <li><a href="">Menu item 1 ></a>
         <ul>
          <li><a href="">Sub menu 1 item 1</a></li>
          <li><a href="">Sub menu 2 item 2</a></li>
        </ul>
          </li>
        <li><a href="">Menu item 2</a></li>
        <li><a href="">Menu item 3 ></a>
        <ul>
          <li><a href="">Sub menu 2 item 1</a></li>
          <li><a href="">Sub menu 2 item 2</a></li>
        </ul>
          </li>
      </ul>
    </div>
  </div>
  <div class="nav-item" href="">Nav item 2</div>
</div>