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