1. 程式人生 > >css3實現固定表格頭部而無需設定單元格td的寬度

css3實現固定表格頭部而無需設定單元格td的寬度

背景

最近小弟在工作都是做後臺系統,一堆的表格,各種各樣的。然後需求上要有固定的表頭的表格,如下圖所示

table-thead-fixed

在網上查詢固定表頭的實現方式為:

  • thead 設定為 fixed
  • 拆分表格為兩個表格 thead一個,tbody一個

​然而上面的實現上有個條件是*要提前設定單元格的大小 *, 如果沒有設定對的會就是下面這個樣子(下面是其它的博主的例子圖, 我盜用下 :smirk: )

​:expressionless::expressionless::expressionless::expressionless: 我可不要固定單元格寬度

,固定的寬度怎麼做組作啊。。。

在css3中的transform不會改變原來元素的大小位置,相當於是複製了份出來,然後transform的計算速也夠快(這裡婊一下absolute加left ,top,經常慢半拍)。用這個做這個功能非常合適,還要加點js用於監聽滾輪。

實現方式

執行我寫的 線上例子 打不開,請使用科學上網*:smirk::smirk::smirk:

 

下面貼下程式碼

js:

// Code goes here
'use strict'
window.onload = function(){
  var tableCont = document.querySelector('#table-cont')
  /**
   * scroll handle
   * @param {event} e -- scroll event
   */
  function scrollHandle (e){
    console.log(this)
    var scrollTop = this.scrollTop;
    this.querySelector('thead').style.transform = 'translateY(' + scrollTop + 'px)';
  }

  tableCont.addEventListener('scroll',scrollHandle)
}

css:

/* Styles go here */

.table-cont{
  /**make table can scroll**/
  max-height: 200px;
  overflow: auto;
  /** add some style**/
  /*padding: 2px;*/
  background: #ddd;
  margin: 20px 10px;
  box-shadow: 0 0 1px 3px #ddd;
}
thead{
  background-color: #ddd;
}

html:

<!DOCTYPE html>
<html>

  <head>
    <link data-require="
[email protected]
*" data-semver="3.3.7" rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" /> <link rel="stylesheet" href="style.css" /> <script src="script.js"></script> </head> <body> <div class='table-cont' id='table-cont'><!--看這裡--> <table class="table table-striped"> <thead> <tr> <th>#</th> <th>First Name</th> <th>Last Name</th> <th>Username</th> </tr> </thead> <tbody> <tr> <th scope="row">1</th> <td>Mark</td> <td>Otto</td> <td>@mdo</td> </tr> <tr> <th scope="row">2</th> <td>Jacob</td> <td>Thornton</td> <td>@fat</td> </tr> <tr> <th scope="row">3</th> <td>Larry</td> <td>the Bird</td> <td>@twitter</td> </tr> <tr> <th scope="row">3</th> <td>Larry</td> <td>the Bird</td> <td>@twitter</td> </tr> <tr> <th scope="row">3</th> <td>Larry</td> <td>the Bird</td> <td>@twitter</td> </tr> <tr> <th scope="row">3</th> <td>Larry</td> <td>the Bird</td> <td>@twitter</td> </tr> <tr> <th scope="row">3</th> <td>Larry</td> <td>the Bird</td> <td>@twitter</td> </tr> <tr> <th scope="row">3</th> <td>Larry</td> <td>the Bird</td> <td>@twitter</td> </tr> <tr> <th scope="row">3</th> <td>Larry</td> <td>the Bird</td> <td>@twitter</td> </tr> <tr> <th scope="row">3</th> <td>Larry</td> <td>the Bird</td> <td>@twitter</td> </tr> <tr> <th scope="row">3</th> <td>Larry</td> <td>the Bird</td> <td>@twitter</td> </tr> <tr> <th scope="row">3</th> <td>Larry</td> <td>the Bird</td> <td>@twitter</td> </tr> <tr> <th scope="row">3</th> <td>Larry</td> <td>the Bird</td> <td>@twitter</td> </tr> </tbody> </table> </div> </body> </html>

table-thead-fixed

搞定,美滋滋:thumbsup::thumbsup::thumbsup:

本站專欄文章皆為原創,轉載請註明出處(帶有 前端亂燉 字樣)和本文的顯式連結(http://www.html-js.com/article/4019),本站和作者保留隨時要求刪除文章的權利!