2013-10-10 42 views
0

我有以下的jQuery彈出對話框的初始化,動態內容:中心的jQuery彈出對話框與汽車的寬度和高度自動

jQuery("#popUp").live("click", function (e) { 
    e.preventDefault(); 
    jQuery("<div></div>") 
     .addClass("dialog") 
     .attr("id", "popUpDialog") 
     .appendTo("body") 
     .dialog({ 
      title: jQuery(this).attr("data-dialog-title"), 
      close: function() { jQuery(this).remove(); }, 
      modal: true, 
      hide: { effect: "none", duration: 150 }, 
      show: { effect: "none", duration: 150 }, 
      width: 'auto', 
      height: 'auto', 
      position: { 
       my: 'center', 
       at: 'center', 
      of: jQuery(window) 
      } 
     }).load(this.href); 

    jQuery(".close").live("click", function (e) { 
     e.preventDefault(); 
     jQuery(this).closest(".dialog").dialog("close"); 
    }); 
}); 

});

我很抱歉這個最簡單的問題,但我不能把彈出窗口放在整個頁面的中心。問題是:

    position: { 
        my: 'center', 
        at: 'center', 
       of: jQuery(window)} 
+0

位置:{ 我:「中心」, 在:「中心」的 :jQuery的(窗口) }刪除它通過默認顯示對話框在中心 –

回答

3

問題是因爲對話框在其內容加載之前被定位,因此它的大小在變化。從而使內容首先加載改變你的邏輯,然後實例對話框:

jQuery('<div />', { 
    class: 'dialog', 
    id: 'popUpDialog' 
}) 
    .appendTo("body") 
    .load(this.href, function() { 
     // set up the dialog in the callback of the AJAX request... 
     jQuery(this).dialog({ 
      title: jQuery(this).attr("data-dialog-title"), 
      close: function() { jQuery(this).remove(); }, 
      modal: true, 
      hide: { effect: "none", duration: 150 }, 
      show: { effect: "none", duration: 150 }, 
      width: 'auto', 
      height: 'auto', 
      position: { 
       my: 'center', 
       at: 'center', 
       of: window 
      } 
     }) 
    }); 
+0

我用它了很多在我的應用程序(MVC)中。點擊具有id'popUp'的按鈕,內容將顯示在popUp中,我如何改變我的邏輯,我可以使用SeTimeOut功能嗎? – MDDDC

+0

你可以,你需要將左邊位置設置爲'(window.width/2) - (dialog.width/2)',頂部位置也是一樣的。 –

0

這應該是足夠的位置選項:

position:'center' 

工作的呢?

+0

沒有它不工作,它會工作,如果我會指示寬度和高度,寬度:自動和高度:自動是不工作... – MDDDC

相關問題