如何使下拉菜单的高度与引导程序4中子菜单的高度相同

Posted

技术标签:

【中文标题】如何使下拉菜单的高度与引导程序4中子菜单的高度相同【英文标题】:How to make the height of dropdown menu same as the height of submenu in bootstrap 4 【发布时间】:2018-09-05 14:32:57 【问题描述】:

我正在制作一个带有多级下拉菜单的导航栏。我想让下拉菜单的高度与子菜单的高度相同。

像这样:

当子菜单隐藏时,下拉菜单会返回原来的大小。 像这样: 这是我的代码

.dropdown-submenu a::after 
  transform: rotate(-90deg);
  right: 3px;
  top: 40%;


.dropdown-submenu:hover .dropdown-menu,
.dropdown-submenu:focus .dropdown-menu 
  display: block;
  margin-top: -1px;
  margin-left: -1px;
  top: 0px;
  left: 100%;
  min-height: 100%;


.dropdown-menu 
  min-height: 200%;
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>

<nav class="navbar navbar-expand-sm navbar-light ">

  <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
            
            </button>

  <div class="collapse navbar-collapse" id="navbarSupportedContent">
    <ul class="navbar-nav mr-auto">
      <li class="nav-item dropdown">
        <a class="nav-link dropdown-toggle" href="http://example.com" id="navbarDropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                      Dropdown link
                    </a>

        <ul class="dropdown-menu " aria-labelledby="navbarDropdownMenuLink">
          <li><a class="dropdown-item" href="#">Action</a></li>
          <li class="dropdown-submenu"><a class="dropdown-item dropdown-toggle" data-toggle="dropdown" href="#">Something else here</a>
            <div class="dropdown-menu top">
              <a class="dropdown-item" href="#">A</a>
              <a class="dropdown-item" href="#">b</a>
              <a class="dropdown-item" href="#">b</a>
              <a class="dropdown-item" href="#">b</a>
              <a class="dropdown-item" href="#">b</a> <a class="dropdown-item" href="#">b</a> <a class="dropdown-item" href="#">b</a>


            </div>
          </li>
        </ul>
      </li>
    </ul>
</nav>

我该怎么做?

【问题讨论】:

【参考方案1】:

这是你想要的吗?我添加了一个类.add-to-ul 和一个类.add-to-dropdown-div,名字就说明了。并赋予它一些风格。

我还向 nav 添加了一个 ID,以便我可以更轻松地设置引导元素的样式。

.dropdown-submenu a::after 
  transform: rotate(-90deg);
  right: 3px;
  top: 40%;


.dropdown-submenu:hover .dropdown-menu,
.dropdown-submenu:focus .dropdown-menu 
  display: block;
  margin-top: -1px;
  margin-left: -1px;
  top: 0px;
  left: 100%;
  min-height: 100%;


.dropdown-menu 
  min-height: 200%;


#bootstrap-edits .add-to-ul
    position: relative;
    width: 100%;
    top: 0;


#bootstrap-edits .add-to-dropdown-div
  min-height: 100%;
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>

<nav class="navbar navbar-expand-sm navbar-light " id="bootstrap-edits">

  <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
            
            </button>

  <div class="collapse navbar-collapse" id="navbarSupportedContent">
    <ul class="navbar-nav mr-auto">
      <li class="nav-item dropdown">
        <a class="nav-link dropdown-toggle" href="http://example.com" id="navbarDropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                      Dropdown link
                    </a>

        <ul class="dropdown-menu add-to-ul" aria-labelledby="navbarDropdownMenuLink">
          <li><a class="dropdown-item" href="#">Action</a></li>
          <li class="dropdown-submenu"><a class="dropdown-item dropdown-toggle" data-toggle="dropdown" href="#">Something else here</a>
            <div class="dropdown-menu top add-to-dropdown-div">
              <a class="dropdown-item" href="#">A</a>
              <a class="dropdown-item" href="#">b</a>
              <a class="dropdown-item" href="#">b</a>
              <a class="dropdown-item" href="#">b</a>
              <a class="dropdown-item" href="#">b</a> <a class="dropdown-item" href="#">b</a> <a class="dropdown-item" href="#">b</a>


            </div>
          </li>
        </ul>
      </li>
    </ul>
</nav>

【讨论】:

感谢您的评论。结果并不完全是我想要的。我希望下拉菜单会自动匹配子菜单的高度。 现在可以了吗?如果您向其中添加另一个 A 或 b,菜单也会变长以匹配相同的高度。 我在子菜单中添加了另一个 a 或 b。下拉菜单不会自动匹配子菜单的高度。此外,当我单击下拉菜单时,导航栏将展开。

以上是关于如何使下拉菜单的高度与引导程序4中子菜单的高度相同的主要内容,如果未能解决你的问题,请参考以下文章

如何在引导程序中增加菜单栏的高度

如何设置select下拉菜单的高度

select2 高度随主题变化:经典无法正常工作

vue最多显示2个按钮,超出的用下拉菜单显示

单击下拉菜单时如何调整tableView的高度?

使用引导程序使每列内的元素具有相同的高度