序言
前段時间在工作中中恰好必须这个作用,可是找了许多都不可以完善的完成,因此在此就自身做了1个固定不动表头的方式,关键用到了css3中的translate和1小段js编码,下面来1起看看吧。
实际效果以下:
觉得是否很和睦,并且编码也少,不够的是IE9下列不适用translate特性,但如今也没是多少要考滤IE9下列的适配了吧,做前端开发老兼具低版本号的访问器免不了会让自身畏畏缩缩。。。。
下面看来下编码吧
HTML
<div class="box"> <table> <thead> <tr> <th>1</th> <th>2</th> <th>3</th> <th>4</th> <th>5</th> <th>6</th> <th>7</th> <th>8</th> <th>9</th> <th>10</th> <th>11</th> <th>12</th> <th>13</th> <th>14</th> <th>15</th> </tr> </thead> <tbody> <script> var tr = ''; for(var i=0; i<15; i++) { tr += '<tr>\ <td>'+i+'</td>\ <td>'+i+'</td>\ <td>'+i+'</td>\ <td>'+i+'</td>\ <td>'+i+'</td>\ <td>'+i+'</td>\ <td>'+i+'</td>\ <td>'+i+'</td>\ <td>'+i+'</td>\ <td>'+i+'</td>\ <td>'+i+'</td>\ <td>'+i+'</td>\ <td>'+i+'</td>\ <td>'+i+'</td>\ <td>'+i+'</td>\ </tr>'; } document.write(tr); </script> </tbody> </table> </div>
CSS款式
<style> *{ margin: 0; padding: 0; list-style: none;} .box { width: 300px; height: 300px; margin: 50px auto 0; overflow: auto; } .box table{ width: 100%; border-collapse: collapse; border-right: 1px solid #ccc; border-top: 1px solid #ccc; text-align: center; } .box table thead { background-color: #ccc; } .box table th, .box table td { padding: 8px 10px; border-left: 1px solid #ccc; border-bottom: 1px solid #ccc; white-space: nowrap; } </style>
JS脚本制作
<script> window.onload = function() { var $ = document.querySelector.bind(document); var boxEle = $('.box'); boxEle.addEventListener('scroll', function(e) { this.querySelector('thead').style.transform = 'translate(0, '+this.scrollTop+'px)'; }); } </script>
总结
好了,以上便是这篇文章内容的所有內容了,编码是否真的非常少呢,还在等甚么?快点体会1下吧。期待本文的內容对大伙儿的学习培训或工作中能带来1定的协助,假如有疑惑大伙儿能够留言沟通交流。