twbs/bootstrap

Dropdown position relatively to body with boundary option in BS5

Open

#35.774 aperta il 3 feb 2022

Vedi su GitHub
 (14 commenti) (0 reazioni) (0 assegnatari)MDX (78.904 fork)batch import
help wantedjsv5

Metriche repository

Star
 (174.240 star)
Metriche merge PR
 (Merge medio 6g 11h) (47 PR mergiate in 30 g)

Descrizione

I have a Bootstrap 5 dropdown menu inside owl-carousel. but dropdown is being cut off because has outer div overflow:hidden in owl-carousel.

enter image description here

Complete snippet here: jsfiddle

So, I have try to position a dropdown relatively to body, not parent with boundary option from Bootstrap Docs,

Bootstrap docs says that can only be done via javascript (not via data attributes), so I have try below two methods code. but no luck.

Where I'm going wrong. can you please suggest proper solution.

1st try via JavaScript:

var dropdownElementList = [].slice.call(document.querySelectorAll('.dropdown-toggle'))
    var dropdownList = dropdownElementList.map(function (dropdownToggleEl) {
      return new bootstrap.Dropdown(dropdownToggleEl, {
        boundary: document.querySelector('#main-wrapper')
      })
    })

2nd try Using function with popperConfig. Find from here

var dropdown = new bootstrap.Dropdown('.dropdown-toggle', {
  popperConfig: function (defaultBsPopperConfig) {
    
return new bootstrap.Dropdown(dropdownTriggerEl, {
            popperConfig: {
                strategy: "fixed"
            }
        });	
  }
})

Our HTML Code:

<div id="main-wrapper">
<div class="owl-carousel owl-theme">
  <div class="item">
    <div class="d-flex align-items-center">
      <h4>Shilipp Sotocnik</h4>
      
      <div class="dropdown dropdown-lg d-inline-block ms-auto"> 
      <a href="#" role="button" id="dropdownMenu1" data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false"><i class="fas fa-ellipsis-h"></i></a>
        <ul class="dropdown-menu dropdown-menu-end" aria-labelledby="dropdownMenu1">
          <li><a class="dropdown-item" href="#">Link 1</a></li>
          <li><a class="dropdown-item" href="#">Link 2</a></li>
          <li><a class="dropdown-item" href="#">Link 3</a></li>
          <li><a class="dropdown-item" href="#">Link 4</a></li>
        </ul>
      </div>
      
    </div>
  </div>
</div>
</div>

Guida contributor