强制 Bootstrap 下拉菜单始终显示在底部并允许它离开屏幕

Force Bootstrap dropdown menu to always display at the bottom and allow it go offscreen

当视口底部没有空间容纳下拉菜单时,它会显示在下拉按钮的顶部。是否可以改变此行为并使下拉列表始终出现在底部?

    <div class="dropdown">
  <button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
    Dropdown button
  </button>
  <div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
    <a class="dropdown-item" href="#">Action</a>
    <a class="dropdown-item" href="#">Another action</a>
    <a class="dropdown-item" href="#">Something else here</a>
  </div>
</div>

我想单独 CSS 解决这个问题。

Bootstrap 的下拉菜单使用 Popper.js 来定位下拉菜单。这使得任务具有挑战性,因为 Popper.js 似乎在滚动 window 时检查和评估下拉列表的位置,所以我需要使用 !important 规则来覆盖 Popper.js。

这是我根据您的示例得出的代码。

.dropdown-menu{
    transform: translate3d(5px, 35px, 0px)!important;
}

代码笔示例:https://codepen.io/Washable/pen/xPdqVp

这将始终强制下拉菜单位于按钮下方,即使按钮位于屏幕底部也是如此。

Bootstrap 4 使用 Popper.js 定位下拉菜单。此外,Bootstrap 4 允许您传递一些 data-* attributes to control the behaviour of Popper.js on that dropdown.

需要调整的选项是Popper.js的flip选项。默认情况下 Bootstrap 4 将其设置为 true,当视口底部没有足够的空间时,下拉菜单会出现在下拉切换元素上方。

要强制下拉列表始终显示在底部,请添加 data-flip="false" 作为 dropdown-toggle 元素的属性。例如:

<div class="dropdown">
  <button class="btn btn-secondary dropdown-toggle" data-flip="false" type="button" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
    Dropdown button
  </button>
  <div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
    <a class="dropdown-item" href="#">Action</a>
    <a class="dropdown-item" href="#">Another action</a>
    <a class="dropdown-item" href="#">Something else here</a>
  </div>
</div>

对于Bootstrap 5 (≥ 5.0)

通过在 dropdown-toggle 元素(按钮或 link)上设置 data-bs-display="static" 来完全禁用 Popper.js 动态定位。

来自 Bootstrap 5 Docs > Dropdowns: Options:

By default, we use Popper.js for dynamic positioning. Disable this with static.

.window {
    height: 8em;
    overflow: auto;
    margin: 1em;
    padding: 1em;
    border: 10px solid #DFEAF2;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p" crossorigin="anonymous"></script>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet"/>

<div class="window">
    <div class="dropdown">
        <button data-bs-display="static" class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenuButton1" data-bs-toggle="dropdown" aria-expanded="false">
            Dropdown dropdown-toggle with data-bs-display="static"
        </button>
        <ul class="dropdown-menu" aria-labelledby="dropdownMenuButton1">
            <li><a class="dropdown-item" href="#">Action</a></li>
            <li><a class="dropdown-item" href="#">Another action</a></li>
            <li><a class="dropdown-item" href="#">Something else here</a></li>
            <li><a class="dropdown-item" href="#">Action</a></li>
            <li><a class="dropdown-item" href="#">Another action</a></li>
            <li><a class="dropdown-item" href="#">Something else here</a></li>
        </ul>
    </div>
</div>

对于 Bootstrap 4 (4.1 - 4.6)

通过在 dropdown-toggle 元素(按钮或 link)上设置 data-display="static" 完全禁用 Popper.js 动态定位。

来自Bootstrap 4 Docs > Dropdowns: Options

By default, we use Popper.js for dynamic positioning. Disable this with static.

.window {
  height: 8em;
  overflow: auto;
  margin: 1em;
  padding: 1em;
  border: 10px solid #DFEAF2;
}
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet" />
<div class="window">
  <div class="dropdown">
    <button data-display="static" class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
      Dropdown dropdown-toggle with data-display="static"
    </button>
    <div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
      <a class="dropdown-item" href="#">Action</a>
      <a class="dropdown-item" href="#">Another action</a>
      <a class="dropdown-item" href="#">Something else here</a>
      <a class="dropdown-item" href="#">Action</a>
      <a class="dropdown-item" href="#">Another action</a>
      <a class="dropdown-item" href="#">Something else here</a>
    </div>
  </div>
</div>

对于 Bootstrap 5 及更高版本,要禁用动态定位,请将其添加到下拉按钮 class:

data-bs-display="static"

我不得不这样做,因为它根据 window 的滚动位置随机将实际的下拉菜单放在错误的位置。可能是 Popper、Bootstrap 5.1 或 Chromium 的错误...不确定,那将是另一个线程的主题。

看这里:https://getbootstrap.com/docs/5.1/components/dropdowns/#responsive-alignment