dynamic height div according to resizing window

2019-07-07 03:02发布

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/

I want to increase and decrease according to resizing window. if window height<600 decrease .body div height or if window height>600 increase. But I can't do it. My jquery code not work how can I do this?

3条回答
疯言疯语
2楼-- · 2019-07-07 03:36

You don't need to add "px". And you should be retrieving the window height after the resize event is triggered.

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

Your logic is flawed though. With this code, the height of the '.body' will be shrunk down to 0 after enough resize events are triggered.

查看更多
Ridiculous、
3楼-- · 2019-07-07 03:37

Try this one. You can assign a range and a specific height for that range

$(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);
    }
});
查看更多
唯我独甜
4楼-- · 2019-07-07 03:39

Tried to do it without javascript and added one additional .container division:

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/  */
}

and here is the full final result: http://jsfiddle.net/bDL46/2/

查看更多
登录 后发表回答