2016-08-23 90 views
0

我有一個模式,我在元素中有下拉列表。Dropdown忽略父級溢出

問題是因爲我有一個溢出設置,儘管看起來下拉菜單沒有出現在模態上。我明白,因爲我在父級上設置了overflow:auto。

有什麼辦法通過CSS,我可以忽略父母,並顯示「模態」上方的下拉?

你會在例子中看到,如果你滾動,紅色的內容是可見的。基於目前我的代碼的預期行爲是什麼。我需要做出什麼調整才能顯示在模式之上的下拉菜單?

嘗試使用z-index進行修復,我讀到SO上的某處以設置祖父母相對位置。

首選CSS解決方案。

謝謝!

.w-100 { 
 
    width: 100%; 
 
    height: 100%; 
 
} 
 

 
.h-100 { 
 
    width: 100%; 
 
    height: 100%: 
 
} 
 

 
.modal-overlay { 
 
    height: 100%; 
 
    width: 100%; 
 
    position: fixed; 
 
    top: 0; 
 
    left: 0; 
 
    display: block; 
 
    z-index: 65; 
 
    padding-top: 100px; 
 
    overflow: auto; 
 
    background-color: rgba(0,0,0,.6); 
 
} 
 

 
.modal-small { 
 
    max-width: 600px; 
 
    width: 40%; 
 
    margin: 0 auto; 
 
    float: none; 
 
    display: block; 
 
    position: relative; 
 
    background-color: #fff; 
 
    padding: 0; 
 
} 
 

 
.container { 
 
    min-height: 120px; 
 
    max-height: 400px; 
 
    overflow: auto; 
 
    padding: 15px; 
 
} 
 

 
.element-container { 
 
    height: 100px; 
 
    width: 100%; 
 
    display: inline-block; 
 
    padding: 10px; 
 
    margin-bottom: 10px; 
 
    position: relative; 
 
} 
 

 
.element-flex-container { 
 
    display: flex; 
 
    align-items: center; 
 
    height: 100%; 
 
    padding: 5px 15px; 
 
    border-radius: 2px; 
 
    border-bottom: 3px solid rgba(0,0,0,.1); 
 
    border-left: 1px solid rgba(0,0,0,.1); 
 
    border-right: 1px solid rgba(0,0,0,.1); 
 
    border-top: 1px solid rgba(0,0,0,.1); 
 
} 
 

 
.avatar { 
 
    height: 32px; 
 
    width: 32px; 
 
    border-radius: 100%; 
 
    margin-right: 10px; 
 
} 
 

 
.flex-1 { 
 
    flex: 1; 
 
} 
 

 
.dropdown-width { 
 
    text-align: right; 
 
    width: 100px; 
 
} 
 

 
.dropdown-container { 
 
    display: inline; 
 
    position: relative; 
 
} 
 

 
.toggle-dropdown { 
 
    color: #4caf50 
 
} 
 

 
.dropdown { 
 
    position: absolute; 
 
    border: 1px solid red; 
 
    left: auto; 
 
    right: 0; 
 
    width: 120px; 
 
    display: block; 
 
    background-color: #fff; 
 
    z-index: 10; 
 
    margin-bottom: 20px; 
 
    padding: 0; 
 
}
<div class="modal-overlay"> 
 
    <div class="modal-small"> 
 
    <div class="w-100 h-100"> <!-- this is since I inject an ui-view --> 
 
     <div class="w-100 h-100"> <!-- this is since I inject an ui-view --> 
 
     <div class="container"> 
 
      
 
      <!-- Repeat of elements --> 
 
      <div class="element-container"> 
 
      <div class="element-flex-container"> 
 
       <img src="http://images.freeimages.com/images/previews/7ab/chrysanthemum-3-1621562.jpg" class="avatar" /> 
 
       <div class="flex-1"> 
 
       Something here 
 
       </div> 
 
       <div class="dropdown-width"> 
 
       <div class="dropdown-container"> 
 
        <div class="toggle-dropdown"> 
 
        Toggle 
 
        </div> 
 
        <div class="dropdown"> 
 
        Something here 
 
        <br /> 
 
        Something else 
 
        <br /> 
 
        Something else 
 
        <br /> 
 
        Something else 
 
        <br /> 
 
        Something else 
 
        </div> 
 
       </div> 
 
       </div> 
 
      </div> 
 
      </div> 
 
     </div> 
 
     </div> 
 
    </div> 
 
    </div> 
 
