浮動橫幅、固定在底部的操作列、隨捲動停在視窗角落的按鈕,都屬於「把元素釘在畫面上」的需求。做法是 position: fixed,再針對舊版 IE 補一段兼容寫法。
.bottomTable{
background-color: rgb(249, 249, 249);
z-index: 99999999;
position: fixed;
bottom: 0;
left: 0;
width: 100%;
_position: absolute; /* for IE6 */
_top: expression(document.body.scrollTop + document.body.clientHeight - this.offsetHeight - 6); /* for IE6 */
overflow: visible;
}
前導底線的 _position 與 _top 是 IE 專用的 hack:只有 IE6 以下會讀懂,其他瀏覽器一律忽略,因此得以在保留標準寫法的同時補上舊版相容。
今天的實務建議:IE6 早已退場,expression 也因為效能問題被微軟移除,正式專案直接寫 position: fixed 即可。若確實需要相容舊版 IE,建議改用 feature detection 搭配 JavaScript 於 onscroll 事件中手動計算位置,並注意 fixed 元素在行動裝置上與虛擬鍵盤、位址列收合之間的互動,必要時可用 position: sticky 取代,讓元素在容器內自然黏附。