IE6下 position:fixed 兼容问题

来源:互联网 发布:韩红 中国好声音 知乎 编辑:程序博客网 时间:2024/05/14 09:59

IE6不支持position:fixed,这个bug与IE6的双倍margin和不支持PNG透明等bug一样臭名昭著。

你是如何让position:fixed在IE6中工作的?

本文所使用的技巧是用了一条Internet Explorer的CSS表达式(expression)。你不可以直接使用该表达式,因为它可能会因为缓存而不更新。解决这一点的最简单的方式是使用eval包裹你的语句。

如何解决“振动”的问题?

显然IE有一个多步的渲染进程。当你滚动或调整你的浏览器大小的时候,它将重置所有内容并重画页面,这个时候它就会重新处理css表达式。这会引起一个丑陋的“振动”bug,在此处固定位置的元素需要调整以跟上你的(页面的)滚动,于是就会“跳动”。

解决此问题的技巧就是使用background-attachment:fixed为body或html元素添加一个background-image。这就会强制页面在重画之前先处理CSS。因为是在重画之前处理CSS,它也就会同样在重画之前首先处理你的CSS表达式。这将让你实现完美的平滑的固定位置元素!

<span style="font-size:14px;">/*让position:fixed在IE6下可用! */  .fixed-top /* 头部固定 */{position:fixed;bottom:auto;top:0px;}.fixed-bottom /* 底部固定 */{position:fixed;bottom:0px;top:auto;}.fixed-left /* 左侧固定 */{position:fixed;right:auto;left:0px;}.fixed-right /* 右侧固定 */{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 右侧固定 */ {position:absolute;right:auto;left:expression(eval(document.documentElement.scrollLeft</span>
<span style="font-size:14px;">+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 </span>
<span style="font-size:14px;">+document.documentElement.clientHeight-this.offsetHeight-(parseInt(this.currentStyle.marginTop,10)||0)-(parseInt(this.currentStyle.marginBottom,10)||0)));}* html .fixed-left /* IE6 左侧固定 */{position:absolute;right:auto;left:expression(eval(document.documentElement.scrollLeft));}</span>

0 0