0

我想实现一个下拉菜单,将鼠标悬停在菜单中的特定选项上会打开另一组选项,如下所示(悬停在选项 3 上会打开包含选项 4 和 5 的菜单)-

在此处输入图像描述

我一直在使用碳类尝试类似的事情(参考自 - https://the-carbon-components.netlify.app/?nav=dropdownhttps://codepen.io/team/carbon/pen/wEGEoz)但是我无法实现它。

这就是我的代码的样子 - HTML -

<ul data-dropdown data-value class="bx--dropdown" tabindex="0">
      <li class="bx--dropdown-text">Choose an option</li>
      <li>
        <ul class="bx--dropdown-list">
          <li data-option data-value="Option 1" class="bx--dropdown-item"><a class="bx--dropdown-link" href="javascript:void(0)">Option 1</a></li>
          <li data-option data-value="Option 2" class="bx--dropdown-item"><a class="bx--dropdown-link" href="javascript:void(0)">Option 2</a></li>
          <li data-option data-value="Option 3" class="bx--dropdown-item" id="openMenu"><a class="bx--dropdown-link" href="javascript:void(0)">Option 3</a>                                                     
                <ul class="bx--dropdown-list" id="customMenu" style="display:none;margin-left:75px;">
                    <li data-option data-value="Option 4" class="bx--dropdown-item"><a class="bx--dropdown-link" href="javascript:void(0)">Option 4</a></li>
                    <li data-option data-value="Option 5" class="bx--dropdown-item"><a class="bx--dropdown-link" href="javascript:void(0)">Option 5</a></li>
                </ul>                                                 
          </li>
        </ul>
      </li>
    </ul>

CSS -

.bx--dropdown {
  outline: 2px solid transparent;
  outline-offset: -2px;
  position: relative;
  list-style: none;
  display: block;
  background-color: white;
  border: none;
  border-bottom: 1px solid #8C8c8c;
  width: 100%;
  height: 2.5rem;
  cursor: pointer;
  color: #171717;
  outline: 2px solid transparent;
  transition: background-color 70ms cubic-bezier(0.2, 0, 0.38, 0.9);
}

.bx--dropdown-text {
  display: block;
  padding-left: 1rem;
  padding-right: 2.625rem;
  overflow: hidden;
  text-overflow: ellipsis;
  font-size: 0.875rem;
  font-weight: 400;
  line-height: 1.125rem;
  letter-spacing: 0.16px;
}

.bx--dropdown-list {
  background-color: white;
  display: flex;
  flex-direction: column;
  outline: 2px solid transparent;
  outline-offset: -2px;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.3);
  font-size: 0.875rem;
  font-weight: 400;
  line-height: 1.125rem;
  letter-spacing: 0.16px;
  width: 100%;
  list-style: none;
  position: absolute;
  z-index: 1000;
  max-height: 0;
  transition: max-height 110ms cubic-bezier(0.2, 0, 0.38, 0.9);
  overflow-x: auto;
  overflow-y: auto;
}

.bx--dropdown-item {
    transition: visibility 70ms cubic-bezier(0.2, 0, 0.38, 0.9), opacity
        70ms cubic-bezier(0.2, 0, 0.38, 0.9), background-color 70ms
        cubic-bezier(0.2, 0, 0.38, 0.9);
    opacity: 0;
    visibility: inherit;
    position: relative;
}

.bx--dropdown-link {
    display: block;
    outline: 2px solid transparent;
    outline-offset: -2px;
    height: 2.5rem;
    color: #393939;
    text-decoration: none;
    font-weight: normal;
    line-height: 1rem;
    padding: 0.6875rem 0;
    margin: 0 1rem;
    border: 1px solid transparent;
    border-top-color: #e0e0e0;
    text-overflow: ellipsis;
    overflow: hidden;
    white-space: nowrap;
}

.bx--dropdown--open .bx--dropdown-list {
    max-height: 15rem;
    transition: max-height 110ms cubic-bezier(0, 0, 0.38, 0.9);
}

.bx--dropdown--open .bx--dropdown-item {
    opacity: 1;
}

在单击“选择一个选项”框后,此处 .bx--dropdown--open 类被添加到包含类bx--dropdwon的ul中,例如 -

<ul data-dropdown="" data-value="" class="bx--dropdown bx--dropdown--open" tabindex="0">

JS-

$("#openMenu").on("mouseover",function() {showNew();});
$("#openMenu").on("mouseout",function() {showOld();});
function showNew(evt) { 
    $("#customMenu").attr("style", "display:block;margin-left:75px;");
}
function showOld() {
    $("#customMenu").attr("style", "display:none");
}

