2015-10-13 34 views
0

我有一個頁面,在頁面底部有一個div,單擊時顯示另一個div,正好位於底部div上方。如何在窗口大小調整時避免絕對底部定位div與其他重疊區域

我想避免當窗口調整大小時,頁腳div與頁面上方的內容div重疊。

所涉及的div的高度不應該改變。

是僅有CSS的解決方案嗎?

我創建了一個的jsfiddle here

CSS

html, body { 
    height: 100%; 
    width: 100%; 
    padding: 0; 
    margin: 0; 
} 
#container { 
    height: 100%; 
    width: 100%; 
    background-color: white; 
    border: solid #aaa 1px; 
    padding: 4px; 
} 

#content { 
    height: 300px; 
    border: solid blue 1px; 
} 

#footer-content { 
    height: 100px; 
    border: solid red 1px; 
    display:none; 
} 

#footer-footer { 
    cursor: pointer; 
    height: 20px; 
    border: solid cyan 1px; 
} 

#footer.expanded #footer-content { 
    display:block; 
} 

#footer { 
    position: absolute; 
    bottom: 0px; 
    width: 100%; 
} 

HTML

<div id="container"> 
    <div id="content">content 
    </div> 
    <div id="footer"> 
    <div id="footer-content">footer-content</div> 
    <div id="footer-footer">Click me to expand</div> 
    </div> 
</div> 

JS

$("#footer-footer").on("click", function (evt) { 
    $("#footer").toggleClass("expanded"); 
}); 
+0

我認爲刪除位置 –

+0

您希望有一個100px的空白空間,頁腳顯示或者「點擊我展開」滑動? –

+0

@HubertGrzeskowiak - 不確定是否誠實,但我想我寧願有一個100px的空白空間。 – user5325596

回答

1

只需添加position: relative到#container的。這樣,頁腳的絕對位置就是指容器。

http://jsfiddle.net/5bkznxud/5/

你可能會注意到,在上面的例子中總是有右側的滾動條。這是因爲#container上的邊框和填充。下面是與輪廓(邊界沒有計算出的寬度),並且沒有任何填充的例子:

http://jsfiddle.net/5bkznxud/6/

TIP:始終使用輪廓代替邊框用於阻擋佈局或使用box-sizing: border-box。這會導致框的尺寸也爲邊框計算。否則,寬度爲100%和邊框的框將比您想要的寬度稍寬。

+0

看起來不錯,謝謝。我想出了以下「解決方案」 - http://jsfiddle.net/5bkznxud/8/ - 使用包裝div。 – user5325596

0

可以使用calc()來解決。 在這種情況下,您可以創建一個獲取footer-contentfooter-footer - >.height()的高度的jQuery函數。沒有jQuery,我不認爲這是可能的。
下面是一個例子:

html, body { 
    height: 100%; 
    width: 100%; 
    padding: 0; 
    margin: 0; 
} 
#container { 
    height: 100%; 
    width: 100%; 
    background-color: white; 
    border: solid #aaa 1px; 
    padding: 4px; 
    min-height: 420px; 

} 

#content { 
    height:calc(100% - 135px); 
    border: solid blue 1px; 
} 

#footer-content { 
    height: 100px; 
    border: solid red 1px; 
    display:none; 
} 

#footer-footer { 
    cursor: pointer; 
    height: 20px; 
    border: solid cyan 1px; 
} 

#footer.expanded #footer-content { 
    display:block; 
} 

#footer { 
    position: absolute; 
    bottom: 0px; 
    width: 100%; 
} 

http://jsfiddle.net/dokmngv0/

calc()功能瀏覽器支持:http://caniuse.com/#feat=calc

+0

當我調整你的小提琴的大小時,我仍然可以使頁腳重疊內容。我也會編輯我的問題,以清楚說明'content' div不應該調整大小。 – user5325596

+0

是的,這是事實,因爲您應該手動獲取頁腳元素的高度,以確保其具有正確的值。我隨意放了135px –

相關問題