2010-04-28 67 views
46

好吧,所以我還沒有找到一個問題的答案,所以我決定做我自己的。CSS 100%高度,然後滾動DIV而不是頁面

我想創建一個100%的流體佈局,這在技術上已經完成了。 http://stickystudios.ca/sandbox/stickyplanner/layout/index2.php

但是,我現在要做的,是使頁面100%在HEIGHT中......但我不希望頁面滾動我希望內部DIV滾動。

所以我相信簡而言之,我希望它能檢測到視口屏幕的高度,100%,然後IF內容比屏幕更長,滾動特定的DIV,而不是頁面。

我希望這是有道理的。

在此先感謝。 賈斯汀

+0

可能會更好過,詢問在的DocType:http://doctype.com/ – teabot 2010-04-28 16:52:45

回答

50
<html> 
    <body style="overflow:hidden;"> 
    <div style="overflow:auto; position:absolute; top: 0; left:0; right:0; bottom:0"> 
    </div> 
    </body> 
</html> 

應該這樣做了一個簡單的例子

我相信這會爲你的情況下工作

<html> 
    <body style="overflow:hidden;"> 
     <div id="header" style="overflow:hidden; position:absolute; top:0; left:0; height:50px;"></div> 
     <div id="leftNav" style="overflow:auto; position:absolute; top:50px; left:0; right:200px; bottom:50px;"></div> 
     <div id="mainContent" style="overflow:auto; position:absolute; top:50px; left: 200px; right:0; bottom:50px;"></div> 
     <div id="footer" style="overflow:hidden; position:absolute; bottom:0; left:0; height:50px"></div> 
    </body> 
</html> 

這個例子給你一個靜態的頁眉和頁腳,並允許導航器和內容區域可滾動。

+0

約翰,你的第二個例子的工作真棒!非常感謝,我一直在爲這個問題煩惱。欣賞它。 – Justin 2010-04-28 17:24:04

+0

沒問題。很高興爲您解答 – 2010-04-28 17:40:49

+0

由於某種原因,在IE8下工作不正常,也許是JQuery CSS – 2011-04-08 22:51:27

6

化妝overflow:auto的DIV

2

overflow:auto; DIV風格 您應該知道div的大小應該增加,以便它可以顯示卷軸。 如果你增加頁面的大小(它應該與body =「overflow:hidden;」一致) 它不起作用。

+1

請編輯這個答案讓它稍微多點可讀。然後把它放在名爲'從真正的CSS忍者古怪的垃圾祕密建議'的書頂部 – Zlatko 2013-08-02 06:57:47

8

這是一種不同的方式與所有ABS面板要做到這一點,它會失敗在IE6,但我可以提供對IE6的解決方法CSS如果這是一個要求:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<title>Fluid Layout</title> 
<meta http-equiv="content-type" content="application/xhtml+xml; charset=UTF-8" /> 
<style rel="stylesheet" type="text/css"> 
    body { background-color:black; margin:0px; padding:0px; } 
    .pageBox { position:absolute; top:20px; left:20px; right:20px; bottom:20px; min-width:200px} 
    .headerBox { position:absolute; width:100%; height:50px; background-color:#333; } 
    .contentBox { position:absolute; width:100%; top:52px; bottom:32px; background-color:blue; } 
    .footerBox { position:absolute; width:100%; height:30px; background-color:#ccc; bottom:0px; }   
    .contentBoxLeft { position:absolute; width:20%; height:100%; background-color:#b6b6b6; } 
    .contentBoxRight { position:absolute; width:80%; left:20%; height:100%; background-color:white; }  
    .contentBoxLeft, 
    .contentBoxRight { overflow:auto; overflow-x:hidden; } 
</style> 
</head> 
<body>&nbsp; 
    <div class="pageBox"> 
     <div class="headerBox">Header</div> 
     <div class="contentBox"> 
      <div class="contentBoxLeft">ContentLeft asdf asdf adsf assf</div> 
      <div class="contentBoxRight">ContentRight asdf asdfa dasf asdf asdfd asfasd fdasfasdf dasfsad fdasfds<br /><br />asdfsad ff asdf asdfasd</div> 
     </div> 
     <div class="footerBox">Footer</div> 
    </div> 
</body> 
</html> 
+1

大衛,真棒芽,你的第一個實際使用我給出的信息,感謝你顯示實施。謝謝你,非常有用。 – Justin 2010-04-28 17:26:22

0

如果你不這樣做想要使用position:absolute(因爲如果邊距需要與所有零不同,則會打印出來),那麼可以使用一點JavaScript來完成。

設置你的身體和DIV像這樣,讓DIV滾動:

<body style='overflow:hidden'> 
    <div id=scrollablediv style='overflow-y:auto;height:100%'> 
    Scrollable content goes here 
    </div> 
</body> 

這種技術依賴於具有一組高度的股利,以及我們需要的JavaScript。

創建一個簡單的函數來重置滾動DIV

function calculateDivHeight(){ 
    $("#scrollablediv").height(window.innerHeight); 
} 

的高度,然後呼籲兩國頁面加載和調整本功能。

// Gets called when the page loads 
calculateDivHeight(); 

// Bind calculate height function to window resize 
$(window).resize(function() { 
    calculateDivHeight(); 
} 
0

你可以試試這個:

<!DOCTYPE> 
 
<html xmlns="http://www.w3.org/1999/xhtml"> 
 
<head> 
 
<meta http-equiv="content-type" content="application/xhtml+xml; charset=UTF-8" /> 
 
<style rel="stylesheet" type="text/css"> 
 
    .modal{width:300px;border:1px solid red;overflow: auto;opacity: 0.5;z-index:2;} 
 
    .bg{background:-webkit-linear-gradient(top,red,green,yellow);width:1000px;height:2000px;top:0;left:0;} 
 
    .btn{position:fixed;top:100px;left:100px;} 
 
</style> 
 
</head> 
 
<body style='padding:0px;margin:0px;'> 
 
\t <div class='bg' style='position:static'></div> 
 
\t <div class='modal' style='display:none'></div> 
 
\t <button class='btn'>toggle </button> 
 
</body> 
 
<script> 
 
\t var str='',count=200; 
 
\t while(count--){ 
 
\t \t str+=count+'<br>'; 
 
\t } 
 
\t var modal=document.querySelector('.modal'),bg=document.querySelector('.bg'), 
 
\t btn=document.querySelector('.btn'),body=document.querySelector('body'); 
 
\t modal.innerHTML=str; 
 
\t btn.onclick=function(){ 
 
\t \t if(bg.style.position=='fixed'){ 
 
\t \t \t bg.style.position='static'; 
 
\t \t \t window.scrollTo(0,bg.storeTop); 
 
\t \t }else{ 
 
\t \t \t let top=bg.storeTop=body.scrollTop; 
 
\t \t \t bg.style.position='fixed'; 
 
\t \t \t bg.style.top=(0-top)+'px'; 
 
\t \t } 
 
\t \t modal.style.display=modal.style.display=='none'?'block':'none'; 
 
\t } 
 
</script> 
 
</html>