萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 腳本程式專題 >> JavaScript

CSS expression 固定表格表頭與左欄:IE 專屬凍結列實作

利用 IE 專屬的 CSS expression() 動態計算 top/left,實現表格捲動時表頭固定頂部、左欄固定左側。注意:expression 在 IE8+ 標準模式、所有現代瀏覽器均已移除/不支援,僅供維護舊系統參考。

核心 CSS

.FixedTitleRow { /* 凍結表頭列 */
    position: relative;
    top: expression(this.offsetParent.scrollTop);
    z-index: 10;
    background-color: #FFCC00;
}
.FixedDataColumn { /* 凍結左欄 */
    position: relative;
    left: expression(this.parentElement.offsetParent.parentElement.scrollLeft);
    background-color: #E6ECF0;
}
.FixedColumnAndRows { /* 左上角交叉區 */
    position: relative;
    top: expression(this.offsetParent.scrollTop);
    left: expression(this.offsetParent.scrollLeft);
    z-index: 20;
    background-color: #FFCC00;
}

HTML 結構要點

  • 外層 <div id="scrollDiv" style="width:500px; height:150px; overflow:auto; position:absolute;"> 提供捲軸容器。
  • table 加 style="table-layout:fixed" 固定版面算法。
  • 表頭列套用 FixedTitleRow / FixedColumnAndRows,左欄套用 FixedDataColumn / FixedColumnAndRows。

現代標準替代方案

  1. CSS position: sticky(支援現代所有瀏覽器):
    thead th { position: sticky; top: 0; z-index: 1; }
    tbody th { position: sticky; left: 0; }
  2. 雙 Table 法:分離表頭 Table 與內容 Table,同步捲動事件。
  3. 成熟庫:FixedHeader (DataTables)、Grid.js、AG Grid 等。
copyright © 萬盛學電腦網 all rights reserved