在open事件中更改jquery ui对话框的位置 [英] Change the position of a jquery ui dialog in the open event

查看:71
本文介绍了在open事件中更改jquery ui对话框的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm having trouble changing the position of a jQuery-ui dialog box.

我正在做的是将图像加载到open事件中的对话框中.由于图像的高度未知,因此对话框不再位于窗口的中心.因此,我在加载图像后也对其进行了重新定位,但是重新定位似乎被忽略了.

What I'm doing is loading an image into the dialog box within the open event. Because of the unknown height of the image, the dialog box is no longer centred in the window. So I also reposition it after loading the image, but the repositioning seems to be ignored.

但是,如果我在重新定位之前添加了警报,则它可以正常工作,因此很明显,此处出现了某种timimg问题.

But, if I add an alert before the repositioning, it works fine, so clearly some sort of timimg issue is in play here.

对此有何解决方法?

我的代码的相关部分是:

The relevant bit of my code is:

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

推荐答案

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

You have to wait for the image to load before repositionning:

$( "#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/片段/jquery/修复加载即缓存图像/

用于IE8缓存图像加载事件修复.

for IE8 chached images load event fixing.

这篇关于在open事件中更改jquery ui对话框的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