CSS表達式(expression)解決IE6 position:fixed無效問題
發(fā)布時間:2014-07-28 16:29:01 作者:佚名
我要評論

IE6 position:fixed無效在做兼容時,很是頭疼,本例通過一條Internet Explorer的CSS表達式(expression)來完美的實現(xiàn)ie6下position: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>
以上這段代碼在網(wǎng)上很常見,通過設置html{overflow:hidden}和body{height:100%;overflow:auto}來實現(xiàn)ie6下position:fixed效果,但這種辦法有個缺陷,那就是:這會使頁面上原有的absolute、relation都變成fixed的效果,在這里我就不做demo了,如果有懷疑,可以自己去試驗一下。
于是我找了下資料,發(fā)現(xiàn)可以通過一條Internet Explorer的CSS表達式(expression)來完美的實現(xiàn)ie6下position:fixed效果,css代碼如下:
/* 除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個像素,那就要這樣子寫:
/* 除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下實現(xiàn)position:fixed的效果解決了,而且也不會影響到其他的absolute、relation,但還有一個問題,就是懸浮的元素會出現(xiàn)振動
IE有一個多步的渲染進程。當你滾動或調(diào)整你的瀏覽器大小的時候,它將重置所有內(nèi)容并重畫頁面,這個時候它就會重新處理css表達式。這會引起一個丑陋的“振動”bug,在此處固定位置的元素需要調(diào)整以跟上你的(頁面的)滾動,于是就會“跳動”。
解決此問題的技巧就是使用background-attachment:fixed為body或html元素添加一個background-image。這就會強制頁面在重畫之前先處理CSS。因為是在重畫之前處理CSS,它也就會同樣在重畫之前首先處理你的CSS表達式。這將讓你實現(xiàn)完美的平滑的固定位置元素!
然后我發(fā)現(xiàn)background-image無需一張真實的圖片,設置成about:blank就行了。
下面附上完整代碼
/* 除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-(parseInt(this.currentStyle.marginTop,10)||0)-(parseInt(this.currentStyle.marginBottom,10)||0)))}
復制代碼
代碼如下:<!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>
以上這段代碼在網(wǎng)上很常見,通過設置html{overflow:hidden}和body{height:100%;overflow:auto}來實現(xiàn)ie6下position:fixed效果,但這種辦法有個缺陷,那就是:這會使頁面上原有的absolute、relation都變成fixed的效果,在這里我就不做demo了,如果有懷疑,可以自己去試驗一下。
于是我找了下資料,發(fā)現(xiàn)可以通過一條Internet Explorer的CSS表達式(expression)來完美的實現(xiàn)ie6下position:fixed效果,css代碼如下:
復制代碼
代碼如下:/* 除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個像素,那就要這樣子寫:
復制代碼
代碼如下:/* 除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下實現(xiàn)position:fixed的效果解決了,而且也不會影響到其他的absolute、relation,但還有一個問題,就是懸浮的元素會出現(xiàn)振動
IE有一個多步的渲染進程。當你滾動或調(diào)整你的瀏覽器大小的時候,它將重置所有內(nèi)容并重畫頁面,這個時候它就會重新處理css表達式。這會引起一個丑陋的“振動”bug,在此處固定位置的元素需要調(diào)整以跟上你的(頁面的)滾動,于是就會“跳動”。
解決此問題的技巧就是使用background-attachment:fixed為body或html元素添加一個background-image。這就會強制頁面在重畫之前先處理CSS。因為是在重畫之前處理CSS,它也就會同樣在重畫之前首先處理你的CSS表達式。這將讓你實現(xiàn)完美的平滑的固定位置元素!
然后我發(fā)現(xiàn)background-image無需一張真實的圖片,設置成about:blank就行了。
下面附上完整代碼
復制代碼
代碼如下:/* 除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-(parseInt(this.currentStyle.marginTop,10)||0)-(parseInt(this.currentStyle.marginBottom,10)||0)))}
相關文章
- 用CSS的expression判斷表達式設置input樣式,簡單,輕量級。缺點在于expression判斷表達式FireFox是不支持的。致命的是只能區(qū)分出一個(例如例子中就只能區(qū)分出text文本框2009-06-14
- CSS中寫expression可能會在Chrome中有問題.2010-01-27
優(yōu)化瀏覽器渲染 避免CSS expressions
CSS表達式會降低瀏覽器的渲染性能;用其他方案替換它們將會改善IE瀏覽器的渲染性能。2010-03-09CSS中使用expression完美設置頁面最小寬度(兼容ie)
CSS中使用expression有ie才能識別,其把CSS屬性和Javascript表達式關聯(lián)起來,效果相當于min-width,即實現(xiàn)了兼容ie,示例如下,感興趣的朋友可以參考下,希望對大家有所幫2013-07-31css expression使用概述及其優(yōu)缺點介紹
css expression(css表達式)又稱Dynamic properties(動態(tài)屬性)是早期微軟DHTML的產(chǎn)物,微軟從IE8 beta2標準模式開始,取消對css expression的支持,感興趣的朋友可以了解下2013-11-04CSS行為expression輕松實現(xiàn)IE6無抖動固定定位
IE6不支持固定定位(position:fixed)是眾所周知的事情,想在IE6做出固定定位的效果就只能用JS,用js會出現(xiàn)“跳動”的效果,下面為大家介紹下CSS中的行為expression2014-03-03- 這篇文章主要介紹了CSS中使用expression表達式,需要的朋友可以參考下2014-12-22