数据展示时,表头的固定,可以有更好的可读性。
一、实现方式:
1、定义2个表格,一个absolute固定
<div class="table1-wapper"> <table width="100%" cellpadding="0" cellspacing="0" id="table1"> <tr><th><div>序号</div></th><th><div>股票名称</div></th>...</tr> <tr>...</tr> </table> </div> <div class="fixed-table1-wapper"> <table width="100%" cellpadding="0" cellspacing="0" id="fixed-table1"> </table> </div>
2、表1<th>复制,并插入表2
var th_new=$("#table1 tr").eq(0).clone(); $("#fixed-table1").append(th_new);
3、resize()方法,实时获取表1各列<th>宽度
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、宽度自适应、去除单元格间隙:
<table width="100%" cellpadding="0" cellspacing="0" ></table>
2、表格线:
直接<td>添加border,会出现边线重合;添加属性:border-collapse: collapse;
3、td宽度:
控制第一行宽度即可 <td width="70"></td> / <td width="20%"></td>
4、奇偶行颜色不同:
css: #table1 tr:nth-child(2n){background-color:#ccc;} ie7兼容性问题
jquery: $("#table1 tr:even").css("background-color","#ccc");
以下为完整代码:
<!doctype html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>表格整理</title> <link rel="stylesheet" type="text/css" href="css/basic.css"> <script type="text/javascript" src="js/jquery-1.8.3.min.js"></script> <style type="text/css"> 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;} /*#table1 tr:nth-child(2n){background-color:#ccc;}*/ </style> </head> <body > <div class="table-wapper"> <div class="table1-wapper"> <table width="100%" cellpadding="0" cellspacing="0" id="table1"> <tr> <th>序号</th> <th>股票名称</th> <th>股票代码</th> <th>成交</th> <th>涨跌幅</th> <th>换手率</th> <th>行业板块</th> </tr> <tr> <td>1</td> <td>光明乳业</td> <td>600597</td> <td>15.2</td> <td>+6.23%</td> <td>0.12%</td> <td>食品饮料</td> </tr> <tr> <td>2</td> <td>光明乳业</td> <td>600597</td> <td>15.2</td> <td>+6.23%</td> <td>0.12%</td> <td>食品饮料</td> </tr> <tr> <td>3</td> <td>光明乳业</td> <td>600597</td> <td>15.2</td> <td>+6.23%</td> <td>0.12%</td> <td>食品饮料</td> </tr> <tr> <td>4</td> <td>光明乳业</td> <td>600597</td> <td>15.2</td> <td>+6.23%</td> <td>0.12%</td> <td>食品饮料</td> </tr> <tr> <td>5</td> <td>光明乳业</td> <td>600597</td> <td>15.2</td> <td>+6.23%</td> <td>0.12%</td> <td>食品饮料</td> </tr> <tr> <td>6</td> <td>光明乳业</td> <td>600597</td> <td>15.2</td> <td>+6.23%</td> <td>0.12%</td> <td>食品饮料</td> </tr> <tr> <td>7</td> <td>光明乳业</td> <td>600597</td> <td>15.2</td> <td>+6.23%</td> <td>0.12%</td> <td>食品饮料</td> </tr> <tr> <td>8</td> <td>光明乳业</td> <td>600597</td> <td>15.2</td> <td>+6.23%</td> <td>0.12%</td> <td>食品饮料</td> </tr> <tr> <td>9</td> <td>光明乳业</td> <td>600597</td> <td>15.2</td> <td>+6.23%</td> <td>0.12%</td> <td>食品饮料</td> </tr> <tr> <td>10</td> <td>光明乳业</td> <td>600597</td> <td>15.2</td> <td>+6.23%</td> <td>0.12%</td> <td>食品饮料</td> </tr> </table> </div> <div class="fixed-table1-wapper"> <table width="100%" cellpadding="0" cellspacing="0" id="fixed-table1"> </table> </div> </div> <script type="text/javascript"> $(function(){<BR> $("#table1 tr:even").css("background-color","#ccc"); //奇偶行颜色 var inner_width=$("#table1").outerWidth(); $(".fixed-table1-wapper").css("width",inner_width+"px"); var th_new=$("#table1 tr").eq(0).clone(); $("#fixed-table1").append(th_new); }) $(window).resize(function(){ trResize(); }); $(".table1-wapper").scroll(function(){ var scroll=-$(this).scrollLeft() $(".fixed-table1-wapper").css("left",scroll+"px"); }); function trResize(){ var inner_width=$("#table1").outerWidth(); $(".fixed-table1-wapper").css("width",inner_width+"px"); $("#fixed-table1 th").each(function(){ var num=$(this).index(); var th_width=$("#table1 th").eq(num).width(); //console.log("th_width:"+th_width); $(this).css("width",th_width+"px"); }); } </script> </body> </html>
以上这篇响应式表格之固定表头的简单实现就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持小牛知识库。
我做了一个表头和列固定的表。但这里我已经指定了我不想要的头/体的宽度。我用引导类col-md-6(50%宽度)将整个表放在一个div中。但是除非我为thead和tbody硬编码样式,否则它是不起作用的。我希望thead和tbody取父div的宽度。但如果没有指定像素或百分比,则会占用屏幕的全宽。这是我的代码 null null 所有我想要的是使表的响应,而不是像静态400px或50%或类似的东西。
本文向大家介绍Angular实现响应式表单,包括了Angular实现响应式表单的使用技巧和注意事项,需要的朋友参考一下 介绍 Angular 总共提供了 3 中表单实现方式,分别是:Template-driven Forms (模板驱动表单) 、 Reactive Forms (响应式表单) 、 Dynamic Forms (动态表单) 。本文只介绍响应式表单。 响应式表单是什么呢?其实跟我们以前
问题内容: 许多人仍然使用表格来布局控件,数据等。-流行的jqGrid就是一个例子。但是,发生了一些我似乎无法理解的魔术(它的桌子大声喊叫,可能有多少魔术?) 如何设置表的列宽并使它像jqGrid那样遵守!?如果我尝试复制此内容,即使我设置了every ,只要这些单元格之一的内容大于20px,该单元格就会展开! 有什么想法或见解吗? 问题答案: 您可以尝试对所有行使用标签管理表样式,但是您需要在或
我正试图让一个Google表单根据1个表单输入的数据,用多行填充电子表格,如下所示: 表单简单,客户信息最少https://docs.google.com/forms/d/1LrKlVuI7kxVU0lxz70Uu-2Obj4x3qIwe6nS-ErzbCAg/ 输入后,我需要表格在表格(或格式化表格)中输入数据,如下所示:-输入的每个部分(1、2或3)应位于单独的行中,具有相同的客户名称 然后,
本文向大家介绍JS实现table表格固定表头且表头随横向滚动而滚动,包括了JS实现table表格固定表头且表头随横向滚动而滚动的使用技巧和注意事项,需要的朋友参考一下 先看一张效果图 思路: 1、头部用一个table并用一个div包裹着, 表格的具体内容用一个table 2、头部外面的div用positon: relative相对定位 3、获取整个表格的高度 4、获取表格的dom(或者包裹着表格的
让我们假设使用相同的登录表单,但现在我们还要测试我们的密码在其中某处有感叹号。 app/login-form.component.ts 一个简单的函数接受实例,并返回null如果一切都很好。 如果测试失败,它返回一个具有任意命名属性的对象。 属性名称将用于.hasError()测试。 app/login-form.component.ts <!-- ... --> <div [hidden]="!