这样是不行的。
我上面的代码就已经是取屏幕高度了。如果下拉滑动条发生变化,div 的 top 还是不变的。

解决方案 »

  1.   

    用event.x  event.y   试试..   
    或者 event.clienty   
      

  2.   

      加一句:
    document.body.onscroll=function()
        {         
              div_content.style.top = document.documentElement.scrollTop+windows.screen.height/2;      
        }
      

  3.   

    我给的代码经过测试一定能用,拿分了!~
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
    <style type="text/css">
    </style>
    <title>无标题文档</title>
    <script language="JavaScript" type="text/javascript">
    function load(){  
        var div_test = document.getElementById("div_center");
        var doc_element = document.documentElement;
        div_test.style.left = document.body.clientWidth/2 - parseInt(div_test.style.width)/2;
        div_test.style.top  = doc_element.clientHeight/2 - parseInt(div_test.style.height)/2;
    }
    function scroll(){
        var div_test = document.getElementById("div_center");
        var doc_element = document.documentElement;
        div_test.style.left = document.body.clientWidth/2 - parseInt(div_test.style.width)/2;
    //div_test.style.top  = document.body.clientHeight/2 - parseInt(div_test.style.height)/2;
        div_test.style.top  = doc_element.clientHeight/2 - parseInt(div_test.style.height)/2 + doc_element.scrollTop;
    }
    window.onscroll = scroll;
    </script>
    </head>
    <body onload="load()">
    <script language="JavaScript" type="text/javascript">
        for(var i=0;i<20;i++)
        {
        for(var j=0;j<100;j++){
            document.write("测试"+j);
            }
            document.write("<br>");
    }

    </script>
    <div id="div_center" style="border:solid;position:absolute;width:200px;height:200px;left:50%;font-size:20px;background-color :red;"></div>
    </body>
    </html>