2011-12-15 99 views
1

我想在jQuery中定位一個對話框,並且我有以下代碼。它似乎沒有做我期望的事情。我只需要將對話框設置爲居中或出現在身體的左上角。定位對話框

這裏是我的代碼有

agreeDialog = $j('#termsOfAgreementConfirm').dialog({ 
       modal: true, 
       autoOpen:false, 
       resizable:false, 
       width : 1000, 
       height :400, 
       stack:false, 
       closeOnEscape:false, 
       title:"Terms of Usage", 
       open: function(event, ui) { 

        $j('.ui-dialog-titlebar-close').hide(); 

       }, 
       buttons: { 

        Disagree: function() { 
         disagree.dialog('open'); 
         disagree.dialog('moveToTop'); 

        }, 


        Agree: function() { 
         $j(this).dialog('close'); 
         $j.cookie("agree", "Y"); 
         new Ajax.Request('/myballback/user/ajaxUpdateAgreementForUser', 
           { 
            onSuccess:function(resp) { 
            }, 
            onError: function(resp) { 
             alert("Error:" + resp.toJSON()); 
             return; 
            }, 
            asynchronous:true, 
            evalScripts:true 
           }); 
         $j(this).dialog('close'); 

        } 
       } 
      }); 


var x = jQuery(agreeDialog).position().left + jQuery(agreeDialog).outerWidth(); 
      var y = jQuery(agreeDialog).position().top - jQuery(document).scrollTop();    //agreeDialog.dialog({width:width, height:height ,position:[posX, posY]}); 


      agreeDialog.dialog('option', 'position',[x, y]); 

回答

3

你爲什麼不嘗試使用:

$(".selector").dialog("option", "position", ['left','top']); 

$(".selector").dialog("option", "position", 'center'); 
+0

當我做了左上角。它所做的是移動對話框,但將它移動到瀏覽器窗口的左邊緣,但對話框仍位於頁面的底部。我的div在對話中的定義方式和位置有什麼關係? – allthenutsandbolts 2011-12-15 20:43:53

+0

你自己的CSS規則是否對``div``起作用? – jabclab 2011-12-15 20:47:48

2

您可以使用jQuery的dialogposition財產爲了達成這個;是這樣的:

$j('#termsOfAgreementConfirm').dialog({ 
    ... 
    position: "center", 
    .... 
}); 

jQueryUI docs

指定應顯示該對話框。可能的值:

1)表示視口中的位置的單個字符串:'中心','左','右','頂部','底部'。

2)含有一個X的陣列,y座標中像素對由左,視口的頂端角偏移(例如[350100])

3)包含x,y位置的字符串值的陣列(例如[」右上角]]