2017-10-17 44 views
-2

如果位於父元素(帶position: relativeposition: absolute)具有position: absolute一個子元素的定位的父內,那麼孩子元素將絕對定位。CSS子與位置是:固定不尊重父佈局

position: sticky子元素的工作方式 - 子元素會被放置粘粘的定位父之內。

但是子女元素position: fixed而不是在定位的父級中佔據固定位置。

說明例:

.panel { 
 
display: inline-block; 
 
position: relative; 
 
width: 240px; 
 
height: 180px; 
 
margin-right: 6px; 
 
overflow-x: hidden; 
 
overflow-y: auto; 
 
} 
 

 
header, footer { 
 
display: block; 
 
position: fixed; 
 
left: 0; 
 
width: 240px; 
 
height: 24px; 
 
background-color: rgb(255, 0, 0); 
 
} 
 

 
header { 
 
top: 0; 
 
} 
 

 
footer { 
 
bottom: 0; 
 
} 
 

 
.panel + .panel header, 
 
.panel + .panel footer { 
 
position: sticky; 
 
} 
 

 
header code { 
 
padding-left: 6px; 
 
font-weight: bold; 
 
color: rgb(255, 255, 255); 
 
}
<div class="panel"> 
 
<header><code>position: fixed</code></header> 
 

 
<p>Alice was beginning to get very tired of sitting by her sister on the bank, and of having nothing to do: once or twice she had peeped into the book her sister was reading, but it had no pictures or conversations in it, `and what is the use of a book,' thought Alice `without pictures or conversation?'</p> 
 

 
<p>So she was considering in her own mind (as well as she could, for the hot day made her feel very sleepy and stupid), whether the pleasure of making a daisy-chain would be worth the trouble of getting up and picking the daisies, when suddenly a White Rabbit with pink eyes ran close by her.</p> 
 

 
<p>There was nothing so very remarkable in that; nor did Alice think it so very much out of the way to hear the Rabbit say to itself, `Oh dear! Oh dear! I shall be late!' (when she thought it over afterwards, it occurred to her that she ought to have wondered at this, but at the time it all seemed quite natural); but when the Rabbit actually took a watch out of its waistcoat-pocket, and looked at it, and then hurried on, Alice started to her feet, for it flashed across her mind that she had never before seen a rabbit with either a waistcoat-pocket, or a watch to take out of it, and burning with curiosity, she ran across the field after it, and fortunately was just in time to see it pop down a large rabbit-hole under the hedge.</p> 
 

 
<footer></footer> 
 
</div> 
 

 
<div class="panel"> 
 
<header><code>position: sticky</code></header> 
 

 
<p>Alice was beginning to get very tired of sitting by her sister on the bank, and of having nothing to do: once or twice she had peeped into the book her sister was reading, but it had no pictures or conversations in it, `and what is the use of a book,' thought Alice `without pictures or conversation?'</p> 
 

 
<p>So she was considering in her own mind (as well as she could, for the hot day made her feel very sleepy and stupid), whether the pleasure of making a daisy-chain would be worth the trouble of getting up and picking the daisies, when suddenly a White Rabbit with pink eyes ran close by her.</p> 
 

 
<p>There was nothing so very remarkable in that; nor did Alice think it so very much out of the way to hear the Rabbit say to itself, `Oh dear! Oh dear! I shall be late!' (when she thought it over afterwards, it occurred to her that she ought to have wondered at this, but at the time it all seemed quite natural); but when the Rabbit actually took a watch out of its waistcoat-pocket, and looked at it, and then hurried on, Alice started to her feet, for it flashed across her mind that she had never before seen a rabbit with either a waistcoat-pocket, or a watch to take out of it, and burning with curiosity, she ran across the field after it, and fortunately was just in time to see it pop down a large rabbit-hole under the hedge.</p> 
 

 
<footer></footer> 
 
</div>

是JavaScript的解決這個唯一的方法?

還是有CSS的方法使子元素與position: fixed,拿起自己的定位的父元素中的一個固定的位置?

+2

*我告訴你,孩子(用'位置:fixed')不得不尊重他們的父母(佈局)早在我的一天*; ) – UncaughtTypeError

+0

如果只。事實是,這些孩子從來沒有對父母有過任何尊重。我想知道 - 在這個新時代的「立場:粘性」 - 如果上述孩子們現在有一個他們以前缺乏的新發現的方面......但似乎沒有。 – Rounin

+0

該死的千禧一代 - 我們必須指望下一代。 – UncaughtTypeError

回答

0

這是一個JavaScript方法,它使頁眉和頁腳能夠採用相對於可滾動父項的人造固定位置

工作實例:

var panel = document.getElementsByClassName('panel')[0]; 
 
var panelHeader = panel.getElementsByTagName('header')[0]; 
 
var panelFooter = panel.getElementsByTagName('footer')[0]; 
 

 
function fixElements() { 
 
    var panelHeight = panel.clientHeight; 
 
    var panelFooterHeight = panelFooter.clientHeight; 
 

 
    panelHeader.style.top = panel.scrollTop + 'px'; 
 
    panelFooter.style.top = panel.scrollTop + panelHeight - panelFooterHeight + 'px'; 
 
} 
 

 
panel.addEventListener('scroll', fixElements, false);
.panel { 
 
display: inline-block; 
 
position: relative; 
 
width: 240px; 
 
height: 180px; 
 
overflow-x: hidden; 
 
overflow-y: auto; 
 
} 
 

 
header, footer { 
 
display: block; 
 
position: absolute; 
 
left: 0; 
 
width: 240px; 
 
height: 24px; 
 
background-color: rgb(255, 0, 0); 
 
} 
 

 
header { 
 
top: 0; 
 
} 
 

 
footer { 
 
top: 156px; 
 
} 
 

 
header code { 
 
padding-left: 6px; 
 
font-weight: bold; 
 
color: rgb(255, 255, 255); 
 
} 
 

 
.panel p:first-of-type { 
 
padding-top: 12px; 
 
} 
 

 
.panel p:last-of-type { 
 
padding-bottom: 12px; 
 
}
<div class="panel"> 
 
<header><code>position: faux-fixed</code></header> 
 

 
<p>Alice was beginning to get very tired of sitting by her sister on the bank, and of having nothing to do: once or twice she had peeped into the book her sister was reading, but it had no pictures or conversations in it, `and what is the use of a book,' thought Alice `without pictures or conversation?'</p> 
 

 
<p>So she was considering in her own mind (as well as she could, for the hot day made her feel very sleepy and stupid), whether the pleasure of making a daisy-chain would be worth the trouble of getting up and picking the daisies, when suddenly a White Rabbit with pink eyes ran close by her.</p> 
 

 
<p>There was nothing so very remarkable in that; nor did Alice think it so very much out of the way to hear the Rabbit say to itself, `Oh dear! Oh dear! I shall be late!' (when she thought it over afterwards, it occurred to her that she ought to have wondered at this, but at the time it all seemed quite natural); but when the Rabbit actually took a watch out of its waistcoat-pocket, and looked at it, and then hurried on, Alice started to her feet, for it flashed across her mind that she had never before seen a rabbit with either a waistcoat-pocket, or a watch to take out of it, and burning with curiosity, she ran across the field after it, and fortunately was just in time to see it pop down a large rabbit-hole under the hedge.</p> 
 

 
<footer></footer> 
 
</div>

2

作爲每specs,一個固定的位置總是相對於視口。所以這是一個功能。

+0

謝謝。我知道這曾經是這種情況,但我不知道它還沒有被更新。所以...是否仍然沒有CSS方法來「固定」可滾動定位父元素中的子元素? – Rounin