查看完整版本: CSS布局中自适应高度的解决方法

习斋 2007-3-4 05:33

CSS布局中自适应高度的解决方法

这是div+css布局中经常会碰到的基础问题,个人觉得采用背景图填充的方法,还是比较简单和切实可行的。


[code]        body{
                background: #EDEDED;
                font-size: 70%;
                font-family: Arial, Helvetica, sans-serif;
                line-height: 130%;
                color: #666666;
                margin: 0;
                padding: 0;
                text-align: center;
        }
                #layout{
                width: 760px;/*--for ie5.x--*/
                w\idth: 740px;/*--other--*/
                margin: 10px auto;
                border: solid 10px #999999;
                border-bottom: solid 9px #999999;
                /*--底部要加个1px的DIV来清除浮动,所以把
                底边框设为9px--*/
                background: url(bg.gif) #FFFFFF repeat-y left;
                /*--背景填充,解决左右栏高度不一致--*/
                text-align: left;
        }

        #sidel{

                float: left;
                width: 190px;
        }

        #sider{       
                float: right;
                width: 540px;
        }

        pre{
                padding: 10px;
                margin: 0;
        }               

        .clear{
                background: #999999;
                clear: both;
                height: 1px;
                overflow: hidden;
        }[/code]

习斋 2007-3-4 05:42

脚本控制三行三列自适应高度DIV布局

这个例子是用JS脚本控制并列DIV的高度,通常在DIV布局中,自适应高度一直是比较头疼的问题,一般大都采用背景图、外套DIV、右栏覆盖左栏......来解决。现在加了脚本后,简单多了,假如有三个水平并列的DIV,fbox、mbox、sbox,只要在<body>标签中写入:onload="P7_equalCols('fbox','mbox','sbox')",测试条件:ie5.x、ie6.0、FF1.03、NS7.2、opera8.01。

JS代码:版权归原作者 ,仅供学习研究.

/*
------------------------------------------------
PVII Equal CSS Columns scripts
Copyright (c) 2005 Project Seven Development
www.projectseven.com
Version: 1.5.0
------------------------------------------------
*/
function P7_colH(){ //v1.5 by PVII-www.projectseven.com
var i,oh,hh,h=0,dA=document.p7eqc,an=document.p7eqa;if(dA&&dA.length){
for(i=0;i<dA.length;i++){dA[i].style.height='auto';}for(i=0;i<dA.length;i++){
oh=dA[i].offsetHeight;h=(oh>h)?oh:h;}for(i=0;i<dA.length;i++){if(an){
dA[i].style.height=h+'px';}else{P7_eqA(dA[i].id,dA[i].offsetHeight,h);}}if(an){
for(i=0;i<dA.length;i++){hh=dA[i].offsetHeight;if(hh>h){
dA[i].style.height=(h-(hh-h))+'px';}}}else{document.p7eqa=1;}
document.p7eqth=document.body.offsetHeight;
document.p7eqtw=document.body.offsetWidth;}
}
function P7_eqT(){ //v1.5 by PVII-www.projectseven.com
if(document.p7eqth!=document.body.offsetHeight||document.p7eqtw! =document.body.offsetWidth){
P7_colH();}
}
function P7_equalCols(){ //v1.5 by PVII-www.projectseven.com
if(document.getElementById){document.p7eqc=new Array;for(i=0;i<arguments.length;i++){
document.p7eqc[i]=document.getElementById(arguments[i]);} setInterval("P7_eqT()",10);}
}
function P7_eqA(el,h,ht){ //v1.5 by PVII-www.projectseven.com
var sp=10,inc=10,nh=h,g=document.getElementById(el),oh=g.offsetHeight,ch=parseInt(g.style.height) ;
ch=(ch)?ch:h;var ad=oh-ch,adT=ht-ad;nh+=inc;nh=(nh>adT)?adT:nh;g.style.height=nh+'px';
oh=g.offsetHeight;if(oh>ht){nh=(ht-(oh-ht));g.style.height=nh+'px';}
if(nh<adT){setTimeout("P7_eqA('"+el+"',"+nh+","+ht+") ",sp);}
}
页: [1]
查看完整版本: CSS布局中自适应高度的解决方法