XY 滚动 HTML 表,带有垂直固定的标题
XY Scroll HTML table with vertically fixed header
我想将滚动添加到我的html表中,无论是水平还是垂直。但是只允许 tbody 滚动,并且在垂直滚动 tbody 时应该固定 thead,在水平滚动 tbody 时应该滚动/调整。我知道我必须将溢出 css 属性应用于 tbody,然后在它之后,使用 jquery 调整带有偏移量的头宽度。如果有人知道解决方案,那么答案将不胜感激。这是我的代码。
网页部件
<table>
<thead>
<tr>
<th>Column 1</th>
<th>Column 2</th>
<th>Column 3</th>
<th>Column 4</th>
<th>Column 5</th>
</tr>
</thead>
<tbody>
<tr>
<td>Row 1</td>
<td>Row 1</td>
<td>Row 1</td>
<td>Row 1</td>
<td>Row 1</td>
</tr>
<tr>
<td>Row 2</td>
<td>Row 2</td>
<td>Row 2</td>
<td>Row 2</td>
<td>Row 2</td>
</tr>
<tr>
<td>Row 3</td>
<td>Row 3</td>
<td>Row 3</td>
<td>Row 3</td>
<td>Row 3</td>
</tr>
<tr>
<td>Row 4</td>
<td>Row 4</td>
<td>Row 4</td>
<td>Row 4</td>
<td>Row 4</td>
</tr>
<tr>
<td>Row 5</td>
<td>Row 5</td>
<td>Row 5</td>
<td>Row 5</td>
<td>Row 5</td>
</tr>
<tr>
<td>Row 6</td>
<td>Row 6</td>
<td>Row 6</td>
<td>Row 6</td>
<td>Row 6</td>
</tr>
<tr>
<td>Row 7</td>
<td>Row 7</td>
<td>Row 7</td>
<td>Row 7</td>
<td>Row 7</td>
</tr>
<tr>
<td>Row 8</td>
<td>Row 8</td>
<td>Row 8</td>
<td>Row 8</td>
<td>Row 8</td>
</tr>
<tr>
<td>Row 9</td>
<td>Row 9</td>
<td>Row 9</td>
<td>Row 9</td>
<td>Row 9</td>
</tr>
<tr>
<td>Row 10</td>
<td>Row 10</td>
<td>Row 10</td>
<td>Row 10</td>
<td>Row 10</td>
</tr>
</tbody>
</table>
.CSS
html {
font-family: verdana;
font-size: 10pt;
line-height: 25px;
}
table {
border-collapse: collapse;
width: 300px;
overflow-x: scroll;
display: block;
}
thead {
background-color: #EFEFEF;
}
thead, tbody {
display: block;
}
tbody {
overflow-y: scroll;
overflow-x: hidden;
height: 140px;
}
td, th {
min-width: 100px;
height: 25px;
border: dashed 1px lightblue;
}
.JS
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0-beta1/jquery.js"></script>
<script type="text/javascript">
$('table').on('scroll', function () {
$("table > *").width($("table").width() + $("table").scrollLeft());
});
</script>
http://codepen.io/EasonWang01/pen/bEjRvj 试试。
html {
font-family: verdana;
font-size: 10pt;
line-height: 25px;
}
table {
border-collapse: collapse;
width: 540px;
display: block;
}
thead {
background-color: #EFEFEF;
}
thead, tbody {
display: block;
}
tbody {
overflow-y: scroll;
height: 140px;
}
td, th {
min-width: 100px;
height: 25px;
border: dashed 1px lightblue;
}
完成。因为启动jquery函数是我的错误。通过将js代码放入$(function () {});
,它被解决了。
相关文章:
- CSS-如何定位内容数据标题
- 在PHP中使用javascript更改页面标题'if'
- Brightcove获取/显示HTML中的当前视频标题和描述
- React组件-设置页面标题
- jQuery动态更改标题
- HighCharts长标题文本在某些元素上重叠
- FlexSlider:在右侧垂直获取标题
- 动态垂直表&水平滚动粘性标题和固定列
- 具有固定标题以及水平和垂直滚动的表格,不给 td 或 th 提供宽度
- 如何修复表的标题并允许数据垂直移动,同时允许标题和数据垂直移动
- XY 滚动 HTML 表,带有垂直固定的标题
- 根据垂直和水平标题突出显示表格单元格
- 具有固定标题的水平和垂直滚动内容
- 在图像下方垂直和水平居中显示标题
- 列标题垂直交叉
- 具有固定标题和第一列的垂直表行
- 如何使用html5画布创建垂直表格标题文本
- 我如何使与标题和第一列固定滚动垂直和水平的表
- 垂直滑动菜单:防止标题在悬停时向左移动
- 嵌入式表格垂直滚动带固定标题