365bet体育|www.635288com-365体育手机在线

热门关键词: 365bet体育,www.635288com,365体育手机在线

简单的表格.gif

2020-02-01 作者:365bet体育   |   浏览(199)

在网页中,如果表哥数据量在两个维度上都较大时是不便于查看数据的,如果能够像excel那样冻结表头将有助于提高数据查阅的体验和效率。目前可够选择的解决方案很多,如果不希望引入过多的第三方库,那么运用jQuery也可以实现简单的表格表头冻结。

  • 构建一个简单的表格
<!DOCTYPE html><html lang="zh-CN"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>简单表格</title> <link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va PmSTsz/K68vbdEjh4u" crossorigin="anonymous"> <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries --> <!-- WARNING: Respond.js doesn't work if you view the page via file:// --> <!--[if lt IE 9]> <script src="http://www.hntybf.com/uploads/allimg/200201/02102J242-0.jpg"></script> <script src="http://www.hntybf.com/uploads/allimg/200201/02102H0M-1.jpg"></script> <![endif]--> </head> <body> <table > <thead> <tr> <th>id</th> <th>name</th> <th>age</th> <th>sex</th> <th>posotion</th> <th>introduction</th> </tr> </thead> <tbody> <!-- 该行多次复制 --> <tr> <td>1</td> <td>stone</td> <td>12</td> <td>male</td> <td>manager</td> <td>a man</td> </tr> </tbody> </table> <!-- jQuery (necessary for Bootstrap's JavaScript plugins) --> <script src="http://www.hntybf.com/uploads/allimg/200201/02102H237-2.jpg"></script> <!-- Include all compiled plugins , or include individual files as needed --> <script src="http://www.hntybf.com/uploads/allimg/200201/02102K251-3.jpg" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script> </body></html>

图片 1简单的表格.gif

  • 另外创建一个一模一样的只有表头的表并将这个表格固定在页面顶部
<table style="display: none;position: fixed;top:0;"> <thead> <tr> <th>id</th> <th>name</th> <th>age</th> <th>sex</th> <th>posotion</th> <th>introduction</th> </tr> </thead></table>
  • 页面滚动到主表格表头消失的时候显示附表
<script type="text/javascript"> $(function(){ $.scroll(function  { var scroll = $.scrollTop(); if (scroll > 30) { $('#head-table').css('display', 'block'); } else { $('#head-table').css('display', 'none'); } }); });</script>

图片 2滚动.gif

  • 调整附表表头宽度和主表一致
$('#head-table').find.each(function(){ $.width($('#main-table').find('th:eq('   $.index.width;

图片 3调整附表表头宽度和主表一致.gif

  • 为附表头加上背景颜色
<table style="display: none;position: fixed;top:0;background-color: aquamarine;"> <thead> <tr> <th>id</th> <th>name</th> <th>age</th> <th>sex</th> <th>posotion</th> <th>introduction</th> </tr> </thead></table>

图片 4为附表头加上背景颜色.gif

  • 完整代码
<!DOCTYPE html><html lang="zh-CN"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>简单表格</title> <link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va PmSTsz/K68vbdEjh4u" crossorigin="anonymous"> <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries --> <!-- WARNING: Respond.js doesn't work if you view the page via file:// --> <!--[if lt IE 9]> <script src="http://www.hntybf.com/uploads/allimg/200201/02102J242-0.jpg"></script> <script src="http://www.hntybf.com/uploads/allimg/200201/02102H0M-1.jpg"></script> <![endif]--> </head> <body> <table > <thead> <tr> <th>id</th> <th>name</th> <th>age</th> <th>sex</th> <th>posotion</th> <th>introduction</th> </tr> </thead> <tbody> <!-- 该行多次复制 --> <tr> <td>1</td> <td>stone</td> <td>12</td> <td>male</td> <td>manager</td> <td>a man</td> </tr> </tbody> </table> <table style="display: none;position: fixed;top:0;background-color: aquamarine;"> <thead> <tr> <th>id</th> <th>name</th> <th>age</th> <th>sex</th> <th>posotion</th> <th>introduction</th> </tr> </thead> </table> <!-- jQuery (necessary for Bootstrap's JavaScript plugins) --> <script src="http://www.hntybf.com/uploads/allimg/200201/02102H237-2.jpg"></script> <!-- Include all compiled plugins , or include individual files as needed --> <script src="http://www.hntybf.com/uploads/allimg/200201/02102K251-3.jpg" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script> <script type="text/javascript"> $(function(){ $('#head-table').find.each(function(){ $.width($('#main-table').find('th:eq('   $.index.width; $.scroll(function  { var scroll = $.scrollTop(); if (scroll > 30) { $('#head-table').css('display', 'block'); } else { $('#head-table').css('display', 'none'); } }); }); </script> </body></html>

本文首发于公众号:programmer_cc,转载请注明出处。

图片 5微信公众号.jpg

本文由365bet体育发布于365bet体育,转载请注明出处:简单的表格.gif

关键词: 表头 jQuery table