簡體   English   中英

滾動具有可滾動內容的可滾動子元素的父 div

[英]Scroll parent div with scrollable child that has content to scroll

我有一個可滾動的父子 div。 父 div 具有固定的高度。 子 div 具有動態高度,並且可以根據其內容滾動。

當子元素的內容很長時,如何實現無縫控制? 問題是,如果父滾動條位於最頂部,則子元素的滾動條無法進一步向下滾動,除非鼠標移動一點以將焦點放在父級滾動條上以繼續滾動。

代碼如下。

 .outer { width: 400px; background-color: antiquewhite; height: 400px; overflow-y: scroll; } h2 { margin-bottom: 500px; }.inner { height: 800px; width: 80%; overflow-y: scroll; background-color: aquamarine; }
 <div class="outer"> <div class="inner"> <h2>sddssd</h2> <h2>sddssd</h2> <h2>sddssd</h2> <h2>sddssd</h2> </div> </div>

一種解決方案(如果適合您的用例)是使用 CSS 執行以下操作。

  .outer {
        width: 400px;
        background-color: antiquewhite;
        height: auto;
        overflow-y: hidden;
    }

    h2 {
        margin-bottom: 500px;
    }

    .inner {
       max-height: 400px;
       width: 100%;
       overflow-y: auto; 
       background-color: aquamarine;    
    }

注意外部元素的變化。

height: auto;
overflow-y: hidden;

以及內在元素的變化

       max-height: 400px;
       width: 100%;
       overflow-y: auto;

結果附上:

不過,我希望看到更多解決方案。

 .outer { width: 400px; background-color: antiquewhite; height: auto; overflow-y: hidden; } h2 { margin-bottom: 500px; }.inner { max-height: 400px; width: 100%; overflow-y: auto; background-color: aquamarine; }
 <div class="outer"> <div class="inner"> <h2>sddssd</h2> <h2>sddssd</h2> <h2>sddssd</h2> <h2>sddssd</h2> </div> </div>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM