1.为了固定表头我们需要先把表格的head和tbody切分到两个table里: <table class="table-head"></table>和 <table class="table-body"></table>里。
2.将table-body放在一个table-body-box的div里设置y轴方向可以滚动,x轴方向不可滚动,这样表内容就能在自己的区域实现垂直方向的滚动了。
3.将table-head设置为absolute
4.将table-head和table-body-box的div放入一个table-box的div中,设置横向x轴可以滚动,y轴不可滚动。
这样两个表就会在x方向滚动,y轴不滚动,表头就垂直方向固定在垂直方向,横向滚动在table-box出现滚动条的时候,内部的表格头和表格内容因为没有设置为fixed,所以会被滚动。
表头和表内容的横向方向滚动能力(其实是父级overflow-x:scroll样式赋予的)。表内容的垂直方向滚动是通过表内容外套的table-body-box设置overflow-y:scroll来实现的。
5.代码如下:
<style>
.table-box {
height: 100%;
width: 100%;
overflow-y: hidden;
overflow-x: scroll;
-webkit-overflow-scrolling: touch;
position: relative;
}
.table-head {
background-color: #F9FAFE;
border-top: 1px solid #e8e8e8;
border-bottom: 1px solid #e8e8e8;
color: #777;
table-layout: fixed;
position: absolute;
top:0;
}
.table-head th {
border-collapse: collapse;
height: 40px;
}
.table-body-box {
table-layout: fixed;
-webkit-overflow-scrolling: touch;
overflow-x: hidden;
overflow-y: scroll;
height: 100%;
box-sizing: border-box;
padding-top: 50px;
}
.table-body{
table-layout: fixed;
width: 100%;
background-color: white;
text-align: center;
}
.table-body tr:nth-child(even) {
background-color: #F9FAFE;
}
</style>
html如下
<div style="height:500px">
<div class="table-box">
<table class="table-head" style="width: 900px">
<colgroup>
<col width="150">
<col width="100">
<col>
<col>
<col>
<col>
<col>
</colgroup>
<thead>
<tr>
<th>Company</th>
<th>Provision</th>
<th>Following in Point</th>
<th>Customer Grade</th>
<th>Allocated Time</th>
<th>Follow up Person</th>
<th>Department</th>
</tr>
</thead>
</table>
<div class="table-body-box" style="width: 900px">
<table class="table-body">
<colgroup>
<col width="150">
<col width="100">
<col>
<col>
<col>
<col>
<col>
</colgroup>
<tbody>
<tr>
<td>Shanghai Yinghuang</td>
<td>FOB</td>
<td>Dumped Cabinet</td>
<td>Important</td>
<td>2017-10-28</td>
<td>Nathan</td>
<td>Sales Department</td>
</tr>
<tr>
<td>Shanghai Yinghuang</td>
<td>FOB</td>
<td>Dumped Cabinet</td>
<td>Important</td>
<td>2017-10-28</td>
<td>Nathan</td>
<td>Sales Department</td>
</tr>
<tr>
<td>Shanghai Yinghuang</td>
<td>FOB</td>
<td>Dumped Cabinet</td>
<td>Important</td>
<td>2017-10-28</td>
<td>Nathan</td>
<td>Sales Department</td>
</tr>
<tr>
<td>Shanghai Yinghuang</td>
<td>FOB</td>
<td>Dumped Cabinet</td>
<td>Important</td>
<td>2017-10-28</td>
<td>Nathan</td>
<td>Sales Department</td>
</tr>
<tr>
<td>Shanghai Yinghuang</td>
<td>FOB</td>
<td>Dumped Cabinet</td>
<td>Important</td>
<td>2017-10-28</td>
<td>Nathan</td>
<td>Sales Department</td>
</tr>
<tr>
<td>Shanghai Yinghuang</td>
<td>FOB</td>
<td>Dumped Cabinet</td>
<td>Important</td>
<td>2017-10-28</td>
<td>Nathan</td>
<td>Sales Department</td>
</tr>
<tr>
<td>Shanghai Yinghuang</td>
<td>FOB</td>
<td>Dumped Cabinet</td>
<td>Important</td>
<td>2017-10-28</td>
<td>Nathan</td>
<td>Sales Department</td>
</tr>
<tr>
<td>Shanghai Yinghuang</td>
<td>FOB</td>
<td>Dumped Cabinet</td>
<td>Important</td>
<td>2017-10-28</td>
<td>Nathan</td>
<td>Sales Department</td>
</tr>
<tr>
<td>Shanghai Yinghuang</td>
<td>FOB</td>
<td>Dumped Cabinet</td>
<td>Important</td>
<td>2017-10-28</td>
<td>Nathan</td>
<td>Sales Department</td>
</tr>
<tr>
<td>Shanghai Yinghuang</td>
<td>FOB</td>
<td>Dumped Cabinet</td>
<td>Important</td>
<td>2017-10-28</td>
<td>Nathan</td>
<td>Sales Department</td>
</tr>
<tr>
<td>Shanghai Yinghuang</td>
<td>FOB</td>
<td>Dumped Cabinet</td>
<td>Important</td>
<td>2017-10-28</td>
<td>Nathan</td>
<td>Sales Department</td>
</tr>
<tr>
<td>Shanghai Yinghuang</td>
<td>FOB</td>
<td>Dumped Cabinet</td>
<td>Important</td>
<td>2017-10-28</td>
<td>Nathan</td>
<td>Sales Department</td>
</tr>
<tr>
<td>Shanghai Yinghuang</td>
<td>FOB</td>
<td>Dumped Cabinet</td>
<td>Important</td>
<td>2017-10-28</td>
<td>Nathan</td>
<td>Sales Department</td>
</tr>
<tr>
<td>Shanghai Yinghuang</td>
<td>FOB</td>
<td>Dumped Cabinet</td>
<td>Important</td>
<td>2017-10-28</td>
<td>Nathan</td>
<td>Sales Department</td>
</tr>
<tr>
<td>Shanghai Yinghuang</td>
<td>FOB</td>
<td>Dumped Cabinet</td>
<td>Important</td>
<td>2017-10-28</td>
<td>Nathan</td>
<td>Sales Department</td>
</tr>
<tr>
<td>Shanghai Yinghuang</td>
<td>FOB</td>
<td>Dumped Cabinet</td>
<td>Important</td>
<td>2017-10-28</td>
<td>Nathan</td>
<td>Sales Department</td>
</tr>
<tr>
<td>Shanghai Yinghuang</td>
<td>FOB</td>
<td>Dumped Cabinet</td>
<td>Important</td>
<td>2017-10-28</td>
<td>Nathan</td>
<td>Sales Department</td>
</tr>
<tr>
<td>Shanghai Yinghuang</td>
<td>FOB</td>
<td>Dumped Cabinet</td>
<td>Important</td>
<td>2017-10-28</td>
<td>Nathan</td>
<td>Sales Department</td>
</tr>
<tr>
<td>Shanghai Yinghuang</td>
<td>FOB</td>
<td>Dumped Cabinet</td>
<td>Important</td>
<td>2017-10-28</td>
<td>Nathan</td>
<td>Sales Department</td>
</tr>
<tr>
<td>Shanghai Yinghuang</td>
<td>FOB</td>
<td>Dumped Cabinet</td>
<td>Important</td>
<td>2017-10-28</td>
<td>Nathan</td>
<td>Sales Department</td>
</tr>
<tr>
<td>Shanghai Yinghuang</td>
<td>FOB</td>
<td>Dumped Cabinet</td>
<td>Important</td>
<td>2017-10-28</td>
<td>Nathan</td>
<td>Sales Department</td>
</tr>
<tr>
<td>Shanghai Yinghuang</td>
<td>FOB</td>
<td>Dumped Cabinet</td>
<td>Important</td>
<td>2017-10-28</td>
<td>Nathan</td>
<td>Sales Department</td>
</tr>
<tr>
<td>Shanghai Yinghuang</td>
<td>FOB</td>
<td>Dumped Cabinet</td>
<td>Important</td>
<td>2017-10-28</td>
<td>Nathan</td>
<td>Sales Department</td>
</tr>
<tr>
<td>Shanghai Yinghuang</td>
<td>FOB</td>
<td>Dumped Cabinet</td>
<td>Important</td>
<td>2017-10-28</td>
<td>Nathan</td>
<td>Sales Department</td>
</tr>
<tr>
<td>Shanghai Yinghuang</td>
<td>FOB</td>
<td>Dumped Cabinet</td>
<td>Important</td>
<td>2017-10-28</td>
<td>Nathan</td>
<td>Sales Department</td>
</tr>
<tr>
<td>Shanghai Yinghuang</td>
<td>FOB</td>
<td>Dumped Cabinet</td>
<td>Important</td>
<td>2017-10-28</td>
<td>Nathan</td>
<td>Sales Department</td>
</tr>
<tr>
<td>Shanghai Yinghuang</td>
<td>FOB</td>
<td>Dumped Cabinet</td>
<td>Important</td>
<td>2017-10-28</td>
<td>Nathan</td>
<td>Sales Department</td>
</tr>
<tr>
<td>Shanghai Yinghuang</td>
<td>FOB</td>
<td>Dumped Cabinet</td>
<td>Important</td>
<td>2017-10-28</td>
<td>Nathan</td>
<td>Sales Department</td>
</tr>
<tr>
<td>Shanghai Yinghuang</td>
<td>FOB</td>
<td>Dumped Cabinet</td>
<td>Important</td>
<td>2017-10-28</td>
<td>Nathan</td>
<td>Sales Department</td>
</tr>
<tr>
<td>Shanghai Yinghuang</td>
<td>FOB</td>
<td>Dumped Cabinet</td>
<td>Important</td>
<td>2017-10-28</td>
<td>Nathan</td>
<td>Sales Department</td>
</tr>
<tr>
<td>Shanghai Yinghuang</td>
<td>FOB</td>
<td>Dumped Cabinet</td>
<td>Important</td>
<td>2017-10-28</td>
<td>Nathan</td>
<td>Sales Department</td>
</tr>
<tr>
<td>Shanghai Yinghuang</td>
<td>FOB</td>
<td>Dumped Cabinet</td>
<td>Important</td>
<td>2017-10-28</td>
<td>Nathan</td>
<td>Sales Department</td>
</tr>
<tr>
<td>Shanghai Yinghuang</td>
<td>FOB</td>
<td>Dumped Cabinet</td>
<td>Important</td>
<td>2017-10-28</td>
<td>Nathan</td>
<td>Sales Department</td>
</tr>
<tr>
<td>Shanghai Yinghuang</td>
<td>FOB</td>
<td>Dumped Cabinet</td>
<td>Important</td>
<td>2017-10-28</td>
<td>Nathan</td>
<td>Sales Department</td>
</tr>
<tr>
<td>Shanghai Yinghuang</td>
<td>FOB</td>
<td>Dumped Cabinet</td>
<td>Important</td>
<td>2017-10-28</td>
<td>Nathan</td>
<td>Sales Department</td>
</tr>
<tr>
<td>Shanghai Yinghuang</td>
<td>FOB</td>
<td>Dumped Cabinet</td>
<td>Important</td>
<td>2017-10-28</td>
<td>Nathan</td>
<td>Sales Department</td>
</tr>
<tr>
<td>Shanghai Yinghuang</td>
<td>FOB</td>
<td>Dumped Cabinet</td>
<td>Important</td>
<td>2017-10-28</td>
<td>Nathan</td>
<td>Sales Department</td>
</tr>
<tr>
<td>Shanghai Yinghuang</td>
<td>FOB</td>
<td>Dumped Cabinet</td>
<td>Important</td>
<td>2017-10-28</td>
<td>Nathan</td>
<td>Sales Department</td>
</tr>
<tr>
<td>Shanghai Yinghuang</td>
<td>FOB</td>
<td>Dumped Cabinet</td>
<td>Important</td>
<td>2017-10-28</td>
<td>Nathan</td>
<td>Sales Department</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
6.可以结合flex做到自适应表头和表格内容
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。