2011-10-06 83 views
3

我正在使用jQuery,并将内容加载到div中,然后将其显示为对话框。但是,它不是集中在对话框中。jQuery UI对话框不居中

有没有人有解决方案?

代码:

function Core_Load_Register() 
{ 
    $("body").append("<div id='Register_Popup'></div>"); 
    $("#Register_Popup").load("index.php?section=FrontPage&page=Register&nogui=true"); 
    $("#Register_Popup").dialog({ 
     modal: true, 
     height: "auto", 
     width: "auto", 
     buttons: 
     { 
      "Register New Account": 
      function() 
      { 

      }, 

      "Cancel": 
      function() 
      { 
       $(this).dialog("close"); 
      } 
     } 
    }); 
} 

实例截图:

jQuery Dialog not Centered Example http://oi54.tinypic.com/nlznl4.jpg

+1

尝试这样做:http://stackoverflow.com/questions/1467102/dialog-box-not-positions-center-screen –

+0

试过但没有为我工作。 –

+1

我不确定您的对话框代码在DOM中的位置,但我会尝试将它作为“”之前的最后一个元素。 –

回答

9

因为你的内容是动态的尝试等待加载命令先完成。

$("#Register_Popup") 
    .load("index.php?section=FrontPage&page=Register&nogui=true", 
    function() 
    { 
    $("#Register_Popup").dialog({ 
     modal: true, 
     height: "auto", 
     width: "auto", 
     buttons: 
     { 
     "Register New Account": 
     function() 
     { 

     }, 

     "Cancel": 
     function() 
     { 
      $(this).dialog("close"); 
     } 
     } 
    }); 
}); 
+0

该位置默认居中。但即便如此,我仍试图定义它,但它仍然无效。问题是我正在加载使其偏离中心的内容。 –

+0

根据您的评论更新答案。 –

+0

完美,你做到了。谢谢 !!! –

2

我发现使用特定的数值而不是“自动”的高度和宽度选项的作用好得多,总是居中。

这并不总是最佳的,尤其是当在对话框中使用动态数据..但它为我工作(使用jquery ui 1.9.2)。

0

我的解决方案与接受的答案类似。从AJAX调用的角度来看,内容并不是动态的,而是在显示对话框之前立即分配了图像标记的src。即使在对话框中有固定的宽度和高度,它也无法居中。

setTimeout(function() { 
      img.dialog({ 
       draggable: true, 
       height: 'auto', 
       width: 'auto', 
       maxHeight: '768', 
       maxWidth: '1024', 
       modal: true 
      }); 
     }, 500); 

给予它timeout似乎对话能够了解它的内容和位置本身的正确。