js完美解决IE6不支持position:fixed的bug

   关于IE6,虽然它已被微软抛弃很久了,但是由于大天朝的特殊行情(盗版)对于前端工程师来说,解决IE6兼容position:fixed的问题显得很重要。特别是你需要用到头尾悬停调用的时候

  先来看段代码

  ?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

<!DOCTYPE html>
<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}
#gs{border:1px solid #000;position:fixed;right:30px;top:120px}
</style>
<!--[if IE 6]>
<style type="text/css">
html{overflow:hidden}
body{height:100%;overflow:auto}
#gs{position:absolute}
</style>
<![endif]-->
</head>
<body>
<div id="rightform">
<p>11111111111111111</p>
<input id="gs" name="gs" type="text" value="" />
</div>
</body>
</html>

  以上这段代码在网上很常见,通过设置html{overflow:hidden}和body{height:100%;overflow:auto}来实现ie6下position:fixed效果,但这种办法有个缺陷,那就是:这会使页面上原有的absolute、relation都变成fixed的效果,在这里我就不做demo了,如果有怀疑,可以自己去试验一下。

  于是我找了下资料,发现可以通过一条Internet Explorer的CSS表达式(expression)来完美的实现ie6下position:fixed效果,css代码如下:

  ?

1
2
3
4
5
6

/* 除IE6浏览器的通用方法 */
.ie6fixedTL{position:fixed;left:0;top:0}
.ie6fixedBR{position:fixed;right:0;bottom:0}
/* IE6浏览器的特有方法 */
* 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+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)))}

  上面代码可以直接使用了,如果要设置元素悬浮边距,要分别为设置两次,比如我要让某个元素距顶部10个像素,距左部也是10个像素,那就要这样子写:

  ?

1
2
3
4

/* 除IE6浏览器的通用方法 */
.ie6fixedTL{position:fixed;left:10px;top:10px}
/* IE6浏览器的特有方法 */
* html .ie6fixedTL{position:absolute;left:expression(eval(document.documentElement.scrollLeft+10));top:expression
(eval(document.documentElement.scrollTop+10))}

  这样一来,IE6下实现position:fixed的效果解决了,而且也不会影响到其他的absolute、relation,但还有一个问题,就是悬浮的元素会出现振动

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

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

  然后我发现background-image无需一张真实的图片,设置成about:blank就行了。

  下面附上完整代码

  ?

1
2
3
4
5
6
7
8

/* 除IE6浏览器的通用方法 */
.ie6fixedTL{position:fixed;left:0;top:0}
.ie6fixedBR{position:fixed;right:0;bottom:0}
/* 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+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)))}

  以上所述就是本文的全部内容了,希望大家能够喜欢。

时间: 2024-09-04 19:00:09

js完美解决IE6不支持position:fixed的bug的相关文章

js完美解决IE6不支持position:fixed的bug_javascript技巧

先来看段代码 <!DOCTYPE html> <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不支持position:fixed的bug

外需要注意的一点就是,导航的宽度必须是固定值,不能是auto或者100%因为fixed和absolute都不认识,当然你也可以手动获取到导航的宽度,然后写到浮动导航样式里,不过有个前提,导航原先样式里不能有:position:relative,情况可能比较多,最简单的方法还是把导航宽度定死    代码如下 复制代码 <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" con

最好用也最简单的方法解决IE6不支持position:fixed问题

在网上找了很多方法,大多是 Javascript 解决的,想想能用 IE6 的电脑,得烂成啥样?再用expression eval ,估计还没滚动几下内存都不够了,所以这种能用 CSS 解决的问题最好都用 CSS 解决了. 前提是你的网页要支持 XHTML 在头部加上 [cc lang='html'] dir="ltr" lang="zh-CN"> [/cc] 然后针对 IE 6 进行如下设置: [cc lang='css'] * { margin:0; pa

CSS实例教程:完美解决IE6不支持hover的方法

文章简介:完美解决IE6不支持hover的方法. <style type="text/css">         p:hover,p.hover { background:#eee; }   /* 就是要多写一个.hover而已 */ </style> 下面这段代码要放到页面的底部在</body> 前: <script type="text/javascript">    //<!–  让所有标记支持hover 

解决IE6不支持Css FIXED实现详解

例1. 下面这个方法可以完美的实现ie6里支持fixed   代码:    代码如下 复制代码 <!DOCTYPE html>  <html>  <head>  <style type="text/css">  *{padding:0;margin:0;}   html,body{ _height:100%; _overflow:hidden;}   body{ _overflow:auto;}   #header,#footer{  

ie6浏览器不支持position:fixed解决办法

滚动条是body的滚动,固定元素是相对body定位,那么把它设置窗口那么高,不就一直是在窗口那个位置了?  代码如下 复制代码 html { _overflow: hidden; } body { _height: 100%; _overflow: auto; } .box {     position: fixed;     left: 10px;     top: 180px;     _position: absolute; } 自家用法介绍  代码如下 复制代码 html,html bo

网页设计小技巧:解决IE6不支持hover

文章简介:网页设计小技巧:解决IE6不支持hover.  IE6以及更低版本的浏览器对":hover"的支持不理想,对于类似的"p:hover"."img:hover"."#header:hover"...,今天给大家介绍一种新的方法,可以完美解决IE6不支持hover的情况,用法很简单,将下载后的"csshover.htc"文件,定义在body样式内 body { behavior:url("c

【原】使用iScroll.js解决ios4下不支持position:fixed的问题

一直折腾position:fixed在ios和android的使用,而事实上这么上流的ios4系统居然不支持position:fixed,幸运的是苹果公司在ios5系统修复了这个bug,比较理想的解决方案是让所有用户把系统升级到ios5及以上版本,这种想法在国外还好,在国内环境下,因为越狱而不想去升级手机的人很多,如果强迫用户去升级,那可能会把你的产品KS了.而你也不可能跟你老板说ios4什么不兼容那个属性啊,让用户升级啊!老板看到的是结果,你做不出来,别人怎么做得出来呢,这样你老板可能会对你的

利用JS解决ie6不支持max-width,max-height问题

 本篇文章主要介绍了利用JS解决ie6不支持max-width,max-height问题的方法.需要的朋友可以过来参考下,希望对大家有所帮助 今天遇到一个关于用js解决ie6不支持支持max-width,max-height的问题,刚开始用jQuery方法来实现,不过一直获取不到css里面的值,如   代码如下:   if($.browser.msie && $.browser.version == 6.0)   {     var maxWidth = parseInt($('.view