本文所使用的技巧是用了一條Internet Explorer的CSS表達(dá)式(expression)。你不可以直接使用該表達(dá)式,因?yàn)樗赡軙驗(yàn)榫彺娑桓?。解決這一點(diǎn)的最簡單的方式是使用eval
包裹你的語句。
顯然IE有一個多步的渲染進(jìn)程。當(dāng)你滾動或調(diào)整你的瀏覽器大小的時候,它將重置所有內(nèi)容并重畫頁面,這個時候它就會重新處理css表達(dá)式。這會引起一個丑陋的“振動”bug,在此處固定位置的元素需要調(diào)整以跟上你的(頁面的)滾動,于是就會“跳動”。
解決此問題的技巧就是使用background-attachment:fixed
為body或html元素添加一個background-image。這就會強(qiáng)制頁面在重畫之前先處理CSS。因?yàn)槭窃谥禺嬛疤幚鞢SS,它也就會同樣在重畫之前首先處理你的CSS表達(dá)式。這將讓你實(shí)現(xiàn)完美的平滑的固定位置元素!
這個方案并不是我提供的。我是在網(wǎng)上的某個地方讀到這些的。如果你知道是誰原創(chuàng)了這個方法,請告訴前端觀察。
我發(fā)現(xiàn)的另外一個小技巧是,你根本無需一個真實(shí)的圖片!你可以使用一個about:blank
替代一個spacer.gif圖片,而且它工作的同樣出色。
讓position:fixed在IE6下可用!
.fixed-top /* 頭部固定 */{position:fixed;bottom:auto;top:0px;}
.fixed-bottom /* 底部固定 */{position:fixed;bottom:0px;top:auto;}
.fixed-left /* 左側(cè)固定 */{position:fixed;right:auto;left:0px;}
.fixed-right /* 右側(cè)固定 */{position:fixed;right:0px;left:auto;}
/* 上面的是除了IE6的主流瀏覽器通用的方法 */
* html,* html body /* 修正IE6振動bug */{background-image:url(about:blank);background-attachment:fixed;}
* html .fixed-top /* IE6 頭部固定 */{position:absolute;bottom:auto;top:expression(eval(document.documentElement.scrollTop));}
* html .fixed-right /* IE6 右側(cè)固定 */ {position:absolute;right:auto;left:expression(eval(document.documentElement.scrollLeft+document.documentElement.clientWidth-this.offsetWidth)-(parseInt(this.currentStyle.marginLeft,10)||0)-(parseInt(this.currentStyle.marginRight,10)||0));}
* html .fixed-bottom /* IE6 底部固定 */{position:absolute;bottom:auto;top:expression(eval(document.documentElement.scrollTop+document.documentElement.clientHeight-this.offsetHeight-(parseInt(this.currentStyle.marginTop,10)||0)-(parseInt(this.currentStyle.marginBottom,10)||0)));}