请问一下,我想弹出一个div,但是此div需要显示在页面的正中央(无论滚动过几屏),请问该怎么计算top属性现在该div已经可以弹出了
left属性我已经用window.screen.width计算出来了
但是top属性,我该怎么计算,请帮助,谢谢。

解决方案 »

  1.   

    window.screen.height  也有这个属性 啊    然后除以2  在减去你的层的高度除以2   然后 加上 document.body.scrollTop 
      

  2.   


    <!doctype html>
    <html>
        <head>
            <meta charset="gb2312" />
            <title>Test</title>
            <style>
                body { height:2000px; }
                #test { 
    position:absolute; background:#ddd; 
    }
            </style>
            <script>
                function $(o){return document.getElementById(o)}
                window.onload = function(){
                    var obj = $('test');
    var sWidth = document.documentElement.scrollWidth;
    var sHeight = document.documentElement.clientHeight;
    obj.style.left = sWidth/2-parseInt(obj.style.width)/2+'px';
    obj.style.top = sHeight/2-parseInt(obj.style.height)/2+'px';
                    window.onscroll = function(){
                        var iTop = document.documentElement.scrollTop + sHeight/2-parseInt(obj.style.height)/2+'px';
    obj.style.top = iTop;
                    }
                }
            </script>
        </head>
        <body>
            <div id="test" style="width:100px; height:100px;"></div>
        </body>
    </html>
    楼主试试