求一段jquery实现div始终垂直居中于浏览器代码

解决方案 »

  1.   


    <!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" />
    <title>换行</title>
    <style>
    #test{
    width:200px;
    height:200px;
    border:1px solid red;
    }
    </style>
    <script src="jquery-1.3.2.js"></script>
    <script>
    function test(){
      var width = document.documentElement.clientWidth;
      var height = document.documentElement.clientHeight;
      var width1 = $("#test").width();
      var height1 = $("#test").height();
      var temp_width = (width - width1)/2 + "px";
      var temp_top = (height - height1)/2 + "px";
      $("#test").css("margin-left",temp_width);
      $("#test").css("margin-top",temp_top);
    }
    </script>
    </head><body onresize="test()" onload="test()">
    <div id="test"></div>
    </body>
    </html>