IE6不支持position:fixed的解決方法

下面這段代碼在網上很常見,經過設置html{overflow:hidden}body{height:100%;overflow:auto}來實現ie6position:fixed效果,但這種辦法有個缺陷,那就是:這會使頁面上原有的absoluterelation都變成fixed的效果,在這裏我就不作demo了,若是有懷疑,能夠本身去試驗一下。 css

<!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>

因而我找了下資料,發現能夠經過一條Internet ExplorerCSS表達式(expression)來完美的實現ie6position:fixed效果,css代碼以下: html

/* 除IE6瀏覽器的通用方法 */
.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)))}

上面代碼能夠直接使用了,若是要設置元素懸浮邊距,要分別爲設置兩次,好比我要讓某個元素距頂部10個像素,距左部也是10個像素,那就要這樣子寫: express

/* 除IE6瀏覽器的通用方法 */
.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))}

這樣一來,IE6下實現position:fixed的效果解決了,並且也不會影響到其餘的absoluterelation,但還有一個問題,就是懸浮的元素會出現振動。 瀏覽器

IE有一個多步的渲染進程。當你滾動或調整你的瀏覽器大小的時候,它將重置全部內容並重畫頁面,這個時候它就會從新處理css表達式。這會引發一個醜陋的"振動"bug,在此處固定位置的元素須要調整以跟上你的(頁面的)滾動,因而就會"跳動" ui

解決此問題的技巧就是使用background-attachment:fixedbodyhtml元素添加一個background-image。這就會強制頁面在重畫以前先處理CSS。由於是在重畫以前處理CSS,它也就會一樣在重畫以前首先處理你的CSS表達式。這將讓你實現完美的平滑的固定位置元素! this

而後我發現background-image無需一張真實的圖片,設置成about:blank就好了。 url

下面附上完整代碼: spa

/* 除IE6瀏覽器的通用方法 */
.ie6fixedTL{position:fixed;left:0;top:0}
.ie6fixedBR{position:fixed;right:0;bottom:0}
/* IE6瀏覽器的特有方法 */
/* 修正IE6振動bug */
* html,* html body{background-image:url(about:blank);background-attachment:fixed}
* 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
相關文章
相關標籤/搜索