</div>

回答

1

你需要讓下拉元素position: fixed,下拉容器positon: absolute,爲此家長position:relative工作。您可以使用top,right,left,bottom來調整容器元素的位置,但是您需要在固定元素上使用負邊距。

.w-100 { 
 
    width: 100%; 
 
    height: 100%; 
 
} 
 

 
.h-100 { 
 
    width: 100%; 
 
    height: 100%: 
 
} 
 

 
.modal-overlay { 
 
    height: 100%; 
 
    width: 100%; 
 
    position: fixed; 
 
    top: 0; 
 
    left: 0; 
 
    display: block; 
 
    z-index: 65; 
 
    padding-top: 100px; 
 
    overflow: auto; 
 
    background-color: rgba(0,0,0,.6); 
 
} 
 

 
.modal-small { 
 
    max-width: 600px; 
 
    width: 40%; 
 
    margin: 0 auto; 
 
    float: none; 
 
    display: block; 
 
    position: relative; 
 
    background-color: #fff; 
 
    padding: 0; 
 
} 
 

 
.container { 
 
    min-height: 120px; 
 
    max-height: 400px; 
 
    overflow: auto; 
 
    padding: 15px; 
 
} 
 

 
.element-container { 
 
    height: 100px; 
 
    width: 100%; 
 
    display: inline-block; 
 
    padding: 10px; 
 
    margin-bottom: 10px; 
 
    position: relative; 
 
} 
 

 
.element-flex-container { 
 
    display: flex; 
 
    align-items: center; 
 
    height: 100%; 
 
    padding: 5px 15px; 
 
    border-radius: 2px; 
 
    border-bottom: 3px solid rgba(0,0,0,.1); 
 
    border-left: 1px solid rgba(0,0,0,.1); 
 
    border-right: 1px solid rgba(0,0,0,.1); 
 
    border-top: 1px solid rgba(0,0,0,.1); 
 
} 
 

 
.avatar { 
 
    height: 32px; 
 
    width: 32px; 
 
    border-radius: 100%; 
 
    margin-right: 10px; 
 
} 
 

 
.flex-1 { 
 
    flex: 1; 
 
} 
 

 
.dropdown-width { 
 
    text-align: right; 
 
    width: 100px; 
 
} 
 

 
.dropdown-container { 
 
    display: inline; 
 
    position: absolute; 
 
    top: 0; 
 
    right:0; 
 
} 
 

 

 
.toggle-dropdown { 
 
    color: #4caf50 
 
} 
 

 
.dropdown { 
 
    position: fixed; 
 
    border: 1px solid red; 
 
    width: 120px; 
 
    display: block; 
 
    background-color: #fff; 
 
    z-index: 10; 
 
    margin-bottom: 20px; 
 
    padding: 0; 
 
    
 
}
<div class="modal-overlay"> 
 
    <div class="modal-small"> 
 
    <div class="w-100 h-100"> <!-- this is since I inject an ui-view --> 
 
     <div class="w-100 h-100"> <!-- this is since I inject an ui-view --> 
 
     <div class="container"> 
 
      
 
      <!-- Repeat of elements --> 
 
      <div class="element-container"> 
 
      <div class="element-flex-container"> 
 
       <img src="http://images.freeimages.com/images/previews/7ab/chrysanthemum-3-1621562.jpg" class="avatar" /> 
 
       <div class="flex-1"> 
 
       Something here 
 
       </div> 
 
       <div class="dropdown-width"> 
 
       <div class="dropdown-container"> 
 
        <div class="toggle-dropdown"> 
 
        Toggle 
 
        </div> 
 
        <div class="dropdown"> 
 
        Something here 
 
        <br /> 
 
        Something else 
 
        <br /> 
 
        Something else 
 
        <br /> 
 
        Something else 
 
        <br /> 
 
        Something else 
 
        </div> 
 
       </div> 
 
       </div> 
 
      </div> 
 
      </div> 
 
     </div> 
 
     </div> 
 
    </div> 
 
    </div> 
 
</div>

+0

