jsp怎么让表头不动但是可以上下左右滚动条,请给一个比较完整的例子
时间: 2024-03-14 09:46:22 浏览: 23
可以使用CSS中的position属性来实现这个效果。具体步骤如下:
1. 在HTML中创建一个表格,并且给表格头部和表格内容分别设置不同的id。
2. 使用CSS将表格头部固定在页面顶部,并且设置表格内容可滚动。
3. 使用JavaScript获取表格头部的宽度,并且将表格内容的左边距设置为表格头部的宽度,确保表格内容和表格头部对齐。
下面是一个简单的例子来实现这个效果:
HTML代码:
```html
<div id="table-wrapper">
<table id="table-header">
<thead>
<tr>
<th>表头1</th>
<th>表头2</th>
<th>表头3</th>
</tr>
</thead>
</table>
<div id="table-scroll">
<table id="table-content">
<tbody>
<tr>
<td>内容1</td>
<td>内容2</td>
<td>内容3</td>
</tr>
<tr>
<td>内容4</td>
<td>内容5</td>
<td>内容6</td>
</tr>
<tr>
<td>内容7</td>
<td>内容8</td>
<td>内容9</td>
</tr>
</tbody>
</table>
</div>
</div>
```
CSS代码:
```css
#table-wrapper {
position: relative;
}
#table-header {
position: fixed;
top: 0;
background-color: #fff;
z-index: 1;
}
#table-scroll {
overflow-x: auto;
overflow-y: visible;
margin-top: 20px;
}
#table-content {
margin-left: auto;
margin-right: auto;
border-collapse: collapse;
white-space: nowrap;
}
```
JavaScript代码:
```javascript
var tableHeader = document.getElementById('table-header');
var tableContent = document.getElementById('table-content');
tableContent.style.marginLeft = tableHeader.offsetWidth + 'px';
```
以上代码会让表格头部固定在页面顶部,而表格内容可以上下左右滚动。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)