代码之家  ›  专栏  ›  技术社区  ›  Justin

CSS 100%高度,然后滚动DIV not page

  •  49
  • Justin  · 技术社区  · 16 年前

    好吧,我还没找到一个有答案的问题,所以我决定自己做一个。

    我试图创造一个100%的流体布局,技术上我已经做到了。 http://stickystudios.ca/sandbox/stickyplanner/layout/index2.php

    但是,我现在想做的是,使页面100%的高度。。。但是我不希望页面滚动,我希望内部DIV滚动。

    我希望这有道理。

    提前谢谢。 贾斯廷

    6 回复  |  直到 8 年前
        1
  •  65
  •   Kerem Baydoğan David Zorychta    8 年前
    <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>
    

    此示例将为您提供一个静态页眉和页脚,并允许导航器和内容区域可滚动。

        2
  •  9
  •   David    16 年前

    这是一个不同的方式来做这一切的abs面板,它将失败的IE6,但我可以提供解决方案CSS的IE6如果这是一个要求:

    <!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>
    
        3
  •  6
  •   Mahesh Velaga    16 年前

    overflow:auto 对于div

        4
  •  2
  •   Gabriel Guimarães    16 年前

    overflow:auto; 论DIV风格 您应该知道div的大小应该增加,以便它可以显示其中的滚动条。 如果您增加页面的大小(应该使用style=“overflow:hidden;”在身体上) 这行不通。

        5
  •  1
  •   Vincent    10 年前

    如果你不想用position:absolute (因为如果页边距需要不同于全零,那么打印输出会比较混乱)然后可以使用一点JavaScript来完成。

    像这样设置身体和div以允许div滚动:

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

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

    然后在页面加载和调整大小时调用此函数。

    // Gets called when the page loads
    calculateDivHeight();
    
    // Bind calculate height function to window resize
    $(window).resize(function () {
      calculateDivHeight();
    }
    
        6
  •  0
  •   詹特维    9 年前

    <!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;'>
    	<div class='bg' style='position:static'></div>
    	<div class='modal' style='display:none'></div>
    	<button class='btn'>toggle </button>
    </body>
    <script>
    	var str='',count=200;
    	while(count--){
    		str+=count+'<br>';
    	}
    	var modal=document.querySelector('.modal'),bg=document.querySelector('.bg'),
    	btn=document.querySelector('.btn'),body=document.querySelector('body');
    	modal.innerHTML=str;
    	btn.onclick=function(){
    		if(bg.style.position=='fixed'){
    			bg.style.position='static';
    			window.scrollTo(0,bg.storeTop);
    		}else{
    			let top=bg.storeTop=body.scrollTop;
    			bg.style.position='fixed';
    			bg.style.top=(0-top)+'px';
    		}
    		modal.style.display=modal.style.display=='none'?'block':'none';
    	}
    </script>
    </html>