实现 IE6 支持 position fixed 的 CSS 属性
众所周知 IE6 不支持 position:fixed ,这个 bug 与 IE6 的双倍 margin 和不支持 png 透明等 bug 一样臭名昭著。
如何让 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 图片,而且它工作的同样出色。
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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | /*让position:fixed在IE6下可用! */ /* 头部固定 */ .fixed- top { position : fixed ; bottom : auto ; top : 0px ;} /* 底部固定 */ .fixed- bottom { position : fixed ; bottom : 0px ; top : auto ;} /* 左侧固定 */ .fixed- left { position : fixed ; right : auto ; left : 0px ;} /* 右侧固定 */ .fixed- right { position : fixed ; right : 0px ; left : auto ;} /* 上面的是除了IE6的主流浏览器通用的方法 */ /* 修正IE6振动bug */ * html, * html body{ background-image : url (about:blank); background-attachment : fixed ;} /* IE6 头部固定 */ * html .fixed- top { position : absolute ; bottom : auto ; top :expression(eval(document.documentElement.scrollTop));} /* IE6 右侧固定 */ * html .fixed- right { position : absolute ; right : auto ; left :expression(eval(document.documentElement.scrollLeft+document.documentElement.clientWidth-this.offsetWidth)-(parseInt(this.currentStyle.marginLeft, 10 )|| 0 )-(parseInt(this.currentStyle.marginRight, 10 )|| 0 ));} /* IE6 底部固定 */ * html .fixed- bottom { 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 )));} /* IE6 左侧固定 */ * html .fixed- left { position : absolute ; right : auto ; left :expression(eval(document.documentElement.scrollLeft));} <strong> |
1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 2 3 <html xmlns="http://www.w3.org/1999/xhtml"> 4 5 <head> 6 7 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 8 9 <title>实现 IE6 下支持 position-fixed 的 CSS 属性</title> 10 11 <style type="text/css"> 12 13 * html, * html body { 14 15 background-image:url(about:blank); 16 17 background-attachment:fixed; 18 19 } 20 21 * html .fixed { 22 23 bottom:auto; 24 25 position:absolute; 26 27 right:0; 28 29 top:expression(eval(document.documentElement.scrollTop+document.documentElement.clientHeight-this.offsetHeight-(parseInt(this.currentStyle.marginTop, 10)||0)-(parseInt(this.currentStyle.marginBottom, 10)||0))); 30 31 } 32 33 .fixed { 34 35 bottom:0; 36 37 position:fixed; 38 39 right:0; 40 41 } 42 43 </style> 44 45 </head> 46 47 <body> 48 49 <div style="height:950px;"> </div> 50 51 <div class="fixed">我管你怎么滚,我就是雷打不动,哈哈哈。</div> 52 53 </body> 54 55 </html>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
2011-12-22 30个精美的模板,贺卡,图形圣诞素材