(screen.availWidth/2-320,screen.availHeight/2-240)
你自己根据这两个参数你自己设置一下,screen.availWidth/2得到屏幕的最中心线,然后再根据你的div宽度调整一下,OK?

解决方案 »

  1.   

    <HTML>
    <script>
    function divcenter(){
    document.all.mxh.style.left=(Math.round((document.body.scrollWidth-200)/2)).toString()+"px"
    }
    </script>
    <body onload="divcenter()" onresize=divcenter()>
    <table width=100%>
    <tr>
    <td>test
    </td>
    </tr>
    </table>
    <div id=mxh style="position:absolute;left:200px;top:30px;width:200px;height:200px;background-color:navy;border:2px;color:#FFFFFF">测试文字</div>
    </HTML>
      

  2.   

    麻烦点,在div外边套两个table:
    <table style="position:absolute;left:0;top:0;" height="100%*" width="100%*" valign="middle">
      <tr valign="middle">
        <td>
          <table width="*" border="0" align="center" valign="middle">
            <tr>
              <td>
    <!-- add you div element here. -->
              </td>
            </tr>
          </table>
        </td>
      </tr>
    </table>
      

  3.   

    赋什么值能将div总是显示在屏幕的中央
    屏幕中共有两个按钮,我 想在单击较上的一个时在该位置的屏幕中央显示,
    当滚动到较下的一个按钮时本代码还是在上一次显示的位置,而不是该此位置的屏幕中央显示,
    需要滚动条才看得见div层
    用我试一下好吗? 顺便问一下大家,大家用什么编辑器定javascript,怎样记得这么多的对象属性,
    用VS.Net没有 梅花雨 写的这些属性提示?
    -----------------------------------------------
    <html>
    <head>
    <script language="javascript">
    function switchSpan(spanId){
     
    if(document.all[spanId].style.visibility=="visible")
    document.all[spanId].style.visibility="hidden";
    else{
    document.all[spanId].style.visibility="visible";
    document.all[spanId].style.left=screen.availWidth/2-315;
    /*怎样设置在屏幕的中央,不要用50%这种*/
    document.all[spanId].style.top=screen.availHeight/2-76;
    }
    }
    </script>
    <script language="JavaScript">
    var dragapproved=false
    var z,x,y
    function move(){
    if (event.button==1&&dragapproved){
    z.style.pixelLeft=temp1+event.clientX-x
    z.style.pixelTop=temp2+event.clientY-y
    return false
    }}
    function drags(){
    if (!document.all)
    return
    if (event.srcElement.className=="drag"){
    dragapproved=true
    z=event.srcElement
    temp1=z.style.pixelLeft
    temp2=z.style.pixelTop 
    x=event.clientX
    y=event.clientY
    document.onmousemove=move
    }}
    document.onmousedown=drags
    document.onmouseup=new Function("dragapproved=false")
    </script>
    <style> <!--.drag{
    position:relative;
    cursor:hand;
    background-color: #3399CC;
    visibility: hidden;
    width: 315px;
    height: 76px;
    border: 1pt solid #000000;
    } --></style>
    </head>
    <body bgcolor="#ffffff" text="#000000">
    <div title="移动窗口" id="post" class="drag" name="post">发贴<a href='javascript:switchSpan("post")'>关闭<img alt="关闭发贴窗口"></a>
    <table style="cursor:default" width="100%" height="100" border="0" cellpadding="0" cellspacing="0" bgcolor="#eeeeee">
    <tr>
    <td>
    <input type="text">
    <br>
    <input type="text">
    <br>
    <input type="button" value="button"></td>
    </tr>
    </table>
    </div>
    <input type="button" value="发贴" onclick='switchSpan("post")'> f f f f fff f ff 
    f f f ff ff<p></p>
    f<p></p>
    f f ff ff<p></p>
    f ff<p></p>
    f<p></p>
    f<p></p>
    f<p></p>
    f<p></p>
    f f<p></p>
    ff<p></p>
    f<p></p>
    f<p></p>
    f<p></p>
    f<p></p>
    f f ff ff<p></p>
    f<p></p>
    f<p></p>
    f<p></p>
    f<p></p>
    f ff<p></p>
    f f ff ff<p></p>
    <input type="button" value="发贴" onclick='switchSpan("post")' ID="Button1" NAME="Button1">
    f ff<p></p>
    f
    </body>
    </html>
      

  4.   

    document.all[spanId].style.left=parseInt(parseInt(window.screen .availWidth) -parseInt(document.all[spanId].offsetWidth))/2
      

  5.   

    <HTML>
    <script>
    function divcenter(){
    with(document.body){
    document.all.mxh.style.posLeft=scrollLeft+(offsetWidth-document.all.mxh.style.posWidth)/2
    document.all.mxh.style.posTop=scrollTop+(offsetHeight-document.all.mxh.style.posHeight)/2
    }
    }
    </script>
    <body onload="divcenter()" onresize="divcenter()" onscroll="divcenter()">
    <table width=1000% height=1000%>
    <tr>
    <td>test
    </td>
    </tr>
    </table>
    <div id=mxh style="position:absolute;left:200px;top:30px;width:200px;height:200px;background-color:navy;border:2px;color:#FFFFFF">测试文字</div>
    </HTML>