利用 IE 專屬的 CSS expression() 動態計算 top/left,實現表格捲動時表頭固定頂部、左欄固定左側。注意:expression 在 IE8+ 標準模式、所有現代瀏覽器均已移除/不支援,僅供維護舊系統參考。
.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;
}
<div id="scrollDiv" style="width:500px; height:150px; overflow:auto; position:absolute;"> 提供捲軸容器。table 加 style="table-layout:fixed" 固定版面算法。FixedTitleRow / FixedColumnAndRows,左欄套用 FixedDataColumn / FixedColumnAndRows。position: sticky(支援現代所有瀏覽器):
thead th { position: sticky; top: 0; z-index: 1; }
tbody th { position: sticky; left: 0; }
FixedHeader (DataTables)、Grid.js、AG Grid 等。