带有子菜单的Flex下拉菜单未在正确位置打开

时间:2017-09-20 01:30:02

标签: html css css3 flexbox css-position

我正在构建一个下拉菜单,其中可能包含使用CSS flex的子菜单。这是我的代码:

<div class="nav-menu-dropdown">
  <div class="dropdown-menu-container dropdown-menu-open-left">
    <div class="dropdown-menu">
      <ul>
        <div class="dropdown-menu-item">
          <li class="dropdown-menu-title"><a href="#"><span>Admin Menu</span></a></li>
        </div>
        <div class="dropdown-menu-item">
          <li><a href="#"><span><div class="dropdown-menu-item-with-icon"><div class="dropdown-menu-item-with-icon-text">Admin</div><div class="dropdown-menu-item-with-icon-icon"><span class="fa fa-caret-right"></span></div></div></span></a>
            <div
            class="dropdown-menu-container dropdown-menu-submenu">
              <div class="dropdown-menu">
                <ul>
                  <div class="dropdown-menu-item">
                    <li><a href="#"><span><div class="dropdown-menu-item-with-icon"><div class="dropdown-menu-item-with-icon-text">Option 1</div><div class="dropdown-menu-item-with-icon-icon"><span class="fa fa-building"></span></div></div></span></a></li>
                  </div>
                  <div class="dropdown-menu-item">
                    <li><a href="#"><span><div class="dropdown-menu-item-with-icon"><div class="dropdown-menu-item-with-icon-text">Option 2</div><div class="dropdown-menu-item-with-icon-icon"><span class="fa fa-user"></span></div></div></span></a></li>
                  </div>
                </ul>
              </div>
        </div>
        </li>
    </div>
    <div class="dropdown-menu-item">
      <div class="dropdown-menu-divider"></div>
    </div>
    <div class="dropdown-menu-item">
      <li class="dropdown-menu-title"><a href="#"><span>User Menu</span></a></li>
    </div>
    <div class="dropdown-menu-item">
      <li><a href="#"><span><div class="dropdown-menu-item-with-icon"><div class="dropdown-menu-item-with-icon-text">User</div><div class="dropdown-menu-item-with-icon-icon"><span class="fa fa-caret-right"></span></div></div></span></a>
        <div
        class="dropdown-menu-container dropdown-menu-submenu">
          <div class="dropdown-menu">
            <ul>
              <div class="dropdown-menu-item">
                <li><a href="#"><span><div class="dropdown-menu-item-with-icon"><div class="dropdown-menu-item-with-icon-text">User Option 2</div><div class="dropdown-menu-item-with-icon-icon"><span class="fa fa-users"></span></div></div></span></a></li>
              </div>
              <div class="dropdown-menu-item">
                <li><a href="#"><span><div class="dropdown-menu-item-with-icon"><div class="dropdown-menu-item-with-icon-text">User Option 3</div><div class="dropdown-menu-item-with-icon-icon"><span class="fa fa-caret-right"></span></div></div></span></a></li>
              </div>
            </ul>
          </div>
    </div>
    </li>
  </div>
  <div class="dropdown-menu-item">
    <li><a href="#"><span><div class="dropdown-menu-item-with-icon"><div class="dropdown-menu-item-with-icon-text">Companies</div><div class="dropdown-menu-item-with-icon-icon"><span class="fa fa-caret-left"></span></div></div></span></a></li>
  </div>
  <div class="dropdown-menu-item">
    <div class="dropdown-menu-divider"></div>
  </div>
  <div class="dropdown-menu-item">
    <li class="dropdown-menu-title"><a href="#"><span>End Of Options</span></a></li>
  </div>
  </ul>
</div>
</div>
</div>

CSS:

.nav-menu-dropdown {
  position: relative;
  display: flex;
}

.dropdown-menu-container {
  position: absolute;
  display: flex;
  flex-direction: column;
  flex-wrap: nowrap;
  min-width: 100px;
  background-color: lightgrey;
  border: solid;
  border-color: black;
  border-width: 1px;
  border-radius: 4px;
  box-shadow: 0px 2px 2px 0px red;
  top: 105%;
  text-align: left;
  z-index: 1;
}

.dropdown-menu * {
  padding: 0px;
  margin: 0px;
  text-decoration: none;
  list-style-type: none;
}

.dropdown-menu-submenu {
  position: absolute;
  display: none;
}

.dropdown-menu-open-left {
  left: initial;
  right: 0px;
}

.dropdown-menu-item {
  display: relative;
  padding: 3px 3px 3px 3px; // top right botton left
}

.dropdown-menu-item:hover .dropdown-menu-submenu {
  display: flex;
}

.dropdown-menu-item:hover {
  background-color: green;
}

.dropdown-menu-item:active {
  color: green;
  background-color: red;
}

.dropdown-menu-item-with-icon {
  display: flex;
  flex-direction: row;
  justify-content: space-between;
}

.dropdown-menu-item-with-icon-text {
  flex: 8;
}

.dropdown-menu-item-with-icon-icon {
  flex: 2;
  text-align: right;
}

.dropdown-menu-title {
  color: $ux-theme-color-text;
  white-space: nowrap;
  letter-spacing: 1px;
}

.dropdown-menu-divider {
  border-bottom-style: solid;
  border-color: black;
  border-width: 1px;
  margin: 1px -1px;
}
奇怪的是,我不能让我的子菜单在悬停物品的侧面打开。子菜单出现在容器的底部,两个子菜单位于相同的位置。

我认为这应该很简单,但我不知道这里会发生什么。如何在悬停的菜单项旁边显示我的菜单?

可以找到小提琴here

2 个答案:

答案 0 :(得分:2)

