1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > js完美解决IE6不支持position:fixed的bug【javascript】

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

时间:2020-01-23 15:59:57

相关推荐

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

web前端|js教程

IE6不支持position:fixed

web前端-js教程

先来看段代码

微橙科技微信商城源码,vscode调用取色板,ubuntu硬件时间,swing和tomcat,sqlite3 查勘表,前端一滑是一页的框架,爬虫响应速度太慢的原因,php 统计访问次数,seo最新优化政策,微软概念网站模板,php静态网页源码,母婴论坛 模板lzw

IE6 position:fixed bug*{padding:0;margin:0}p{height:2000px}#gs{border:1px solid #000;position:fixed;right:30px;top:120px}html{overflow:hidden}body{height:100%;overflow:auto}#gs{position:absolute}

11111111111111111

返利网微商城网站源码,vscode运行vuejs,ubuntu安装时蓝屏,web部署tomcat,怎么引用sqlite,易语言注册大漠插件,前端框架制作教程学习,爬虫汇报过程,php 为空,seo优化基础知识,php网站框架模板,水果销售网页模板,dede 手机模板安装,返回登陆页面代码,响应式权限管理系统源码,五克商城小程序系统lzw

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

手游源码 完整服务端,vscode设置gihub,ubuntu 巡风,tomcat 版本修改,sqlite存取数据c,相册预览插件下载,前端框架怎么修改样式,python编程爬虫全栈,时间格式化 php,广州seo项目诊断,公安网站下载,安卓网页源码查看,模板名称lzw

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

/* 除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个像素,那就要这样子写:

/* 除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就行了。

下面附上完整代码

/* 除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)))}

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

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。