js完美解決IE6不支持position:fixed的bug
先來看段代碼
<!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)上很常見,通過設(shè)置html{overflow:hidden}和body{height:100%;overflow:auto}來實(shí)現(xiàn)ie6下position:fixed效果,但這種辦法有個(gè)缺陷,那就是:這會(huì)使頁面上原有的absolute、relation都變成fixed的效果,在這里我就不做demo了,如果有懷疑,可以自己去試驗(yàn)一下。
于是我找了下資料,發(fā)現(xiàn)可以通過一條Internet Explorer的CSS表達(dá)式(expression)來完美的實(shí)現(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)))}
上面代碼可以直接使用了,如果要設(shè)置元素懸浮邊距,要分別為設(shè)置兩次,比如我要讓某個(gè)元素距頂部10個(gè)像素,距左部也是10個(gè)像素,那就要這樣子寫:
/* 除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下實(shí)現(xiàn)position:fixed的效果解決了,而且也不會(huì)影響到其他的absolute、relation,但還有一個(gè)問題,就是懸浮的元素會(huì)出現(xiàn)振動(dòng)
IE有一個(gè)多步的渲染進(jìn)程。當(dāng)你滾動(dòng)或調(diào)整你的瀏覽器大小的時(shí)候,它將重置所有內(nèi)容并重畫頁面,這個(gè)時(shí)候它就會(huì)重新處理css表達(dá)式。這會(huì)引起一個(gè)丑陋的“振動(dòng)”bug,在此處固定位置的元素需要調(diào)整以跟上你的(頁面的)滾動(dòng),于是就會(huì)“跳動(dòng)”。
解決此問題的技巧就是使用background-attachment:fixed為body或html元素添加一個(gè)background-image。這就會(huì)強(qiáng)制頁面在重畫之前先處理CSS。因?yàn)槭窃谥禺嬛疤幚鞢SS,它也就會(huì)同樣在重畫之前首先處理你的CSS表達(dá)式。這將讓你實(shí)現(xiàn)完美的平滑的固定位置元素!
然后我發(fā)現(xiàn)background-image無需一張真實(shí)的圖片,設(shè)置成about:blank就行了。
下面附上完整代碼
/* 除IE6瀏覽器的通用方法 */
.ie6fixedTL{position:fixed;left:0;top:0}
.ie6fixedBR{position:fixed;right:0;bottom:0}
/* IE6瀏覽器的特有方法 */
/* 修正IE6振動(dòng)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)))}
以上所述就是本文的全部內(nèi)容了,希望大家能夠喜歡。
相關(guān)文章
echarts圖表無數(shù)據(jù)/空數(shù)據(jù)如何展示"暫無數(shù)據(jù)"
在開發(fā)echarts的時(shí)候我們不得不考慮數(shù)據(jù)為空的情況,其實(shí)有很多種解決辦法,下面這篇文章主要給大家介紹了關(guān)于echarts圖表無數(shù)據(jù)/空數(shù)據(jù)如何展示“暫無數(shù)據(jù)”的相關(guān)資料,需要的朋友可以參考下2022-10-10
你必須了解的JavaScript中的屬性描述對(duì)象詳解(上)
JavaScript提供了一個(gè)內(nèi)部數(shù)據(jù)結(jié)構(gòu),用來描述對(duì)象的屬性,控制它的行為,比如該屬性是否可寫、可遍歷等等。這個(gè)內(nèi)部數(shù)據(jù)結(jié)構(gòu)稱為“屬性描述對(duì)象”。本文主要帶大家了解一下JavaScript中你必須了解的屬性描述對(duì)象,需要的可以參考一下2022-12-12
js實(shí)現(xiàn)本地持久化存儲(chǔ)登錄注冊(cè)
這篇文章主要為大家詳細(xì)介紹了js實(shí)現(xiàn)本地持久化存儲(chǔ)登錄注冊(cè),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-08-08

