日期:2014-05-17  浏览次数:20779 次

如何让一个div固定悬浮在浏览器顶部?类似这样的
刚刚看见 http://www.wpxap.com/ 顶部用户信息栏是个固定位置的div,网页可以在这个div下面滚动,这个是如何实现的?

------解决方案--------------------

<div id="d1" style="position:absolute;background:#eeeeee;border:1px dotted #000;">
<div><img src="..."/></div>
<div>content</div>
</div>
<script type="text/javascript">
function scrollImg(){
    var posX,posY;
    if (window.innerHeight) {
        posX = window.pageXOffset;
        posY = window.pageYOffset;
    }
    else if (document.documentElement && document.documentElement.scrollTop) {
        posX = document.documentElement.scrollLeft;
        posY = document.documentElement.scrollTop;
    }
    else if (document.body) {
        posX = document.body.scrollLeft;
        posY = document.body.scrollTop;
    }
 
    var ad=document.getElementById("d1");
    ad.style.top=(posY+100)+"px";
    ad.style.left=(posX+50)+"px";
    setTimeout("scrollImg()",100);
}
scrollImg();
</script>


------解决方案--------------------

div{width: 100%; height: 30px; position: fixed; top: 0;opacity:0.75;}

------解决方案--------------------
那个还在用IE6嘛
你要IE6
多加2句就完了


html { _background-image: url(about:blank); _background-attachment: fixed; }
div { width: 100%; height: 30px;position: fixed; top: 0; opacity: 0.75; _position: absolute; _bottom: auto; _top: expression(eval(document.documentElement.scrollTop)); }