如何在IE6下实现position:fixed效果

如何在IE6下实现position:fixed效果:
建议:尽可能的手写代码,可以有效的提高学习效率和深度。
由于IE6并不支持position:fixed,所以导致很多好的效果都无法实现,但是在IE6下并不是不能够实现此中效果,下面就通过一段实例介绍一下如何实现此种效果。代码实例如下:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="author" content="http://www.51texiao.cn/" />
<title>蚂蚁部落</title>
<style type="text/css">
body{
  margin:40px;
  padding:0px;
  border:1px solid blue;
}
.first{
  width:300px;
  height:600px;
  border:1px solid red;
  margin:20px;
}
.first .fixed{
  width:100px;
  height:100px;
  background-color:black;
  position:fixed;
  _margin-top:20px;
  _position:absolute;
  _top:expression(eval(document.documentElement.scrollTop));
  left:20px;
  top:20px;
}
</style>
</head>
<body>
<div class="first">
  <div class="fixed"></div>
</div>
</body>
</html>

以上代码可以兼容IE6和其他主流浏览器,实现此效果的核心代码是:

_top:expression(eval(document.documentElement.scrollTop));

注意:加上了上面这一行代码之后,IE6下top功能失效了,所以要加一个_margin-top。
虽然上面的代码实现了想要的功能,但是还是有点问题,那就是当拖动滚动条的时候会出现颤抖现象。代码修改如下:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="author" content="http://www.51texiao.cn/" />
<title>蚂蚁部落</title>
<style type="text/css">
*{
  margin:0px;
  padding:0px;
}
body{height:1000px;}
#thediv{
  width:100px;
  height:100px;
  background-color:#CCC;
  position:fixed;
  _margin-top:20px;
  _position:absolute;
  _top:expression(eval(document.documentElement.scrollTop));
  left:20px;
  top:20px;
  text-align:center;
  line-height:100px;
}
</style>
<script type="text/javascript" src="mytest/jQuery/jquery-1.8.3.js"></script>
<script type="text/javascript">
$(function(){
  $(window).scroll(function(){
    $("#thediv").text($(window).scrollTop());
  })
});
</script>
</head>
<body>
<div id="thediv"></div>
</body>
</html>

以上代码完美实现了我们想要的功能,在第一段实例基础上添加如下代码:

*html{
  background-image:url(about:blank);
  background-attachment:fixed;
}

原文地址是:http://www.51texiao.cn/div_cssjiaocheng/2015/0405/144.html

时间: 2024-08-25 19:32:40

如何在IE6下实现position:fixed效果的相关文章

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

废话不多说,先看一下下面这段代码: <!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{heigh

解决IE6不支持position:fixed属性

最近在优化网站浮动广告时候遇见了IE6不支持position:fixed属性.上网收集了一下解决方案 比较好的方案就是利用css表达式进行解决 补充:CSS Expression (CSS 表达式),是一种使用动态设置 CSS 属性的方式,并且被 IE5 以上的版本所支持,但是 IE8 的标准模式已不再支持 CSS 表达式了 IE7和以上的浏览器都支持position:fixed: 之前写过一篇介绍过固定页脚的文字,那时候没在ie6下测试 方法一 <!--[if IE 6]> <styl

让IE6也支持position:fixed

众所周知IE6不支持position:fixed,这个bug与IE6的双倍margin和不支持PNG透明等bug一样臭名昭著.前些天遇到了这个问题.当时就简单的无视了IE6,但是对于大项目或商业网站,如果有用到这个属性的时候,是不可能直接无视的.涞水县梁以纸业 如何让position:fixed在IE6中工作呢? 本文所使用的技巧是用了一条InternetExplorer的CSS表达式(expression).你不可以直接使用该表达式,因为它可能会因为缓存而不更新.解决这一点的最简单的方式是使用

IE6 下关于Position:absolute;属性对同级元素的影响问题

今天小码哥做一个专题页面的时候,碰到一个关于IE6下position:absolute:属性对同级元素的margin属性有影响的问题.虽然,作为前端人员,IE6下的Bug问题,始终让人头疼不已.但生活在Zhong国的码民们只能继续忍受. 言归正传,是什么问题呢? 即:假如在一个设有position:relative:相对定位属性的div盒子里,同时放另外两个div块级元素layer2,layer3.layer2设有position:absolute:属性,layer3设有margin:30px

解决IE6不支持position:fixed固定定位的bug(转载范鸭)

http://blog.funya.in/csscss3/ie6-fixed-bug/ 在IE6中实现 position:fixed; 的办法: 浏览器头部固定 .fixed_top { position:fixed; top:0px; } * html .fixed_top /* IE6 头部固定 */{ position:absolute; bottom:auto; top:expression(eval(document.documentElement.scrollTop)); } 浏览器

关于ie6 不支持position:fixed的问题

css中有position: fixed;可以直接用.IE6确不支持,解决办法: * html,* html body{background-image:url(about:blank);background-attachment:fixed;} .top-nav-wrap-fix{position: fixed; top:0; _position:absolute;_top:expression_r(eval_r(document.documentElement.scrollTop)); le

解决IE6不支持position:fixed的Bug

/* 除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:

ie6下不支持fixed的解决方法

position: fixed;_position: absolute;right: 0;_right: -1px;top: 80px;_bottom: auto;z-index: 2147483647;_top: expression(eval(document.documentElement.scrollTop + 80));_top:expression(eval(document.documentElement.scrollTop || document.body.scrollTop)

解决IE6浏览器下position:fixed固定定位问题

2010年4月15日 像你所遇到的问题一样, IE6浏览器有太多的bug让制作网页的人头疼.这篇文章介绍的是介绍的是如何解决IE6不支持position:fixed;属性的办法.如果我们需要做某个元素始终位于浏览器的底部,不会因为浏览器窗口的缩放和滚动条的滚动而变化,那个肯定是想到的用position:fixed生成绝对定位,只要设置这个CSS属性就能达到刚刚的需求.当其他浏览器都正常显示的时候,只有IE6不那么完美.该元素的位置是通过"left", "top",