2017-03-05 31 views
0

在一個web應用程序,我有一個頭,並希望使中繼續顯示其作爲用戶滾動,我以前position: fixed;來實現這一點,但身體現在從頁面的頂部,開始反對從標題下面開始。頭變成固定的,當用戶滾動

原:

enter image description here

現在:

enter image description here

我想要做的就是給頭部固定的位置,當用戶開始滾動給它一個固定位置,但根據頭部的高度保持標題下的那個區域(我目前可以只添加具有固定的高度一個div在那裏,但如果T他頭的變化,這意味着我必須改變,以及和感覺就像一個翻倍)

小提琴:https://jsfiddle.net/zmp7fbrx/3/

+0

好多久你打算更改標題的高度? –

+0

@WilliamRobinson不經常,但我不想離開的驚喜在未來開發。 –

回答

0

你可以用柔性佈局做到這一點,那麼.header可以不管的高度,並在頁面的其餘部分將自動適應。

套裝body作爲柔性父母,並在新的元素包裹.body.footer和元素設置爲flex-grow: 1overflow: scroll,這樣它會填滿窗口的其餘部分,而這個元素將溢出滾動,並.header會保持放置(模仿position: fixed)。

body { 
 
    display: flex; 
 
    flex-direction: column; 
 
    height: 100vh; 
 
    margin: 0; 
 
} 
 
.header { 
 
    background: black; 
 
    color: white; 
 
} 
 
.body { 
 
    min-height: 200vh; /* you can remove. just for demo purposes */ 
 
    flex-grow: 1; 
 
} 
 
main { 
 
    overflow-y: scroll; 
 
}
<div class="header">My menu</div> 
 
<main> 
 
    <div class="body">body</div> 
 
    <div class="footer">footer</div> 
 
</main>

+0

聽起來很棒,但我試圖將它應用到當前的小提琴,它不起作用:https://jsfiddle.net/zmp7fbrx/5/ –

+1

@NaguibIhab再次閱讀答案。 *「將.body和.footer包裝到一個新元素中,並將該元素設置爲flex-grow:1並溢出:scroll」* –

+0

好吧,那有效。謝謝 –

1

你可以只設置bodypadding-top到頭部的高度,並添加top: 0到頭部的CSS。

body { 
 
    padding-top: 50px; 
 
} 
 

 
.header{ 
 
    position: fixed; 
 
    top: 0; 
 
} 
 
.header, .body, .footer { 
 
    width:100%; 
 
    height:200px; 
 
    border:1px solid blue; 
 
} 
 

 
.header {height: 50px;} 
 
.body {text-align:center;} 
 
.footer {} 
 

 
.center-horiz{width:200px;height:100px;border:1px solid #666; display: inline-block;}
<div class="header">My menu</div> 
 
<div class="body">some text2 
 
    <br> 
 
    <div class="center-horiz">div centered horizontally</div> 
 
</div> 
 
<div class="footer">some text3</div>

或者你可以給.body頭的高度的margin-top

.header{ 
 
    position: fixed; 
 
    top: 0; 
 
} 
 
.header, .body, .footer { 
 
    width:100%; 
 
    height:200px; 
 
    border:1px solid blue; 
 
} 
 

 
.header {height: 50px;} 
 
.body { 
 
    margin-top: 50px; 
 
} 
 
.body {text-align:center;} 
 
.footer {} 
 

 
.center-horiz{width:200px;height:100px;border:1px solid #666; display: inline-block;}
<div class="header">My menu</div> 
 
<div class="body">some text2 
 
    <br> 
 
    <div class="center-horiz">div centered horizontally</div> 
 
</div> 
 
<div class="footer">some text3</div>

使用JavaScript得到DOM頭高度加載並設置.body元素的margin-top。 (這也適用於頭部的動態高度)

document.addEventListener('DOMContentLoaded', function(e) { 
 
    var headerElement = document.querySelector('.header'); 
 
    var bodyElement = document.querySelector('.body'); 
 
    
 
    bodyElement.style.marginTop = headerElement.getBoundingClientRect().height + 'px'; 
 
})
.header{ 
 
    position: fixed; 
 
    top: 0; 
 
} 
 
.header, .body, .footer { 
 
    width:100%; 
 
    height:200px; 
 
    border:1px solid blue; 
 
} 
 

 
.header {height: 50px;} 
 
.body {text-align:center;} 
 
.footer {} 
 

 
.center-horiz{width:200px;height:100px;border:1px solid #666; display: inline-block;}
<div class="header">My menu</div> 
 
<div class="body">some text2 
 
    <br> 
 
    <div class="center-horiz">div centered horizontally</div> 
 
</div> 
 
<div class="footer">some text3</div>
注:如果頭的和動態的高度上的窗口大小調整變化,你必須添加一個 resize監聽器,設置在調整大小保證金。

+0

謝謝你的回答cyrix。如果標題的高度得到調整,靜態高度也需要調整。你能想出一種方法來做到這一點,而不是在兩個地方的高度? –

+1

您可以使用'sass'之類的東西,並將標題高度存儲在一個變量中,或者使用JavaScript來獲取標題高度並將其應用到body作爲填充或將.body作爲邊距,我將爲javascript添加一個示例部分。 – cyrix

+0

請注意,這聽起來像是最佳解決方案:) –