数据展示时,表头的固定,可以有更好的可读性。
一、实现方式:
1、定义2个表格,一个absolute固定
| 序号 | 股票名称 | ...
|---|---|
2、表1
var th_new=$("#table1 tr").eq(0).clone();
$("#fixed-table1").append(th_new);
3、resize()方法,实时获取表1各列
function trResize(){
$("#fixed-table1 th").each(function(){
var num=$(this).index();
var th_width=$("#table1 th").eq(num).width();
$(this).css("width",th_width+"px");
});
}
4、页面过小时,表格滚动带表头滚动
$(".table1-wapper").scroll(function(){
var scroll=-$(this).scrollLeft()
$(".fixed-table1-wapper").css("left",scroll+"px");
});
二、注意细节:
1、宽度自适应、去除单元格间隙:
2、表格线:
直接
3、td宽度:
控制第一行宽度即可
4、奇偶行颜色不同:
css: #table1 tr:nth-child(2n){background-color:#ccc;} ie7兼容性问题
jquery: $("#table1 tr:even").css("background-color","#ccc");
以下为完整代码:
表格整理 html{overflow:auto;} .table-wapper{width:100%;margin:30px auto;font-size:16px;position:relative;overflow:hidden;} .table1-wapper{height:200px;overflow-y:auto;} .table-wapper table{border:1px solid #f00;line-height:2;border-collapse: collapse;white-space:nowrap;} .table-wapper th{font-size:18px;font-weight:normal;background-color:#dbdbdb;} .table-wapper td{text-align:center;border:1px solid #f00;} .fixed-table1-wapper{position:absolute;top:0px;left:0px;background-color:#dbdbdb;}
序号 股票名称 股票代码 成交 涨跌幅 换手率 行业板块 1 光明乳业 600597 15.2 +6.23% 0.12% 食品饮料 2 光明乳业 600597 15.2 +6.23% 0.12% 食品饮料 3 光明乳业 600597 15.2 +6.23% 0.12% 食品饮料 4 光明乳业 600597 15.2 +6.23% 0.12% 食品饮料 5 光明乳业 600597 15.2 +6.23% 0.12% 食品饮料 6 光明乳业 600597 15.2 +6.23% 0.12% 食品饮料 7 光明乳业 600597 15.2 +6.23% 0.12% 食品饮料 8 光明乳业 600597 15.2 +6.23% 0.12% 食品饮料 9 光明乳业 600597 15.2 +6.23% 0.12% 食品饮料 10 光明乳业 600597 15.2 +6.23% 0.12% 食品饮料
以上这篇响应式表格之固定表头的简单实现就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持考高分网。



