HTML 表格、跟踪标题的水平滚动和垂直滚动 - 文本宽度问题
好吧,如果您今天认识我之前的一些帖子,首先我们深表歉意。我写了一段 HTML,这似乎是做到这一点的唯一方法。
- 当您移动主体时,随后的水平滚动使标题与主体对齐。
- 垂直滚动仍然可以让广告保持在视野中。
我能做到这一点的唯一方法是使用两个表。这很有效,请随意尝试。但是,我对列的宽度是由列内文本的大小而不是我设置的宽度属性确定的方式有问题。有人可以帮忙吗?
我只是希望列的宽度表示列的宽度
编辑:宽度为1200的原因是因为在溢出div内部实际大小是8x 150px
<script type='text/javascript'>
function MatchScroll(SourceID, TargetID, DoIfMoz) {
if (DoIfMoz || navigator.userAgent.indexOf("Firefox") == -1) document.getElementById(TargetID).scrollLeft = document.getElementById(SourceID).scrollLeft;
}
</script>
<div id='HeaderTable' style='width:883px;overflow:auto;overflow-y:hidden;overflow-x:hidden;'>
<table border='1' id='HeaderTable' style='width:1200px;float:left;table-layout:fixed;'>
<thead style='text-align:left;'>
<tr style='display:block;margin-left:1px;'>
<th width='150' style='width:150px;'>t</th>
<th width='150' style='width:150px;'>t</th>
<th width='150' style='width:150px;'>tt</th>
<th width='150' style='width:150px;'>t</th>
<th width='150' style='width:150px;'>t</th>
<th width='150' style='width:150px;'>ttttt</th>
<th width='150' style='width:150px;'>ttttt</th>
<th width='150' style='width:150px;'>tt</th>
</tr>
</thead>
</table>
</div>
<div id='DataTable' style='height:300px;float:left;overflow:auto;overflow-x:auto;overflow-y:scroll;width:900px;' onmouseup="MatchScroll('DataTable','HeaderTable', true);" onmousemove="MatchScroll('DataTable','HeaderTable', false);" >
<table border='1' style='width:1200px;float:left;table-layout:fixed;'>
<tbody id='ClearDetails'>
<tr id='Row0' style='color:black;height:auto;display:block;'>
<td style='width:150px;'>1</td>
<td style='width:150px;'>1</td>
<td style='width:150px;'>1</td>
<td style='width:150px;'>1</td>
<td style='width:150px;'>1111111111</td>
<td style='width:150px;'>1</td>
<td style='width:150px;'>1</td>
<td style='width:150px;'>1</td>
</tr>
</tbody>
</table>
</div>
Ok firstly apologies if you recognise some of my earlier posts today. I have a piece of HTML i wrote, which seemed the only way to do this.
- Horizontal scroll which follows keeps the thead aligned with tbody as you move tbody.
- Vertical scroll which still keeps the thead in view.
The only way i could do this was using two tables. This works and feel free to try it. However, i have a problem with the way in which the widths of columns are being determined by the size of the text inside them and not the width property i set. Can anyone help?
I simply want the width of a column to mean the width of a column
EDIT: the reason for the 1200 widths is because inside the overflow div the actual size is 8x 150px
<script type='text/javascript'>
function MatchScroll(SourceID, TargetID, DoIfMoz) {
if (DoIfMoz || navigator.userAgent.indexOf("Firefox") == -1) document.getElementById(TargetID).scrollLeft = document.getElementById(SourceID).scrollLeft;
}
</script>
<div id='HeaderTable' style='width:883px;overflow:auto;overflow-y:hidden;overflow-x:hidden;'>
<table border='1' id='HeaderTable' style='width:1200px;float:left;table-layout:fixed;'>
<thead style='text-align:left;'>
<tr style='display:block;margin-left:1px;'>
<th width='150' style='width:150px;'>t</th>
<th width='150' style='width:150px;'>t</th>
<th width='150' style='width:150px;'>tt</th>
<th width='150' style='width:150px;'>t</th>
<th width='150' style='width:150px;'>t</th>
<th width='150' style='width:150px;'>ttttt</th>
<th width='150' style='width:150px;'>ttttt</th>
<th width='150' style='width:150px;'>tt</th>
</tr>
</thead>
</table>
</div>
<div id='DataTable' style='height:300px;float:left;overflow:auto;overflow-x:auto;overflow-y:scroll;width:900px;' onmouseup="MatchScroll('DataTable','HeaderTable', true);" onmousemove="MatchScroll('DataTable','HeaderTable', false);" >
<table border='1' style='width:1200px;float:left;table-layout:fixed;'>
<tbody id='ClearDetails'>
<tr id='Row0' style='color:black;height:auto;display:block;'>
<td style='width:150px;'>1</td>
<td style='width:150px;'>1</td>
<td style='width:150px;'>1</td>
<td style='width:150px;'>1</td>
<td style='width:150px;'>1111111111</td>
<td style='width:150px;'>1</td>
<td style='width:150px;'>1</td>
<td style='width:150px;'>1</td>
</tr>
</tbody>
</table>
</div>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不知道你的整个项目有多大,但是有一个 jquery dataTable 插件可以解决这个问题。
它有很多用于处理表格的选项。如果您只想实现一些选项,我建议您使用“jquery.min”文件。
I don't know how big your whole project is, but there is a jquery dataTable plugin to solve this problem.
It has a lot of options for working with tables. I suggest that you use the "jquery.min" file if you just want to implement a few options.
在 CSS 中,
width: x
指定最小宽度,因此浏览器可能会扩展列以适应表格正文中超出该宽度的文本。In CSS
width: x
specifies the minimum width, so the browser might expand the column to fit text in the table body that exceeds it.