css3實現(xiàn)固定表格頭部而無需設(shè)置單元格td的寬度

本博客持續(xù)修改與更新中,點擊這里查看最新的內(nèi)容

** 已知在移動端和safairi 都沒有 軟用,大家看看就好,有好的方式可以說下 **
[TOC]

背景

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

table-thead-fixed

在網(wǎng)上查詢固定表頭的實現(xiàn)方式為:

  • thead 設(shè)置為 fixed
  • 拆分表格為兩個表格 thead一個,tbody一個

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

?:expressionless::expressionless::expressionless::expressionless: 我可不要固定單元格寬度,固定的寬度怎么做組作啊。。。

在css3中的transform不會改變原來元素的大小位置,相當(dāng)于是復(fù)制了份出來,然后transform的計算速也夠快(這里婊一下absolute加left ,top,經(jīng)常慢半拍)。用這個做這個功能非常合適,還要加點js用于監(jiān)聽滾輪。

實現(xiàn)方式

運行我寫的 在線例子 打不開,請使用科學(xué)上網(wǎng)*:smirk::smirk::smirk:

<iframe height= 600 width= 100% src="https://embed.plnkr.co/k7ipmtXkpL0MDT9KGZbc/" frameborder=0 allowfullscreen></iframe>

下面貼下代碼

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="bootstrap@*" data-semver="3.3.7" rel="stylesheet"  />
    <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:

簡書不支 iframe和emoji嗎? 不開心

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • HTML標(biāo)簽解釋大全 一、HTML標(biāo)記 標(biāo)簽:!DOCTYPE 說明:指定了 HTML 文檔遵循的文檔類型定義(D...
    米塔塔閱讀 3,527評論 1 41
  • 前端07班 王 對于table的使用我們會有種先入為主的厭惡。覺得頁面中不應(yīng)該出現(xiàn)表格!其實這只是針對使用HTML...
    ea203453e188閱讀 2,806評論 0 5
  • img標(biāo)簽: img標(biāo)簽中的img其實是英文image的縮寫,所以img標(biāo)簽的作用, 就是告訴瀏覽器我們需要顯示一...
    佩佩216閱讀 1,701評論 0 2
  • 問答題47 /72 常見瀏覽器兼容性問題與解決方案? 參考答案 (1)瀏覽器兼容問題一:不同瀏覽器的標(biāo)簽?zāi)J(rèn)的外補...
    _Yfling閱讀 14,122評論 1 92
  • null and undefied (PJ3.4.3) you should never explicitly s...
    tslling閱讀 202評論 0 0

友情鏈接更多精彩內(nèi)容