position:fixed兼容性
IE6不支持position:fixed,解决方法是ie6 css hack,设置_position: absolute; _marin _top _bottom _left ...
position:fixed在IE6中使用
本文所使用的技巧是用了一条Internet Explorer的CSS表达式(expression)。你不可以直接使用该表达式,因为它可能会因为缓存而不更新。解决这一点的最简单的方式是使用eval包裹你的语句。
如何解决“振动”的问题?
显然IE有一个多步的渲染进程。当你滚动或调整你的浏览器大小的时候,它将重置所有内容并重画页面,这个时候它就会重新处理css表达式。这会引起一个丑陋的“振动”bug,在此处固定位置的元素需要调整以跟上你的(页面的)滚动,于是就会“跳动”。
解决此问题的技巧就是使用background-attachment:fixed;为body或html元素添加一个background-image。这就会强制页面在重画之前先处理CSS。因为是在重画之前处理CSS,它也就会同样在重画之前首先处理你的CSS表达式。这将让你实现完美的平滑的固定位置元素!
你可以使用一个about:blank替代一个spacer.gif图片
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta http-equiv="X-UA-Compatible" content="IE=7" /> <title>chmod755 - 演示站</title> <style type="text/css"> html,body{margin:0; padding:0;background-image:url(about:blank);background-attachment:fixed;} .wrap{height:2000px; background:blue} .float { background-color: red; text-align: center; font-size: 12px; z-index: 9999; position: fixed; _position: absolute; } #top{ width:100%; height:50px; line-height: 50px; top: 0; _top: expression(eval(documentElement.scrollTop)); _bottom:auto; } #center{ width:200px; height:100px; line-height:100px; margin:-50px 0 0 -100px; top: 50%; left: 50%; _top: expression(eval(documentElement.scrollTop + (document.documentElement.clientHeight-this.offsetHeight)/2)); _left: expression(eval(documentElement.scrollLeft + (document.documentElement.clientWidth-this.offsetWidth)/2)); _margin:0; } #fotter{ width:100%; height:50px; line-height: 50px; bottom:0; margin:-50px 0 0 0; _top:expression(eval(document.documentElement.scrollTop+document.documentElement.clientHeight-this.offsetHeight-(parseInt(this.currentStyle.marginTop,10)||0)-(parseInt(this.currentStyle.marginBottom,10)||0))); _margin:0; } #right_center{ width:100px; height:200px; line-height: 200px; right: 0; top: 50%; margin:-100px 0 0 0; _top: expression(eval(documentElement.scrollTop + (document.documentElement.clientHeight-this.offsetHeight)/2)); _margin:0; _right:-1px; } #right_fotter{ background:green; width:100px; height:200px; line-height: 200px; right: 0; bottom:0; _top:expression(eval(document.documentElement.scrollTop+document.documentElement.clientHeight-this.offsetHeight-(parseInt(this.currentStyle.marginTop,10)||0)-(parseInt(this.currentStyle.marginBottom,10)||0))); _right:-1px; } </style> </head> <body> <div class="float" id="top">顶部</div> <div class="float" id="center">垂直居中</div> <div class="float" id="fotter">底部</div> <div class="float" id="right_center">右中</div> <div class="float" id="right_fotter">右下</div> <div class="wrap"></div> </body> </html>