`

完美解决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 >

  以上这段代码在网上很常见,通过设置html{overflow:hidden}body{height:100%;overflow:auto} 来实现ie6下position:fixed效果,但这种办法有个缺陷,那就是:这会使页面上原有的absolute、relation都变成fixed的效果,在这里我就不做demo了,如果有怀疑,可以自己去试验一下。

  于是我找了下资料,发现可以通过一条Internet Explorer的CSS表达式(expression)来完美的实现ie6下position:fixed效果,css代码如下:

?
/* 除IE6浏览器的通用方法 */
.ie 6 fixedTL{ position : fixed ; left : 0 ; top : 0 }
.ie 6 fixedBR{ position : fixed ; right : 0 ; bottom : 0 }
/* IE6浏览器的特有方法 */
* html .ie 6 fixedTL{ position : absolute ; left :expression(eval(document.documentElement.scrollLeft)); top :expression(eval(document.documentElement.scrollTop))}
* html .ie 6 fixedBR{ 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浏览器的通用方法 */
.ie 6 fixedTL{ position : fixed ; left : 10px ; top : 10px }
/* IE6浏览器的特有方法 */
* html .ie 6 fixedTL{ position : absolute ; left :expression(eval(document.documentElement.scrollLeft+ 10 )); top :expression(eval(document.documentElement.scrollTop+ 10 ))}

  这样一来,IE6下实现position:fixed的效果解决了,而且也不会影响到其他的absolute、relation,但还有一个问题,就是悬浮的元素会出现振动

?
IE有一个多步的渲染进程。当你滚动或调整你的浏览器大小的时候,它将重置所有内容并重画页面,这个时候它就会重新处理css表达式。这会引起一个丑陋的“振动”bug,在此处固定位置的元素需要调整以跟上你的(页面的)滚动,于是就会“跳动”。
解 决此问题的技巧就是使用background-attachment:fixed为body或html元素添加一个background-image。这 就会强制页面在重画之前先处理CSS。因为是在重画之前处理CSS,它也就会同样在重画之前首先处理你的CSS表达式。这将让你实现完美的平滑的固定位置 元素!

  然后我发现background-image无需一张真实的图片,设置成about:blank就行了。

  下面附上完整代码

?
/* 除IE6浏览器的通用方法 */
.ie 6 fixedTL{ position : fixed ; left : 0 ; top : 0 }
.ie 6 fixedBR{ position : fixed ; right : 0 ; bottom : 0 }
/* IE6浏览器的特有方法 */
/* 修正IE6振动bug */
* html,* html body{ background-image : url (about:blank); background-attachment : fixed }
* html .ie 6 fixedTL{ position : absolute ; left :expression(eval(document.documentElement.scrollLeft)); top :expression(eval(document.documentElement.scrollTop))}
* html .ie 6 fixedBR{ 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 )))}

  至于demo我想大家都看到了吧:)

 

 

分享到:
评论

相关推荐

    IE6实现position:fixed bug (固定窗口方法)的实例

    这个内容是老生常谈了,主要问题就是IE6不支持 position:fixed 引起的BUG.当我们去搜索解决这个bug的垮浏览器解决办法时,绝大多数结果都是说使用 position:absolute 来替代解决,可是我们真的解决了么?没有,因为当页面...

    IE6不支持position:fixed bug的完美解决

    完美解决IE6不支持position:fixed的bug,需要的朋友可以参考下。

    js完美解决IE6不支持position:fixed的bug

    IE6 position:fixed bug&lt;/title&gt; &lt;style&gt; *{padding:0;margin:0} p{height:2000px} #gs{border:1px solid #000;position:fixed;right:30px;top:120px} &lt;/style&gt; &lt;!--[if IE 6]&gt; &lt;style type...

    IE6中的position:fixed定位兼容性写法分享

    非IE6下的写法大家一般都清楚如何写;...}/* IE6 头部固定 */html .fixed-top{position:absolute;bottom:auto;top:[removed]eval&#40;document.documentElement.scrollTop&#41;);}/* IE6 右侧固定 */html .fixed-r

    IE6 position:fixed bug (固定窗口方法)

    正确的代码:预览/Demo | ie6_position_fixed_bug.txt(源代码) &lt;!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”&gt; &lt;...

    CSS表达式(expression)解决IE6 position:fixed无效问题

    IE6 position:fixed bug&lt;/title&gt; &lt;style&gt; *{padding:0;margin:0} p{height:2000px} #gs{border:1px solid #000;position:fixed;right:30px;top:120px} &lt;/style&gt; &lt;!–[if IE 6]&gt;

    IE6 fixed的完美解决方案

    } 这个方法有一个bug未解决:在IE6下会把所有position:absolute都变成“浮动”的元素;还有使用js方法滚动滚动条时会出现对象闪烁,如下方法结合了CSS和js的办法,解决了以上的问题。 代码如下: &lt;!DOCTYPE ...

    ie6 fixed bug的解决方法 (css+js)

    复制代码代码如下: #fixed { position: absolute; top: 0; left: 0;... 但是这样还不完美 并不能完全实现效果,因为不会随body页面拉动而滚动 要实现随body页面滚动而滚动 需添加以下js代码 复制代码代码

    浏览器兼容性问题简介

    CSS兼容性height/width, position:fixed, …脚本问题:Java script 兼容性DOM, Date.getYear, scrollTop, …缺陷(bug)与特性(feature)IE: 盒模型,float,text-alignIE: filter, 资源太大,传百度网盘了...

    unigui0.83.5.820

    - 0000685: UniDBGrid: Ellipsis in first column bug (IE) - 0000690: UniDBGrid: OnTitleClick event - 0000684: UniEdit: Text alignment - 0000683: UniScreenMask bug with Maximized Form and mfPage set - ...

    ExtAspNet v2.2.1 (2009-4-1) 值得一看

    -修正IE7下不能以下划线作为CSS中类名的前缀的BUG(feedback:Steve.Wei)。 -添加定时器控件Timer,用来定时发起AJAX请求。 +2009-09-06 v2.1.0 -Button的Pressed属性值能够正确的反映客户端的变化。 -优化...

    ExtAspNet_v2.3.2_dll

    -修正IE7下不能以下划线作为CSS中类名的前缀的BUG(feedback:Steve.Wei)。 -添加定时器控件Timer,用来定时发起AJAX请求。 +2009-09-06 v2.1.0 -Button的Pressed属性值能够正确的反映客户端的变化。 -优化...

    JavaScript Table行定位效果

    定位方面,除了不支持fixed的ie6用absolute,其他都使用fixed定位。 【克隆tr】 table有一个rows集合,包括了table的所有tr(包括thead和tfoot里面的)。 程序的Clone方法会根据其参数克隆对应索引的tr: this._...

    javascript 弹出层组件(升级版)

    前面文章里写过一个弹出层对话框,但ie6下有bug,根本没有实现position:fixed的效果,当时没有真实ie6环境,测试疏忽,匆匆放到博客上,还让一些读者看到甚至使用,在这里表示万分抱歉啊

    BURNINTEST--硬件检测工具

    - Two 2D Video memory test crash bug workarounds implemented. Crashes in (i) DirectX DirectShow and (ii) ATI atiumdag.dll library. - A hang on startup has been corrected. A 2 minute timeout has been...

Global site tag (gtag.js) - Google Analytics