长表滚动,但阻止窗口滚动

Long table scroll, but prevent window scroll

本文关键字:滚动 窗口      更新时间:2023-09-26

我有一个表格,上面有很多div

<div></div>...<div></div>
<div id="tablecontent">
<table>...</table>
</div>

我只想让我的表格在屏幕上滚动(表格底部在窗口底部(。所以我需要为#tablecontent设置高度和scroll=auto.为了设置高度,我将其计算为:

var pos=$("#tablecontent").position().top;
var heigth=$( window ).height()-pos;
$("#tablecontent").css("height",heigth+'px');

但这不能正常工作。它比我需要的要大。你能帮我计算正确的价值吗?

CSS的div上,您需要设置overflow: scroll,当内容大于容器时,固定高度应在div上创建一个滚动条。