如何将 jquery 对话框居中以显示在浏览器当前可视区域的中心

How do I center a jquery dialog to show at the center of the current viewable area of the browser?

本文关键字:浏览器 可视 区域 显示 jquery 对话框      更新时间:2023-09-26

我能够使用默认位置(即页面的正中心)使对话框居中。但是,当页面高度比浏览器窗口长时,当对话框首次打开时,它会将整个页面滚动回其中心。

我想将对话框预设为相对于屏幕而不是页面的中心。

我相信这就是我用来初始化它的方法:

dialogDiv.dialog(
{
    title: "Edit Rating",
    width: 'auto',
    height: 'auto',
    modal: true,
    resizable: false
}); 

我认为这可能不需要太多努力。

$("#dialog").dialog();

根据本文,它默认为中心:

http://jqueryui.com/dialog/#default

我也试过这个并工作

这甚至更好:

$("#myDialog").position({
my: "center",
at: "center",
of: window
});

示例代码:http://jqueryui.com/demos/position/

    $("#dialog").dialog("option", "position", "center")