如何在调整浏览器大小时自动定位 jQueryUI 对话框?

当您使用 jquery UI 对话框时,除了一件事之外,其他都可以正常工作。 当浏览器调整大小时,对话框只是停留在它的初始位置,这真的很烦人。

你可以在以下几个方面进行测试: Http://jqueryui.com/demos/dialog/

单击“模式对话框”示例并调整浏览器大小。

当浏览器调整大小时,我希望能够让对话框自动居中。 这是否可以用一种有效的方式来完成我的应用程序中的所有对话框?

68859 次浏览

Setting the position option will force this, so just use the same selector covering all your dialogs where I use #dialog here (if it doesn't find them no action is taken, like all jQuery):

jQuery UI before 1.10

$(window).resize(function() {
$("#dialog").dialog("option", "position", "center");
});

jQuery UI 1.10 or higher

$(window).resize(function() {
$("#dialog").dialog("option", "position", {my: "center", at: "center", of: window});
});

Here's that same jQuery UI demo page adding only the code above, we're just adding a handler to the window's resize event with .resize(), so it triggers the re-center at the appropriate time. ​

A more comprehensive answer, which uses Nick's answer in a more flexible way can be found here.

An adaption of the code of relevance from that thread is below. This extension essentially creates a new dialog setting called autoReposition which accepts a true or false. The code as written defaults the option to true. Put this into a .js file in your project so that your pages can leverage it.

    $.ui.dialog.prototype.options.autoReposition = true;
$(window).resize(function () {
$(".ui-dialog-content:visible").each(function () {
if ($(this).dialog('option', 'autoReposition')) {
$(this).dialog('option', 'position', $(this).dialog('option', 'position'));
}
});
});

This allows you to supply a "true" or "false" for this new setting when you create your dialog on your page.

$(function() {
$('#divModalDialog').dialog({
autoOpen: false,
modal: true,
draggable: false,
resizable: false,
width: 435,
height: 200,
dialogClass: "loadingDialog",
autoReposition: true,    //This is the new autoReposition setting
buttons: {
"Ok": function() {
$(this).dialog("close");
}
}
});
});

Now this dialog will always reposition itself. AutoReposition (or whatever you call the setting) can handle any dialogs that do not have a default position and automatically reposition them when the window resizes. Since you're setting this when you create the dialog, you don't need to identify a dialog somehow because the repositioning functionality becomes built into the dialog itself. And the best part is that since this is set per dialog, you can have some dialogs reposition themselves and others remain where they are.

Credit to user scott.gonzalez on the jQuery forums for the complete solution.

Alternatively to Ellesedil's answer,

That solution did not work for me straight away, So I did the following which is also dynamical but shortened version:

$( window ).resize(function() {
$(".ui-dialog-content:visible").each(function () {
$( this ).dialog("option","position",$(this).dialog("option","position"));
});
});

+1 for Ellesedil though

EDIT:

Much shorter version which works great for single dialogs:

$(window).resize(function(){
$(".ui-dialog-content").dialog("option","position","center");
});

It is not necessary for .each() to be used perhaps if you have some unique dialogs which you dont want to touch.

Another CSS-only option which works is this. The negative margins should equal half your height and half your width. So in this case, my dialog is 720px wide by 400px tall. This centers it vertically and horizontally.

.ui-dialog {
top:50% !important;
margin-top:-200px !important;
left:50% !important;
margin-left:-360px !important
}

Alternatively jQuery ui position can be used,

$(window).resize(function ()
{
$(".ui-dialog").position({
my: "center", at: "center", of: window
});
});

Hello everyone!

Vanilla JS solution:

(function() {
window.addEventListener("resize", resizeThrottler, false);


var resizeTimeout;


function resizeThrottler() {
if (!resizeTimeout) {
resizeTimeout = setTimeout(function() {
resizeTimeout = null;
actualResizeHandler();
}, 66);
}
}


function actualResizeHandler() {
$(".ui-dialog-content").dialog("option", "position", { my: "center", at: "center", of: window });
}
}());