眾所周知ie6不支持position:fixed,這個bug與ie6的雙倍margin和不支持png透明等bug一樣臭名昭著。前些天我做自己的博客模板的時候,遇到了這個問題。當(dāng)時就簡單的無視了ie6——盡管有幾個使用ie6的朋友,一起bs我……但是對于大項(xiàng)目或商業(yè)網(wǎng)站,如果有用到這個屬性的時候,是不可能直接無視的。
本文所使用的技巧是用了一條internet explorer的css表達(dá)式(expression)。你不可以直接使用該表達(dá)式,因?yàn)樗赡軙驗(yàn)榫彺娑桓隆=鉀Q這一點(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圖片,而且它工作的同樣出色。
123456789101112 | /*讓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)));}* html .fixed-left /* ie6 左側(cè)固定 */{position:absolute;right:auto;left:expression(eval(document.documentelement.scrollleft));} |
更新: 添加border、padding 和margin 支持!
note: 如果你不需要支持margin,可以將所有的`parseint`部分去掉。
note: 我只在標(biāo)準(zhǔn)模式下進(jìn)行了測試。
新聞熱點(diǎn)
疑難解答
圖片精選