固定表格的标题列(CSS)

来源:
http://dev.csdn.net/develop/article/51/article/51/51967.shtm

http://dev.csdn.net/develop/article/51/51972.shtm

例1: 

<html>
<head>

<style>
table#headers {table-layout:fixed;background:#eea033;}
table#data {table-layout:relative;background:#eea033;}
td, th {vertical-align:top;background:#ffffff;}
</style>
<script language="javascript">
var rh;
var fr;
var ds;
var hs;

function syncScroll(e) {
     hs.scrollLeft = ds.scrollLeft;
}

function syncResize(e) {
  hs.style.width = ds.offsetWidth-(ds.offsetWidth - ds.clientWidth);

  for( i =0; i < rh.childNodes.length; i++ ) {
     rh.childNodes[i].width = fr.childNodes[i].offsetWidth;
  }
}

function init() {
  rh = document.getElementById("rh");
  fr = document.getElementById("fr");
  ds = document.getElementById("ds");
  hs = document.getElementById("hs");

  hs.style.top = ds.offsetTop;
  hs.style.left = ds.offsetLeft;
  hs.style.visibility = 'visible';
  ds.οnscrοll=syncScroll;
  window.οnresize=syncResize;
  syncResize();
}

</script>

</head>

<body οnlοad='init();'>
<div style="width:90%;overflow:hidden;z-index=3;visibility:hidden;position:absolute;" id='hs'>
<table cellpadding="0" cellspacing="1" id="headers">
<tr id='rh'>
<th>col1</th>
<th>col2</th>
<th>col3</th>
<th>col4</th>
<th>col5</th>
<th>col6</th>
<th>col7</th>
<th>col8</th>
<th>col9</th>
<th>col10</th>
</tr>
</table>
</div>
<div style="width:90%;height:200px;overflow:auto;z-index=2;" id='ds'>
<table cellpadding="0" cellspacing="1" width="100%" id="data">
<tr id='fr'>
<th>col1</th>
<th>col2</th>
<th>col3</th>
<th>col4</th>
<th>col5</th>
<th>col6</th>
<th>col7</th>
<th>col8</th>
<th>col9</th>
<th>col10</th>
</tr>
<tr>
<td>this is a width test-- please tell me it works</td>
<td>this is a width test-- please tell me it works</td>
<td>this is a width test-- please tell me it works</td>
<td>this is a width test-- please tell me it works</td>
<td>this is a width test-- please tell me it works</td>
<td>thisisawidthtest-- please tell me it works</td>
<td>this is a width test-- please tell me it works</td>
<td>this is a width test-- please tell me it works</td>
<td>this is a width test-- please tell me it works</td>
<td>this is a width test-- please tell me it works</td>
</tr>
<tr>
<td>this is a width test-- please tell me it works</td>
<td>this is a width test-- please tell me it works</td>
<td>this is a width test-- please tell me it works</td>
<td>this is a width test-- please tell me it works</td>
<td>this is a width test-- please tell me it works</td>
<td>this is a width test-- please tell me it works</td>
<td>this is a width test-- please tell me it works</td>
<td>this is a width test-- please tell me it works</td>
<td>this is a width test-- please tell me it works</td>
<td>this is a width test-- please tell me it works</td>
</tr>
<tr>
<td>this is a width test-- please tell me it works</td>
<td>this is a width test-- please tell me it works</td>
<td>this is a width test-- please tell me it works</td>
<td>this is a width test-- please tell me it works</td>
<td>this is a width test-- please tell me it works</td>
<td>this is a width test-- please tell me it works</td>
<td>this is a width test-- please tell me it works</td>
<td>this is a width test-- please tell me it works</td>
<td>this is a width test-- please tell me it works</td>
<td>this is a width test-- please tell me it works</td>
</tr>
</table>
</body>
</html>

例2:
<html>
 <body>
  <div style="overflow:auto;height:100px;">
     <table width="100%">
     <TR style="position:relative;top:expression(this.offsetParent.scrollTop);background:navy;color:white;">
     <TH nowrap>Header A</TH>
     <TH nowrap>Header B</TH>
     <TH nowrap>Header C</TH>
     </TR>
     <TR>
     <TD>A</TD>
     <TD>B</TD>
     <TD>C</TD>
     </TR>
     <TR>
     <TD>A</TD>
     <TD>B</TD>
     <TD>C</TD>
     </TR>
     <TR>
     <TD>A</TD>
     <TD>B</TD>
     <TD>C</TD>
     </TR>
     <TR>
     <TD>A</TD>
     <TD>B</TD>
     <TD>C</TD>
     </TR>
     <TR>
     <TD>A</TD>
     <TD>B</TD>
     <TD>C</TD>
     </TR>
     <TR>
     <TD>A</TD>
     <TD>B</TD>
     <TD>C</TD>
     </TR>
     <TR>
     <TD>A</TD>
     <TD>B</TD>
     <TD>C</TD>
     </TR>
     <TR>
     <TD>A</TD>
     <TD>B</TD>
     <TD>C</TD>
     </TR>
     <TR>
     <TD>A</TD>
     <TD>B</TD>
     <TD>C</TD>
     </TR>
     </table>
  </div>
 </body>
</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值