IE6不支持position:fixed bug的完美解决
网站建设 2023-01-28 20:49www.1681989.com免费网站
废话不多说,先看一下狼蚁网站SEO优化这段代码
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>IE6 position:fixed bug</title>
<style>
{paddg:0;marg:0}
p{height:2000px}
#gs{border:1px solid #000;position:fixed;right:30px;: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>
<put 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了,如果有怀疑,可以自己去试验一下。
于是我找了下资料,发现可以通过一条Inter Explorer的CSS表达式(expression)来完美的实现ie6下position:fixed效果,css代码如下
/ 除IE6浏览器的通用方法 /
.ie6fixedTL{position:fixed;left:0;:0}
.ie6fixedBR{position:fixed;right:0;bottom:0}
/ IE6浏览器的特有方法 /
html .ie6fixedTL{position:absolute;left:expression(eval(document.documentElement.scrollLeft));:expression(eval(document.documentElement.scrollTop))}
html .ie6fixedBR{position:absolute;left:expression(eval(document.documentElement.scrollLeft+document.documentElement.clientWidth-this.offsetWidth)-(parseInt(this.currentStyle.margLeft,10)||0)-(parseInt(this.currentStyle.margRight,10)||0));:expression(eval(document.documentElement.scrollTop+document.documentElement.clientHeight-this.offsetHeight-(parseInt(this.currentStyle.margTop,10)||0)-(parseInt(this.currentStyle.margBottom,10)||0)))}
上面代码可以直接使用了,如果要设置元素悬浮边距,要分别为设置两次,比如我要让某个元素距顶部10个像素,距左部也是10个像素,那就要这样子写
/ 除IE6浏览器的通用方法 /
.ie6fixedTL{position:fixed;left:10px;:10px}
/ IE6浏览器的特有方法 /
html .ie6fixedTL{position:absolute;left:expression(eval(document.documentElement.scrollLeft+10));: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就行了。
狼蚁网站SEO优化附上完整代码
/ 除IE6浏览器的通用方法 /
.ie6fixedTL{position:fixed;left:0;: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));:expression(eval(document.documentElement.scrollTop))}
html .ie6fixedBR{position:absolute;left:expression(eval(document.documentElement.scrollLeft+document.documentElement.clientWidth-this.offsetWidth)-(parseInt(this.currentStyle.margLeft,10)||0)-(parseInt(this.currentStyle.margRight,10)||0));:expression(eval(document.documentElement.scrollTop+document.documentElement.clientHeight-this.offsetHeight-(parseInt(this.currentStyle.margTop,10)||0)-(parseInt(this.currentStyle.margBottom,10)||0)))}
复制代码
代码如下:<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>IE6 position:fixed bug</title>
<style>
{paddg:0;marg:0}
p{height:2000px}
#gs{border:1px solid #000;position:fixed;right:30px;: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>
<put 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了,如果有怀疑,可以自己去试验一下。
于是我找了下资料,发现可以通过一条Inter Explorer的CSS表达式(expression)来完美的实现ie6下position:fixed效果,css代码如下
复制代码
代码如下:/ 除IE6浏览器的通用方法 /
.ie6fixedTL{position:fixed;left:0;:0}
.ie6fixedBR{position:fixed;right:0;bottom:0}
/ IE6浏览器的特有方法 /
html .ie6fixedTL{position:absolute;left:expression(eval(document.documentElement.scrollLeft));:expression(eval(document.documentElement.scrollTop))}
html .ie6fixedBR{position:absolute;left:expression(eval(document.documentElement.scrollLeft+document.documentElement.clientWidth-this.offsetWidth)-(parseInt(this.currentStyle.margLeft,10)||0)-(parseInt(this.currentStyle.margRight,10)||0));:expression(eval(document.documentElement.scrollTop+document.documentElement.clientHeight-this.offsetHeight-(parseInt(this.currentStyle.margTop,10)||0)-(parseInt(this.currentStyle.margBottom,10)||0)))}
上面代码可以直接使用了,如果要设置元素悬浮边距,要分别为设置两次,比如我要让某个元素距顶部10个像素,距左部也是10个像素,那就要这样子写
复制代码
代码如下:/ 除IE6浏览器的通用方法 /
.ie6fixedTL{position:fixed;left:10px;:10px}
/ IE6浏览器的特有方法 /
html .ie6fixedTL{position:absolute;left:expression(eval(document.documentElement.scrollLeft+10));: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就行了。
狼蚁网站SEO优化附上完整代码
复制代码
代码如下:/ 除IE6浏览器的通用方法 /
.ie6fixedTL{position:fixed;left:0;: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));:expression(eval(document.documentElement.scrollTop))}
html .ie6fixedBR{position:absolute;left:expression(eval(document.documentElement.scrollLeft+document.documentElement.clientWidth-this.offsetWidth)-(parseInt(this.currentStyle.margLeft,10)||0)-(parseInt(this.currentStyle.margRight,10)||0));:expression(eval(document.documentElement.scrollTop+document.documentElement.clientHeight-this.offsetHeight-(parseInt(this.currentStyle.margTop,10)||0)-(parseInt(this.currentStyle.margBottom,10)||0)))}
上一篇:CSS hack 介绍及速查对照表
下一篇:请慎用样式表中用星号定全局样式
网站设计
- 静宁会SEO的网站建设公司:全面提升您的网络影
- 提升在线业务的关键:选择最佳的丽水网站建设
- 浙江网站优化发展潜力如何
- 井研专业的网站建设公司:打造您的在线品牌
- 灵山SEO网站建设公司:提升您的在线业务表现
- 蒙城网站建设优化公司:提升您网站表现的理想
- 阳谷企业网站优化:提升线上业务力的关键
- 樟树专业的网站建设公司:打造您在线业务的坚
- 通河百度SEO排名的策略与技巧
- 重庆百度快照排名如何进行精准的客户引流
- 重庆百度快照排名
- 常宁便宜的建站公司:助您轻松打造在线业务
- 巫溪百度网站优化:提升网站曝光率与流量的关
- 湖北整站优化怎么做才能放大客户需求
- 闸北网站建设多少钱?全面解析与预算规划
- 辽宁企业网站优化怎么做电话营销