IE6不兼容position:fixed屬性的解決辦法分享

position: fixed;這個屬性用起來確實很方便,可以輕松的實現固定位置的浮動層效果 。但是,它不支持IE6及以下版本 。于是很多同學使用JS模擬 。今天寫了一個DEMO,涉及左側、右側 。及上下兩邊 , 共四種位置的固定,與以往的教程不同的地方是 , 它使用CSS表達式來兼容IE5、IE6,且避免了js模擬時,拖動滾動條時出現抖動的問題,另外在IE5或者怪癖模式下也完全正常,沒有任何問題 。如果你有更好的方案,歡迎來噴我 。
下面是代碼

復制代碼代碼如下:
!DOCTYPE HTML
html
head
meta charset="utf-8"
titleposition: fixedwebjx.com/title
style type="text/css"
* {
padding: 0;
margin: 0;
}
#content {
height: 5000px;
width: 50%;
border-right: 10px dotted red;
}
#demo_t, #demo_b, #demo_l, #demo_r {
background: #f90;
position: fixed;
}
#demo_t, #demo_b {
left: 0;
width: 100%;
}
#demo_l, #demo_r {
width: 50px;
top: 300px;
}
#demo_t {
top: 0;
}
#demo_b {
bottom: 0;
}
#demo_l {
left: 0;
}
#demo_r {
right: 0;
}
/style
!--[if lte IE 6]
style type="text/css"
html {
/*這個可以讓IE6下滾動時無抖動*/
background: url(about:black) no-repeat fixed
}
#demo_t, #demo_b, #demo_l, #demo_r {
position: absolute;
}
#demo_t, #demo_b {
/*這個解決body有padding時,IE6下100%不能鋪滿的問題*/
width: expression(offsetParent.clientWidth);
}
/*下面三組規則用于IE6下top計算*/
#demo_l, #demo_r {
top: expression(offsetParent.scrollTop300);
}
#demo_t {
top: expression(offsetParent.scrollTop);
}
#demo_b {
top: expression(offsetParent.scrollTopoffsetParent.clientHeight-offsetHeight);
}
/style
![endif]--
/head
body
div id="demo_t"此處顯示 id "demo" 的內容/div
div id="demo_b"此處顯示 id "demo" 的內容/div
div id="demo_l"此處顯示 id "demo" 的內容/div
div id="demo_r"此處顯示 id "demo" 的內容/div
div id="content"/div
/body
/html

建議在實際使用時 , 將IE條件注釋中的代碼放在單獨的css文件中,以便節約其他瀏覽器的流量 。
順便頂一下微軟的這個項目,現在有中文版了 , 建議廣大前端er加入這一行列,盡快滅亡IE6

相關經驗推薦