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

mid*_*ack 3 html javascript css jquery 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>
Run Code Online (Sandbox Code Playgroud)

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;
}
Run Code Online (Sandbox Code Playgroud)

JQUERY

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

的jsfiddle

http://jsfiddle.net/bDL46/

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

nde*_*ore 5

您不需要添加"px".并且您应该在触发resize事件后检索窗口高度.

$(window).resize(function(){
    if($(window).height() < 600){
        $('.body').height($('.body').height()-20);
    }
});
Run Code Online (Sandbox Code Playgroud)

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