謝謝!但是,如果有多個元素髮生溢出,則該解決方案不起作用,因爲它是在頁面加載時計算的? –

0

你需要使用的z-index 999,並在相對下拉CSS位置。 例子:

.w-100 { 
 
    width: 100%; 
 
    height: 100%; 
 
} 
 

 
.h-100 { 
 
    width: 100%; 
 
    height: 100%: 
 
} 
 

 
.modal-overlay { 
 
    height: 100%; 
 
    width: 100%; 
 
    position: fixed; 
 
    top: 0; 
 
    left: 0; 
 
    display: block; 
 
    z-index: 65; 
 
    padding-top: 100px; 
 
    overflow: auto; 
 
    background-color: rgba(0,0,0,.6); 
 
} 
 

 
.modal-small { 
 
    max-width: 600px; 
 
    width: 40%; 
 
    margin: 0 auto; 
 
    float: none; 
 
    display: block; 
 
    position: relative; 
 
    background-color: #fff; 
 
    padding: 0; 
 
} 
 

 
.container { 
 
    min-height: 120px; 
 
    max-height: 400px; 
 
    overflow: auto; 
 
    padding: 15px; 
 
} 
 

 
.element-container { 
 
    height: 100px; 
 
    width: 100%; 
 
    display: inline-block; 
 
    padding: 10px; 
 
    margin-bottom: 10px; 
 
    position: relative; 
 
} 
 

 
.element-flex-container { 
 
    display: flex; 
 
    align-items: center; 
 
    height: 100%; 
 
    padding: 5px 15px; 
 
    border-radius: 2px; 
 
    border-bottom: 3px solid rgba(0,0,0,.1); 
 
    border-left: 1px solid rgba(0,0,0,.1); 
 
    border-right: 1px solid rgba(0,0,0,.1); 
 
    border-top: 1px solid rgba(0,0,0,.1); 
 
} 
 

 
.avatar { 
 
    height: 32px; 
 
    width: 32px; 
 
    border-radius: 100%; 
 
    margin-right: 10px; 
 
} 
 

 
.flex-1 { 
 
    flex: 1; 
 
} 
 

 
.dropdown-width { 
 
    text-align: right; 
 
    width: 100px; 
 
} 
 

 
.dropdown-container { 
 
    display: inline; 
 
    position: relative; 
 
} 
 

 
.toggle-dropdown { 
 
    color: #4caf50 
 
} 
 

 
.dropdown { 
 
    position: relative; 
 
    z-index: 9999; 
 
    border: 1px solid red; 
 
    left: auto; 
 
    right: 0; 
 
    width: 120px; 
 
    display: block; 
 
    background-color: #fff; 
 
    z-index: 10; 
 
    margin-bottom: 20px; 
 
    padding: 0; 
 
}
<div class="modal-overlay"> 
 
    <div class="modal-small"> 
 
    <div class="w-100 h-100"> <!-- this is since I inject an ui-view --> 
 
     <div class="w-100 h-100"> <!-- this is since I inject an ui-view --> 
 
     <div class="container"> 
 
      
 
      <!-- Repeat of elements --> 
 
      <div class="element-container"> 
 
      <div class="element-flex-container"> 
 
       <img src="http://images.freeimages.com/images/previews/7ab/chrysanthemum-3-1621562.jpg" class="avatar" /> 
 
       <div class="flex-1"> 
 
       Something here 
 
       </div> 
 
       <div class="dropdown-width"> 
 
       <div class="dropdown-container"> 
 
        <div class="toggle-dropdown"> 
 
        Toggle 
 
        </div> 
 
        <div class="dropdown"> 
 
        Something here 
 
        <br /> 
 
        Something else 
 
        <br /> 
 
        Something else 
 
        <br /> 
 
        Something else 
 
        <br /> 
 
        Something else 
 
        </div> 
 
       </div> 
 
       </div> 
 
      </div> 
 
      </div> 
 
     </div> 
 
     </div> 
 
    </div> 
 
    </div> 
 
</div>

+0

你需要使用的z-index 999,和相對位置:d –

+0

請做[編輯]你的答案,包括你改變了,爲什麼。這將使它成爲更好的答案。有關更多信息,請參閱[答案]。 –

+0

感謝Mike,編輯。 –