如何在IE6下實現position:fixed效果,positionfixed

來源:互聯網
上載者:User

如何在IE6下實現position:fixed效果,positionfixed

如何在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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.