When you use jquery UI dialog, all works well, except for one thing. When the browser is resized, the dialog just stays in it's initial position which can be really annoying.

You can test it out on: http://jqueryui.com/demos/dialog/

点击"模态对话框"示例并调整浏览器大小.

我希望能够让对话框在浏览器调整大小时自动居中.

推荐答案

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. ​

Jquery相关问答推荐

如何在不使用点击事件 bootstrap 程序模式的情况下使用弹出窗口 bootstrap 程序显示用户详细信息?

使用 jQuery,当用户仍在编辑该字段时,如何将文本字段的第一个字母大写?

jQuery Mobile:动态添加内容的标记增强

JavaScript 中的循环计时器

使用 jQuery 发送 JSON 数据

jQuery增量读取AJAX流?

页面滚动后,jQuery可拖动在错误的位置显示助手

如何立即启动 setInterval 循环?

为什么不推荐$().ready(handler)?

如何使用 jquery 动态更改 iframe 中的内容?

如何签入用户已在 Google recaptcha 中选中复选框的 js?

测试两个元素是否相同

Rails 无法正确解码来自 jQuery 的 JSON(数组变成带有整数键的散列)

如何让 JavaScript/jQuery Intellisense 在 Visual Studio 2008 中工作?

jQuery表格行中的每个循环

如何将参数传递给 jQuery 中的事件处理程序?

默认情况下如何将tinymce粘贴为纯文本

如何使用 jquery 使下拉列表只读?

jQuery 使用 AND 和 OR 运算符按属性 Select

jquery在cookie中保存json数据对象