ie6在国内的使用还是占有很大一部分,特别是一些中小企业的公司老板,看到网站在ie6下不对劲就说下面的人办事不利,今天说的是IE6不支持position:fixed的解决方法,是在给我们研发做页面的时候碰到的一个问题,下面看看demo页面代码:
<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}/* 除IE6浏览器的通用方法 */.ie6fixedTL{border:1px solid #000;position:fixed;left:0;top:0}.ie6fixedBR{border:1px solid #000;position:fixed;right:0;bottom:0}</style><!--[if IE 6]><style type="text/css">html{overflow:hidden}body{height:100%;overflow:auto}/* IE6浏览器的特有方法 *//* 修正IE6振动bug */* html,* html body{background-image:url(about:blank);background-attachment:fixed}* html .ie6fixedTL{position:absolute;left:expression(eval(document.documentElement.scrollLeft));top:expression(eval(document.documentElement.scrollTop))}* html .ie6fixedBR{position:absolute;left:expression(eval(document.documentElement.scrollLeft-20+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)))}</style><![endif]--></head><body><div id="rightform"><p>hello fixed!</p><input name="gs" type="text" value="ie6fixedTL" class="ie6fixedTL"/><input name="gs" type="text" value="ie6fixedBR" class="ie6fixedBR"/></div></body></html>
大家可以将代码复制下来到本地测试一下。下面简单说明一下:
/* 除IE6浏览器的通用方法 */.ie6fixedTL{border:1px solid #000;position:fixed;left:0;top:0}.ie6fixedBR{border:1px solid #000;position:fixed;right:0;bottom:0}
上面看到的这段是咱们在普通浏览器中常用的一种漂浮层方法,position:fixed属性让层能固定在一个位置,而这个在ie6里不支持,也有用js或jq解决这个问题的,我们也可以在css里找到解决办法,其实还是结合了js。
解决ie6不兼容的核心代码是判断是否是ie6语句(怎么判断ie版本 前面的文章有写)里面的:
* html .ie6fixedTL{position:absolute;left:expression(eval(document.documentElement.scrollLeft));top:expression(eval(document.documentElement.scrollTop))}* html .ie6fixedBR{position:absolute;left:expression(eval(document.documentElement.scrollLeft-20+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)))}
代码中会看到一个较陌生的语句,expression是什么?IE5及其以后版本支持在CSS中使用expression,用来把CSS属性和Javas cript表达式关联起来,这里的CSS属性可以是元素固有的属性,也可以是自定义属性。就是说CSS属性后面可以是一段Javas cript表达式,CSS属性的值等于Javas cript表达式计算的结果。 在表达式中可以直接引用元素自身的属性和方法,也可以使用其他浏览器对象。这个表达式就好像是在这个元素的一个成员函数中一样。
这样咱们的ie6不支持position:fixed属性就解决了,不过咱们还有一个问题要修正一下:
IE有一个多步的渲染进程。当你滚动或调整你的浏览器大小的时候,它将重置所有内容并重画页面,这个时候它就会重新处理css表达式。这会引起一个丑陋的“振动”bug,在此处固定位置的元素需要调整以跟上你的(页面的)滚动,于是就会“跳动”。
解决此问题的技巧就是使用background-attachment:fixed为body或html元素添加一个background-image。这就会强制页面在重画之前先处理CSS。因为是在重画之前处理CSS,它也就会同样在重画之前首先处理你的CSS表达式。这将让你实现完美的平滑的固定位置元素!
/* 修正IE6振动bug */* html,* html body{background-image:url(about:blank);background-attachment:fixed}
更多的关于ie6兼容性解决方法详见《ie6,ie7,ie8 css bug兼容解决方法》
文章评论