DIV CSS 佈局教程網

IE6 的 position: fixed 怎麼寫才完美:三種方案實測
編輯:關於JavaScript     

在還需要照顧 IE6 的年代,position: fixed 是個尷尬的屬性——標準瀏覽器瀏覽器直接支援,IE6 卻完全無視。下面整理三種可行的補救思路,各有取捨。

方案一:純 CSS 模擬

網路上流傳最廣的寫法,用 overflow 重新分配滾動責任:

html { overflow: hidden; }
body { height: 100%; overflow: auto; }
#rightform form { position: absolute; right: 30px; top: 50px; }

它帶著兩個未解的毛病:IE6 下所有 position: absolute 都會變成「浮動」元素,不受容器約束;而用 JavaScript 操作滾動條時,畫面會出現元素閃爍。

方案二:CSS 表達式 + JS 雙管齊下

把 _position: absolute 與 CSS expression 結合,可以在 IE6 裡模擬出 fixed 的效果,再靠一個空白背景圖壓掉閃動:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="https://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>IE6 fixed</title>
<style type="text/css">
* { margin: 0px; padding: 0px; }

body {
  _background-image: url(about:blank);   /* 用瀏覽器空白頁作為背景 */
  _background-attachment: fixed;         /* prevent screen flash in IE6 */
}

#topNav {
  width: 980px;
  z-index: 100;                          /* 浮動層次 */
  overflow: visible;
  position: fixed;
  top: 50px;                             /* 其他瀏覽器下的定位座標 */
  _position: absolute;                   /* IE6 用 absolute 模擬 fixed */
  _top: expression(documentElement.scrollTop + 50 + "px");
  background-color: #0000FF;
  height: 31px;
}

.show {
  position: absolute;
  top: 500px;
  left: 400px;
  border: #ff0000 1px solid;
}
</style>
</head>
<body>
<div class="jd_menu" id="topNav">1111</div>
<div class="show">show</div>
</body>
</html>

上面只處理了垂直方向。若要水平方向也固定,加上 _left: expression(documentElement.scrollLeft + "px") 即可。

如果 <body> 本身要用滾動的背景圖而產生衝突,把固定背景移到 html 選擇器上就能解開:

html {
  _background-image: url(about:blank);
  _background-attachment: fixed;   /* prevent screen flash in IE6 */
}
body {
  background-image: url(1.jpg);
  background-attachment: scroll;
}

方案三:純 JavaScript 跟著滾

不依賴 expression,直接在 scroll 與 resize 事件裡重算 top。思路最直白,行為也最可控:

<style type="text/css">
body { margin: 0px; padding: 0px; font-size: 12px; line-height: 22px; }
.fixed {
  position: absolute;
  right: 60px;
  top: 100px;
  border: #666666 3px solid;
  background-color: #CCCCCC;
  padding: 26px;
}
</style>
<script type="text/javascript">
window.onload = function () {
  var n = 100;                                  // top 值
  var obj = document.getElementById("fixed");   // position:fixed 對象
  window.onscroll = function () {
    obj.style.top = (document.body.scrollTop || document.documentElement.scrollTop) + n + 'px';
  };
  window.onresize = function () {
    obj.style.top = (document.body.scrollTop || document.documentElement.scrollTop) + n + 'px';
  };
}
</script>

額外一招:用條件註解隔離补丁

曾在一個國外站點看到一種很聰明的做法:完全不寫 JS,靠條件註解只讓 IE6 載入一份补丁樣式,滾動依然平滑,也兼顧了 W3C 標準。

<!-- compliance patch for microsoft browsers -->
<!--[if lt IE 7]>
<link rel="stylesheet" href="ie-stuff.css" type="text/css" media="screen"/>
<![endif]-->
/* ie-stuff.css */
#footer {
  position: absolute;
  bottom: auto;
  clear: both;
  top: expression(eval(document.compatMode &&
    document.compatMode == 'CSS1Compat') ?
    documentElement.scrollTop +
      (documentElement.clientHeight - this.clientHeight) - 1 :
    document.body.scrollTop +
      (document.body.clientHeight - this.clientHeight) - 1);
}

三種方案的取捨很清楚:純 CSS 最省事但副作用最多;expression 視覺效果最接近原生 fixed,卻要付出效能代價;JS 方案行為最可控,代價是多一段腳本。選哪一個,取決於你能接受哪一種妥協。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved