dynamic height div according to resizing window

谁都会走 提交于 2019-12-10 17:44:13

问题


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?


回答1:


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.




回答2:


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);
    }
});



回答3:


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/



来源:https://stackoverflow.com/questions/17032871/dynamic-height-div-according-to-resizing-window

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!