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

前端 未结 3 669
谎友^
谎友^ 2021-02-07 02:38

When there is no room at the bottom of the viewport to fit dropdown menu, it is displayed at the top of its dropdown button. Is it possible alter this behavior and make dropdown

相关标签:
3条回答
  • 2021-02-07 02:45

    For Bootstrap ≥ 4.1

    Completely disable Popper.js dynamic positioning by setting data-display="static" on dropdown-toggle element (button or link) .

    From Bootstrap 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>

    0 讨论(0)
  • 2021-02-07 02:46

    Bootstrap 4 uses Popper.js for positioning the dropdowns. Additionally, Bootstrap 4 allows you to pass some data-* attributes to control the behaviour of Popper.js on that dropdown.

    The option that needs to be tweaked is the flip option of Popper.js. By default Bootstrap 4 sets this to true, causing dropdowns to appear above the dropdown-toggle element when there is not enough room at the bottom of the viewport.

    To force the dropdown to always appear at the bottom, add data-flip="false" as an attribute of your dropdown-toggle element. For example:

    <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>

    0 讨论(0)
  • 2021-02-07 03:02

    I wanted to tackle this problem with CSS alone.

    Bootstrap's dropdown's uses Popper.js for the positioning of the dropdown menu. This makes the task challenging since Popper.js seems to check and evaluate the position of the dropdown when the window is scrolled so I needed to use an !important rule to override Popper.js.

    Here's the code I came up with, based on your example.

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

    Example codepen: https://codepen.io/Washable/pen/xPdqVp

    This will always force the dropdown to be below the button, even if the button is at the bottom of the screen.

    0 讨论(0)
提交回复
热议问题