网页前端设计

http://www.86y.org

搜索文章

css解决ie6下不兼容position:fixed

用声音读出全文关注我吧
 2011/10/6 20:31:10 阅读次数:6747

在IE6中是不能直接使用 position:fixed; 。需要一些 CSS Hack 来解决它。当然,IE6 的问题也不仅仅 position:fixed;

 下例让 <div id=”top”>…</div> 元素固定在浏览器的底部和距离右边的20个像素:

 

#top{

  position:fixed;_position:

  absolute;bottom:0;

  right:5%;

  _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)));
 }

 

right 跟 left 属性可以用绝对定位的办法解决,而 top 跟 bottom 就需要用上面的表达式来实现。其中在 _position:absolute; 中的 _ 符号只有 IE6 才能识别,目的是为了区分其他浏览器。

上面的只是一个例子,下面的才是最重要的代码片段:

使元素固定在浏览器的顶部

#top{

_position:absolute;

_bottom:auto;

 _top:expression(eval(document.documentElement.scrollTop));

}

使元素固定在浏览器的底部

#top{

_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)));

 }

这两段代码只能实现在最底部跟最顶部,你可以使用

_margin-top:10px;

或者

_margin-bottom:10px;

修改其中的数值控制元素的位置。

 

position:fixed; 闪动问题

现在,问题还没有完全解决。在用了上面的办法后,你会发现:被固定定位的元素在滚动滚动条的时候会闪动。解决闪动问题的办法是在 CSS 文件中加入:

*html{

background-image:url(about:blank);

background-attachment:fixed;

}

其中 * 是给 IE6 识别的。

到此,IE6 的 position:fixed; 问题已经被解决了。


大家有什么问题或技术上的想法可以在此与大家分享,也可以加入前端爱好者QQ群(141999928)一起学习进步:【幸凡前端技术交流群】
0

如果您觉得本文的内容对您的学习有所帮助,捐赠与共勉,支付宝(左)或微信(右)

阅读全文内容关闭