定位中的轻微故障 - 您只需在position: relative上添加.dropdown-menu-item - 请参阅下面的演示:

.nav-menu-dropdown {
  position: relative;
  display: flex;
}

.dropdown-menu-container {
  position: absolute;
  display: flex;
  flex-direction: column;
  flex-wrap: nowrap;
  min-width: 100px;
  background-color: lightgrey;
  border: solid;
  border-color: black;
  border-width: 1px;
  border-radius: 4px;
  box-shadow: 0px 2px 2px 0px red;
  top: 105%;
  text-align: left;
  z-index: 1;
}

.dropdown-menu * {
  padding: 0px;
  margin: 0px;
  text-decoration: none;
  list-style-type: none;
}

.dropdown-menu-submenu {
  position: absolute;
  display: none;
}

.dropdown-menu-open-left {
  left: initial;
  right: 0px;
}

.dropdown-menu-item {
  position: relative;
  padding: 3px 3px 3px 3px; 
}

.dropdown-menu-item:hover .dropdown-menu-submenu {
  display: flex;
}

.dropdown-menu-item:hover {
  background-color: green;
}

.dropdown-menu-item:active {
  color: green;
  background-color: red;
}

.dropdown-menu-item-with-icon {
  display: flex;
  flex-direction: row;
  justify-content: space-between;
}

.dropdown-menu-item-with-icon-text {
  flex: 8;
}

.dropdown-menu-item-with-icon-icon {
  flex: 2;
  text-align: right;
}

.dropdown-menu-title {
  color: $ux-theme-color-text;
  white-space: nowrap;
  letter-spacing: 1px;
}

.dropdown-menu-divider {
  border-bottom-style: solid;
  border-color: black;
  border-width: 1px;
  margin: 1px -1px;
}
<div class="nav-menu-dropdown">
  <div class="dropdown-menu-container dropdown-menu-open-left">
    <div class="dropdown-menu">
      <ul>
        <div class="dropdown-menu-item">
          <li class="dropdown-menu-title"><a href="#"><span>Admin Menu</span></a></li>
        </div>
        <div class="dropdown-menu-item">
          <li><a href="#"><span><div class="dropdown-menu-item-with-icon"><div class="dropdown-menu-item-with-icon-text">Admin</div><div class="dropdown-menu-item-with-icon-icon"><span class="fa fa-caret-right"></span></div></div></span></a>
            <div
            class="dropdown-menu-container dropdown-menu-submenu">
              <div class="dropdown-menu">
                <ul>
                  <div class="dropdown-menu-item">
                    <li><a href="#"><span><div class="dropdown-menu-item-with-icon"><div class="dropdown-menu-item-with-icon-text">Option 1</div><div class="dropdown-menu-item-with-icon-icon"><span class="fa fa-building"></span></div></div></span></a></li>
                  </div>
                  <div class="dropdown-menu-item">
                    <li><a href="#"><span><div class="dropdown-menu-item-with-icon"><div class="dropdown-menu-item-with-icon-text">Option 2</div><div class="dropdown-menu-item-with-icon-icon"><span class="fa fa-user"></span></div></div></span></a></li>
                  </div>
                </ul>
              </div>
        </div>
        </li>
    </div>
    <div class="dropdown-menu-item">
      <div class="dropdown-menu-divider"></div>
    </div>
    <div class="dropdown-menu-item">
      <li class="dropdown-menu-title"><a href="#"><span>User Menu</span></a></li>
    </div>
    <div class="dropdown-menu-item">
      <li><a href="#"><span><div class="dropdown-menu-item-with-icon"><div class="dropdown-menu-item-with-icon-text">User</div><div class="dropdown-menu-item-with-icon-icon"><span class="fa fa-caret-right"></span></div></div></span></a>
        <div
        class="dropdown-menu-container dropdown-menu-submenu">
          <div class="dropdown-menu">
            <ul>
              <div class="dropdown-menu-item">
                <li><a href="#"><span><div class="dropdown-menu-item-with-icon"><div class="dropdown-menu-item-with-icon-text">User Option 2</div><div class="dropdown-menu-item-with-icon-icon"><span class="fa fa-users"></span></div></div></span></a></li>
              </div>
              <div class="dropdown-menu-item">
                <li><a href="#"><span><div class="dropdown-menu-item-with-icon"><div class="dropdown-menu-item-with-icon-text">User Option 3</div><div class="dropdown-menu-item-with-icon-icon"><span class="fa fa-caret-right"></span></div></div></span></a></li>
              </div>
            </ul>
          </div>
    </div>
    </li>
  </div>
  <div class="dropdown-menu-item">
    <li><a href="#"><span><div class="dropdown-menu-item-with-icon"><div class="dropdown-menu-item-with-icon-text">Companies</div><div class="dropdown-menu-item-with-icon-icon"><span class="fa fa-caret-left"></span></div></div></span></a></li>
  </div>
  <div class="dropdown-menu-item">
    <div class="dropdown-menu-divider"></div>
  </div>
  <div class="dropdown-menu-item">
    <li class="dropdown-menu-title"><a href="#"><span>End Of Options</span></a></li>
  </div>
  </ul>
</div>
</div>
</div>

答案 1 :(得分:1)

.dropdown-menu * {
  padding: 0px;
  margin: 0px;
  text-decoration: none;
  list-style-type: none;
  position: relative;       /* NEW; establishes the nearest positioned ancestor 
                              (i.e., the bounding box) for abspos descendents */
}

.dropdown-menu-submenu {
  position: absolute;
  display: none;
  right: 105%;            /* NEW */
  top: 0;                 /* NEW */
}

https://jsfiddle.net/xu38Lr5f/1/