在open事件中更改jquery ui对话框的位置

时间:2013-01-07 10:29:11

标签: jquery-ui

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

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

但是,如果我在重新定位之前添加警报,它可以正常工作,所以很明显这里有一些timimg问题。

有没有解决这个问题?

我的代码的相关位是:

$( "#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});
  },
  ...

1 个答案:

答案 0 :(得分: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缓存图像加载事件修复。