首页 > 开发 > CSS > 正文

css 固定表头 拖动滚动条时对应表头所对应的列 ie6测试通过

2024-07-11 08:35:41
字体:
来源:转载
供稿:网友

项目中设计的报表table设计的列数相对过多,当拖动下方的滚动条时无法对应表头所对应的列,因此在网上搜索了好一段日子,最后在网上找到了一些参考资料,然后总结归纳出兼容行列合并的固定表头demo。
多浏览器没有做太多测试,在ie6中已测试通过。

功能期待了很久今天终于找到解决的方式,总结到javaeye中方便日后查阅。实现方式可能还有更好的,希望有大牛路过,给我留点更好的实现例子作日后交流之用。
以下是相关的css


<style type=”text/css”>
<!–
body,table, td, a {font:9pt;}
/*重点:固定行头样式*/
.scrollRowThead{position: relative; left: expression(this.parentElement.parentElement.parentElement.parentElement.scrollLeft);z-index:0;}
/*重点:固定表头样式*/
.scrollColThead {position: relative;top: expression(this.parentElement.parentElement.parentElement.scrollTop);z-index:2;}
/*行列交叉的地方*/
.scrollCR { z-index:3;}
/*div外框*/
.scrollDiv {height:200px;clear: both; border: 1px solid #EEEEEE;OVERFLOW: scroll;width: 320px; }
/*行头居中*/
.scrollColThead td,.scrollColThead th{ text-align: center ;}
/*行头列头背景*/
.scrollRowThead,.scrollColThead td,.scrollColThead th{background-color:EEEEEE;}
/*表格的线*/
.scrolltable{border-bottom:1px solid #CCCCCC; border-right:1px solid #CCCCCC; }
/*单元格的线等*/
.scrolltable td,.scrollTable th{border-left: 1px solid #CCCCCC; border-top: 1px solid #CCCCCC; padding: 5px; }
.scrollTable thead th{background-color:#EEEEEE;font-weight:bold;position:relative;}
–>
</style>

以下是HTML


<h1>利用CSS代码让Table产生固定表头</h1>
<div id=”scrollDiv” class=”scrollDiv” >
<table border=”0″ cellpadding=”3″ cellspacing=”0″ width=”500″ class=”scrollTable”>
<thead>
<tr class=”scrollColThead” >
<th class=”scrollRowThead scrollCR” >&nbsp;</th>
<th colspan=”2″>列头</th>
<th colspan=”2″>列头</th>
<th rowspan=”2″>列头</th>
</tr>
<tr class=”scrollColThead” >
<th class=”scrollRowThead scrollCR” >h1</th>
<th >h2</th>
<th >h3</th>
<th >h4</th>
<th >h5</th>
</tr>
</thead>
<tr>
<td class=”scrollRowThead” >
<input type=”checkbox” name=”checkbox” value=”checkbox”>
a</td>
<td>单元格2</td>
<td>单元格3</td>
<td>单元格4</td>

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表