问题在于,将鼠标悬停在选项 3上时,下拉菜单中会出现滚动条,选项 4选项 5仅显示在初始下拉菜单中 -

在此处输入图像描述在此处输入图像描述在此处输入图像描述

我希望它以与我给出的第一个示例相同的方式出现在该框之外。即使花了很多时间,我也无法理解如何使它工作。任何帮助表示赞赏。

4

1 回答 1

1

我认为仅使用碳是无法做到的。我结合 carbon 和 bootstrap 来实现一个带有多级下拉子项的顶部导航。

https://the-carbon-components.netlify.app/component/ui-shell--default.html

https://bootstrap-menu.com/demos/multilevel.html

.dropdown-menu li {
  position: relative;
}

.nav-item .submenu {
  display: none;
  position: absolute;
  left: 100%;
  top: -7px;
  padding-top: 7px;
}

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

.dropdown-item {
  background-color: #262626;
}
<link href="https://unpkg.com/carbon-components/css/carbon-components.min.css" rel="stylesheet" />
<script src="https://unpkg.com/carbon-components/scripts/carbon-components.min.js"></script>
<header class="bx--header">
  <nav class="bx--header__nav" aria-label="Platform Name" data-header-nav>
    <ul class="bx--header__menu-bar" aria-label="Platform Name">
      <li class="bx--header__submenu" data-header-submenu>
        <a class="bx--header__menu-item bx--header__menu-title" aria-haspopup="true" aria-expanded="false" tabindex="0"><strong> Main </strong>
                    <svg class="bx--header__menu-arrow" width="12" height="7" aria-hidden="true">
                        <path d="M6.002 5.55L11.27 0l.726.685L6.003 7 0 .685.726 0z" />
                    </svg>
                </a>
        <ul class="bx--header__menu navbar-nav" aria-label="Sub1">
          <li class="nav-item dropdown">
            <ul class="dropdown-menu">
              <li>
                <a class="bx--header__menu-item bx--header__menu-title dropdown-item" aria-haspopup="true" aria-expanded="false" tabindex="0" href="#">
                  <strong> Dropdown 1 </strong>   >
                </a>
                <ul class="submenu">
                  <li role="none">
                    <a class="bx--header__menu-item dropdown-item" href="/sub1" tabindex="-1">
                      <span class="bx--text-truncate--end"><strong> Sub-item 1 </strong>
                                        </span>
                    </a>
                  </li>
                  <li role="none">
                    <a class="bx--header__menu-item dropdown-item" href="/sub2" tabindex="-1">
                      <span class="bx--text-truncate--end"><strong> Sub-item 2 </strong>
                                        </span>
                    </a>
                  </li>
                  <li role="none">
                    <a class="bx--header__menu-item dropdown-item" href="/sub3" tabindex="-1">
                      <span class="bx--text-truncate--end"><strong> Sub-item 3 </strong>
                                        </span>
                    </a>
                  </li>
                </ul>
              </li>
            </ul>
          </li>
          <li class="nav-item dropdown">
            <ul class="dropdown-menu">
              <li>
                <a class="bx--header__menu-item bx--header__menu-title dropdown-item" aria-haspopup="true" aria-expanded="false" tabindex="0" href="#">
                  <strong> Dropdown 2 </strong>   >
                </a>
                <ul class="submenu">
                  <li role="none">
                    <a class="bx--header__menu-item dropdown-item" href="/sub4" tabindex="-1">
                      <span class="bx--text-truncate--end"><strong> Sub-item 4 </strong>
                                        </span>
                    </a>
                  </li>
                  <li role="none">
                    <a class="bx--header__menu-item dropdown-item" href="/sub5" tabindex="-1">
                      <span class="bx--text-truncate--end"><strong> Sub-item 5 </strong>
                                        </span>
                    </a>
                  </li>
                </ul>
              </li>
            </ul>
          </li>
          <li role="none">
            <a class="bx--header__menu-item" href="/drop3" tabindex="-1">
              <span class="bx--text-truncate--end"><strong> Dropdown 3 </strong>
                            </span>
            </a>
          </li>
          <li role="none">
            <a class="bx--header__menu-item" href="/drop4" tabindex="-1">
              <span class="bx--text-truncate--end"><strong> Dropdown 4 </strong>
                            </span>
            </a>
          </li>
        </ul>
      </li>
    </ul>
  </nav>
</header>

于 2021-03-11T13:34:13.590 回答