gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-31 22:46:38 25 4
gpt4 key购买 nike

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/

我想根据调整窗口的大小来增加和减少。如果 window height<600 减少 .body div 高度或者如果 window height>600 增加。但我做不到。我的 jquery 代码无法运行,我该怎么办?

最佳答案

您不需要添加“px”。并且您应该在触发调整大小事件后检索窗口高度。

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

不过你的逻辑有问题。使用此代码,在触发足够多的调整大小事件后,“.body”的高度将缩小为 0。

关于javascript - 根据调整窗口大小的动态高度 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17032871/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com