1

我无法更改 jQuery-ui 对话框的位置。

我正在做的是将图像加载到打开事件中的对话框中。由于图像的高度未知,对话框不再居中在窗口中。所以我也在加载图像后重新定位它,但重新定位似乎被忽略了。

但是,如果我在重新定位之前添加一个警报,它工作正常,所以很明显某种时间问题在这里发挥作用。

有什么解决方法吗?

我的代码的相关位是:

$( "#dialog-message" ).dialog({
  open: function(e, ui){
    $("#theImage").attr("src","aRandomImage.jpg");
    alert(1);  // causes the next line to work properly
    $(this).dialog("option", "position", {my: "center", at: "center", of: window});
  },
  ...
4

1 回答 1

2

在重新定位之前,您必须等待图像加载:

$( "#dialog-message" ).dialog({
  open: function(e, ui){
    var $img = $("#theImage"), mydialog = $(this);
    $img.bind('load',function(){ // bind load event
        mydialog.dialog("option", "position", {my: "center", at: "center", of: window});
    });
    $img.attr("src","aRandomImage.jpg"); // start loading
  }

见: http ://css-tricks.com/snippets/jquery/fixing-load-in-ie-for-cached-images/

对于 IE8 chached 图像加载事件修复。

于 2013-01-07T10:36:07.080 回答