Hover multilevel dropdown

2019-07-10 05:05发布

问题:

I'm trying to create a dropdown menu with submenu that will open when I hover the mouse, but the submenu keeps appearing in the bottom instead of in the right of its parent.

 <li class="dropdown">
   <a href="#" class="navbar-brand" data-toggle="dropdown">News <strong class="caret"></strong></a>
     <ul class="dropdown-menu">
       <li>
         <a href="#">A & E Passer </a>
           <ul>
             <li><a href="#">Elementary</a></li>
             <li><a href="#">Secondary</a></li>
            </ul>
        </li>

回答1:

Bootstrap by default stacks submenu items vertically. I think what you're looking for is a combination of custom css classes involving submenus and dropdown menus.

.dropdown-submenu {
  position: relative;
}

.dropdown-submenu>.dropdown-menu {
  top: 0;
  left: 100%;
  margin-top: -6px;
  margin-left: -1px;
  -webkit-border-radius: 0 6px 6px 6px;
  -moz-border-radius: 0 6px 6px 6px;
  border-radius: 0 6px 6px 6px;
}

.dropdown-submenu:hover>.dropdown-menu {
  display: block;
}

.dropdown-submenu>a:after {
  display: block;
  content: " ";
  float: right;
  width: 0;
  height: 0;
  border-color: transparent;
  border-style: solid;
  border-width: 5px 0 5px 5px;
  border-left-color: #cccccc;
  margin-top: 5px;
  margin-right: -10px;
}

.dropdown-submenu:hover>a:after {
  border-left-color: #ffffff;
}

.dropdown-submenu.pull-left {
  float: none;
}

.dropdown-submenu.pull-left>.dropdown-menu {
  left: -100%;
  margin-left: 10px;
  -webkit-border-radius: 6px 0 6px 6px;
  -moz-border-radius: 6px 0 6px 6px;
  border-radius: 6px 0 6px 6px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>


<ul class="dropdown-menu" role="menu" aria-labelledby="dropdownMenu" style="display: block; position: static; margin-bottom: 5px; *width: 180px;">
  <li class="dropdown-submenu ">
    <a tabindex="-1" class="dropdown-toggle topLevel" data-toggle="dropdown" href="#">
        News
        <i class="icon icon-caret-right"></i>
      </a>
    <ul class="dropdown-menu">
      <li class="dropdown-submenu">
        <a href="#">A & E Passer</a>
        <ul class="dropdown-menu">
          <li><a href="#">Elementary</a></li>
          <li><a href="#">Secondary</a></li>
        </ul>
      </li>
    </ul>
  </li>
</ul>