2010-04-28 95 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>