修正IE6不支持position:fixed的bug

众所周知IE6不支持position:fixed,这个bug与IE6的双倍margin和不支持PNG透明等bug同样臭名昭著。前些天我作本身的博客模板的时候,遇到了这个问题。当时就简单的无视了IE6——尽管有几个使用IE6的朋友,一块儿BS我……可是对于大项目或商业网站,若是有用到这个属性的时候,是不可能直接无视的。css

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

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

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

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

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

这个方案并非我提供的。我是在网上的某个地方读到这些的。若是你知道是谁原创了这个方法,请告诉前端观察。 我发现的另一个小技巧是,你根本无需一个真实的图片!你可使用一个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 {
    position:fixed;
    right:auto;
    left:0px;
}
/* 右侧固定 */
.fixed-right {
    position:fixed;
    right:0px;
    left:auto;
}


/* 上面的是除了IE6的主流浏览器通用的方法 */

/* 修正IE6振动bug */
* html,* html body {
    background-image:url(about:blank);
    background-attachment:fixed;
}

/* IE6 头部固定 */
* html .fixed-top {
    position:absolute;
    bottom:auto;
    top:expression(eval(document.documentElement.scrollTop));
}

/* IE6 右侧固定 */
* html .fixed-right {
    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));
}

/* IE6 底部固定 */
* html .fixed-bottom {
    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)));
}

/* IE6 左侧固定 */
* html .fixed-left {
    position:absolute;
    right:auto;
    left:expression(eval(document.documentElement.scrollLeft));
}
相关文章
相关标签/搜索