动态高度div根据调整大小窗口

时间:2013-06-10 21:06:20

标签: javascript jquery html css height

HTML

<div class="header">Header</div>
 <div class="body">
    <table class="body-table">
        <tr>
            <td>Cell</td>
            <td>Cell</td>
            <td>Cell</td>
        </tr>
        <tr>
            <td>Cell</td>
            <td>Cell</td>
           <td>Cell</td>
       </tr>
       <tr>
           <td>Cell</td>
           <td>Cell</td>
           <td>Cell</td>
       </tr>
       <tr>
           <td>Cell</td>
           <td>Cell</td>
           <td>Cell</td>
        </tr>
        <tr>
           <td>Cell</td>
           <td>Cell</td>
           <td>Cell</td>
        </tr>
        <tr>
           <td>Cell</td>
           <td>Cell</td>
           <td>Cell</td>
        </tr>
        <tr>
           <td>Cell</td>
           <td>Cell</td>
           <td>Cell</td>
        </tr>
    </table>    
 </div>
<div class="footer">
    <button>Submit</button>
</div>

CSS

.header{
    height:50px;
    background-color:#ccc;
    width:500px;
}
.body{
    width:500px;
    max-height:600px;
    text-align:center;
    background-color:#ddd;
    overflow:auto;
}
.body .body-table{
    border-collapse:collapse;
    text-align:center;
    margin:0 auto;
    width:100%;
 }
.footer{
    width:500px;
    height:50px;
    background-color:#eee;
}

JQUERY

var windowHeight = $(window).height();
$(window).resize(function(){
    if(windowHeight<600+'px'){
        $('.body').height($('.body').height()-20+'px');
    }
});

的jsfiddle

http://jsfiddle.net/bDL46/

我想根据调整大小窗口增加和减少。如果窗口高度<600,则减小.body div height或窗口高度> 600增加。但我不能这样做。我的jquery代码不起作用我怎么能这样做?

3 个答案:

答案 0 :(得分:5)

您无需添加“px”。并且您应该在触发resize事件后检索窗口高度。

$(window).resize(function(){
    if($(window).height() < 600){
        $('.body').height($('.body').height()-20);
    }
});

但你的逻辑是有缺陷的。使用此代码,在触发足够的调整大小事件后,'.body'的高度将缩小为0。

答案 1 :(得分:1)

试试这个。您可以为该范围指定范围和特定高度

$(window).resize(function(){
    var small_height = 200;
    var big_height = 400;

    if($(window).height() < 600)
    {
        $('.body').height(small_height);
    }
    else // The window height is bigger than or equal to 600
    {
        $('.body').height(big_height);
    }
});

答案 2 :(得分:0)

试图在没有javascript的情况下执行此操作并添加了一个.container部门:

HTML:

<div class="container">
    <div class="header">Header</div>
    <div class="body">Body...</div>
    <div class="footer">Footer</div>
</div>

CSS:

html, body, .container, div {
    margin:0;
    padding:0; /*normalize*/
}
html, body {
    height:100%;
    overflow-y:hidden;
}
.container {
    position:relative;
    height:100%;
    overflow:auto;
    min-width:500px;
}
.header, .body, .footer {
    position:absolute;
    width:500px;
    left:50%;
    margin-left:-250px; /* center divs */
}
.header {
    top:0;
    height:50px;
    background-color:#ccc;
}
.body {
    top:50px;
    bottom:50px;
    text-align:center;
    background-color:#ddd;
    overflow:auto;
}
.footer {
    bottom:0;
    height:50px;
    background-color:#eee;
}
@media all and (max-height:200px) {
    body {
        overflow-y:scroll;
    }
    .container {
        min-height:200px;
    }
    /*  http://www.w3.org/TR/css3-mediaqueries/  */
}

以下是完整的最终结果:http://jsfiddle.net/bDL46/2/