雖然 IE6 早已被微軟棄用,但在中國大陸盜版系統盛行的特殊環境下,前端工程師仍須面對 IE6 相容性問題。特別是需要實作頁首、頁尾懸浮固定效果時,position:fixed 在 IE6 完全失效,這讓許多開發者頭痛不已。
網路上流傳一種透過設定 html{overflow:hidden} 和 body{height:100%;overflow:auto} 來模擬 fixed 效果的方法,程式碼如下:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>IE6 position:fixed bug</title>
<style>
*{padding:0;margin:0}
p{height:2000px}
#gs{border:1px solid #000;position:fixed;right:30px;top:120px}
</style>
<!--[if IE 6]>
<style type="text/css">
html{overflow:hidden}
body{height:100%;overflow:auto}
#gs{position:absolute}
</style>
<![endif]-->
</head>
<body>
<div id="rightform">
<p>11111111111111111</p>
<input id="gs" name="gs" type="text" value="" />
</div>
</body>
</html>
這個方法雖然能在 IE6 達到固定效果,但有個嚴重副作用:頁面上原本的 absolute、relative 定位元素都會變成 fixed 效果,嚴重破壞版面配置。
經過研究發現,利用 IE 專屬的 CSS Expression(動態屬性)能精準實現 position:fixed,且不影響其他定位元素。核心程式碼如下:
/* 現代瀏覽器通用寫法 */
.ie6fixedTL { position:fixed; left:0; top:0 }
.ie6fixedBR { position:fixed; right:0; bottom:0 }
/* IE6 專屬修正 */
* html .ie6fixedTL {
position:absolute;
left:expression(eval(document.documentElement.scrollLeft));
top:expression(eval(document.documentElement.scrollTop))
}
* html .ie6fixedBR {
position:absolute;
left:expression(eval(document.documentElement.scrollLeft + document.documentElement.clientWidth - this.offsetWidth - (parseInt(this.currentStyle.marginLeft,10)||0) - (parseInt(this.currentStyle.marginRight,10)||0)));
top:expression(eval(document.documentElement.scrollTop + document.documentElement.clientHeight - this.offsetHeight - (parseInt(this.currentStyle.marginTop,10)||0) - (parseInt(this.currentStyle.marginBottom,10)||0)))
}
若需設定距離頂部、左側各 10px,需分別修改兩組選擇器:
/* 現代瀏覽器 */
.ie6fixedTL { position:fixed; left:10px; top:10px }
/* IE6 修正 */
* html .ie6fixedTL {
position:absolute;
left:expression(eval(document.documentElement.scrollLeft + 10));
top:expression(eval(document.documentElement.scrollTop + 10))
}
IE 的多步渲染機制會導致 Expression 在捲動或縮放視窗時重複計算,造成固定元素出現明顯的「跳動」現象。解決技巧是為 html 或 body 加上 background-attachment:fixed 的背景圖片,強制瀏覽器在重繪前先處理 CSS,進而優先計算 Expression,實現絲滑平滑的固定效果。背景圖片無需真實檔案,about:blank 即可。
/* 現代瀏覽器通用 */
.ie6fixedTL { position:fixed; left:0; top:0 }
.ie6fixedBR { position:fixed; right:0; bottom:0 }
/* 修正 IE6 震動 Bug */
* html, * html body {
background-image: url(about:blank);
background-attachment: fixed
}
/* IE6 固定定位 Expression */
* html .ie6fixedTL {
position:absolute;
left:expression(eval(document.documentElement.scrollLeft));
top:expression(eval(document.documentElement.scrollTop))
}
* html .ie6fixedBR {
position:absolute;
left:expression(eval(document.documentElement.scrollLeft + document.documentElement.clientWidth - this.offsetWidth - (parseInt(this.currentStyle.marginLeft,10)||0) - (parseInt(this.currentStyle.marginRight,10)||0)));
top:expression(eval(document.documentElement.scrollTop + document.documentElement.clientHeight - this.offsetHeight - (parseInt(this.currentStyle.marginTop,10)||0) - (parseInt(this.currentStyle.marginBottom,10)||0)))
}
採用此方案,即可在 IE6 完美實現 position:fixed,且不干擾其他定位元素,畫面滾動時固定元素平滑無抖動。