2017-09-05 149 views
1

我有一個絕對定位div與兩個孩子 - 一個絕對定位的div和靜態div將在父母的內部滾動。它看起來像這樣:絕對定位的div div滾動內絕對定位的父

<div class='frame'> 
    <div class='absolute-contents'>This should stay put.</div> 
    <div class='static-contents'>This should scroll under it.</div> 
</div> 

而這裏的CSS:

.frame { 
    position: absolute; 
    top: 40px; 
    left: 40px; 
    right: 40px; 
    bottom: 40px; 
    overflow-y: scroll; 
} 

.absolute-contents { 
    position: absolute; 
    top: 40px; 
    left: 40px; 
    right: 40px; 
    bottom: 40px; 
    z-index: 9999; 
    opacity: .9; 
    padding: 40px; 
} 

.static-contents { 
    margin: 24px auto; 
    width: 400px; 
    height: 3000px; 
    padding: 40px; 
} 

我不得不受限於母公司的邊緣絕對的孩子,所以爲何仍滾動,我怎樣才能使它保持放?

例子:https://codepen.io/anon/pen/wqZxXG

+0

你試過'的位置是:固定;'了嗎? –

+0

是的。但是我想讓它在父窗口內而不是整個窗口內修復。 –

回答

3

我通過把我想在一個絕對定位的div與overflow-y: scroll滾動,像這樣的元素解析造型如下:

.frame { 
    background-color: blue; 
    position: absolute; 
    top: 40px; 
    right: 40px; 
    left: 40px; 
    bottom: 40px; 
    overflow-y: hidden; 
} 

.scroll-me { 
    background-color: orange; 
    position: absolute; 
    top: 40px; 
    right: 40px; 
    left: 40px; 
    bottom: 40px; 
    overflow-y: scroll; 
} 

.fix-me { 
    position: absolute; 
    z-index: 999; 
    top: 40px; 
    left: 40px; 
    right: 40px; 
    height: 56px; 
    background-color: purple; 
} 

.long-content { 
    width: 480px; 
    background-color: yellow; 
    height: 4000px; 
    margin: 20px auto; 
} 

筆在這裏:https://codepen.io/dustinlocke/pen/vJMzpK

+0

看起來你已經用額外的標記排序,類似於我的答案中鏈接的問題。不錯的工作! – TylerH

4

你應該調整你的孩子div來使用position: fixed,如果你不希望它移動。 position: absolute只是告訴一個div,它的最初的位置應該是絕對確定的。查看我的回答here瞭解更多有關position: fixed的信息以及與您相似的場景。

設置.frame div爲position: relative(或其父母.frame)以使其生效。這會將position: fixed孩子設置在.frame的父母position: relative內。

您將需要調整定位數量(頂部,底部,左側,右側)以考慮不同的堆疊環境。

事情是這樣的:

<div class='frame'> 
    <div class='fix-me'></div> 
    <div class='scroll-me'> 
    <div class='long-content'></div> 
    </div> 
</div> 

https://codepen.io/anon/pen/brJxVW

body { 
 
    width: 100vw; 
 
} 
 

 
.frame { 
 
    background-color: green; 
 
    position: relative; 
 
    width: calc(100vw - 80px); 
 
    margin: 0 auto; 
 
    top: 40px; 
 
    bottom: 40px; 
 
    overflow-y: scroll; 
 
} 
 

 
.absolute-contents { 
 
    background-color: yellow; 
 
    position: fixed; 
 
    top: 40px; 
 
    left: 40px; 
 
    right: 40px; 
 
    bottom: 40px; 
 
    z-index: 9999; 
 
    opacity: .9; 
 
    margin: 40px; 
 
} 
 

 
.big-contents { 
 
    margin: 24px auto; 
 
    width: 400px; 
 
    height: 3000px; 
 
    background-color: white; 
 
    padding: 40px; 
 
}
<div class='frame'> 
 
    <div class='absolute-contents'>This should stay fixed in the green frame. Why is it scrolling?</div> 
 
    <div class='big-contents'>This should scroll under it.</div> 
 
</div>

+0

你的筆修復了整個窗口內的子div,而不僅僅是'.frame'元素並滾動整個身體。我只想滾動綠色'.frame'中的元素,並修改'.absolute-content'內的綠色'.frame' –

+0

@DustinRichardLocke從您的演示中.frame是文檔中唯一的東西。我稍後回家時會進行更新;在演示中添加一些周圍的現實元素也會對我有所幫助。 – TylerH