溢出在父div后面发送下拉菜单,z-index不起作用

时间:2018-09-25 03:46:48

标签: javascript html css bootstrap-4

人们, 我似乎无法弄清楚为什么溢出会破坏代码

所以下面的代码在两种情况下都能正常工作。 a)如果我从.bs-exmple中删除了overflow-x和overflow-y 要么 b)如果我删除ul和li

但是我的代码都需要,但是由于这些css和ul和li在代码中,因此下拉列表会在div之后崩溃。我想念什么

.bs-example {
  border: 1px solid black;
  overflow-y: scroll;
  overflow-x: hidden;
  min-height: 100px;
  max-height: 250px;
}

.bs-example .dropdown {
  position: absolute;
  z-index: 999;
}
<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://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<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>

<div class="container">
  <div class="bs-example">
    <ul class="list-group ">
      <li class="list-group-item">
        <div class="dropdown">
          <button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                  Menu
                </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>

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

所以您看到,下图显示了div后面的下拉列表

result

1 个答案:

答案 0 :(得分:1)

尝试一下:

$(document).ready(function() {
  $("#dropdownMenuButton").click(function(){
    $('.dropdown-menu').toggle();
  })
})
.helper {
  min-height: 100px;
  max-height: 250px;
  position: relative;
}

.bs-example{
  border: 1px solid black;
  overflow-y: scroll;
  overflow-x: hidden;  
  min-height: 100px;
  max-height: 250px;
  left: 0px;
  right: 0px;
  position: absolute;
}

.dropdown {
  position: absolute;
  z-index: 999;
  left: 20px;
  top: 40px;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

<div class="container">
  <div class="helper">

    <div class="bs-example">
      <ul class="list-group ">
        <li class="list-group-item"></li>
      </ul>
    </div>

   <div class="dropdown">
     <button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
	    Menu
      </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>

  </div>
</div>