Jquery ui 刷新jquery ui对话框位置

Jquery ui 刷新jquery ui对话框位置,jquery-ui,dialog,position,Jquery Ui,Dialog,Position,我正在使用jquery对话框。 此对话框的内容是动态的,因此打开对话框时高度会发生变化 $("#a_div").dialog({ width: 400 }); 该对话框最初显示在页面中央。但当高度变化不再是中心时 如何在不关闭并重新打开对话框的情况下刷新对话框的位置 谢谢您可以通过JQuery直接使用对话框的类来调整对话框的大小() JQueryUI对话框的基本结构如下: <div class="ui-dialog ui-widget ui-widget-content ui-corne

我正在使用jquery对话框。 此对话框的内容是动态的,因此打开对话框时高度会发生变化

$("#a_div").dialog({ width: 400 });
该对话框最初显示在页面中央。但当高度变化不再是中心时

如何在不关闭并重新打开对话框的情况下刷新对话框的位置


谢谢

您可以通过JQuery直接使用对话框的类来调整对话框的大小()

JQueryUI对话框的基本结构如下:

<div class="ui-dialog ui-widget ui-widget-content ui-corner-all ui-draggable ui-resizable">
   <div class="ui-dialog-titlebar ui-widget-header ui-corner-all ui-helper-clearfix">
      <span id="ui-dialog-title-dialog" class="ui-dialog-title">Dialog title</span>
      <a class="ui-dialog-titlebar-close ui-corner-all" href="#"><span class="ui-icon ui-icon-closethick">close</span></a>
   </div>
   <div style="height: 200px; min-height: 109px; width: auto;" class="ui-dialog-content ui-widget-content" id="dialog">
      <p>Dialog content goes here.</p>
   </div>
</div>

我希望这对您有所帮助。

您需要通过以下操作重新设置位置:

$("#a_div").dialog({
    position: { 'my': 'center', 'at': 'center' }
});
该位置在创建对话框时设置一次,但可以在之后更改(或者只是重新设置为相同的值,强制jQuery重新计算)


请参阅此演示:

如果要使用jquery ui用于初始定位的精确位置设置,可以从jquery ui代码中获取选项,并在任何时候重新定位对话框时再次使用它们

function refreshDialogPosition(id) {
    $("#" + id).position({
                        my: "center",
                        at: "center",
                        of: window,
                        collision: "fit",
                        // Ensure the titlebar is always visible
                        using: function (pos) {
                            var topOffset = $(this).css(pos).offset().top;
                            if (topOffset < 0) {
                                $(this).css("top", pos.top - topOffset);
                            }
                        }
                    });
}
这也将确保标题栏始终可见。否则,当使用包含大量内容的对话框时,标题栏将位于屏幕之外。(内容高度>窗口高度)


祝您愉快。

不推荐使用
position
的字符串形式。您应该改为使用
位置:{my:“center”,at:“center”,of:window}
。@cdmckay可以将当前的最佳实践添加到解决方案中!:)对我来说,在末尾添加
window
就达到了目的:
$(“#a#div”).dialog({position:{'my':'center',at':'center',of:window})
function refreshDialogPosition(id) {
    $("#" + id).position({
                        my: "center",
                        at: "center",
                        of: window,
                        collision: "fit",
                        // Ensure the titlebar is always visible
                        using: function (pos) {
                            var topOffset = $(this).css(pos).offset().top;
                            if (topOffset < 0) {
                                $(this).css("top", pos.top - topOffset);
                            }
                        }
                    });
}
refreshDialogPosition("YourDialogId");