jQuery UI dialog box not positioned center screen

2019-01-08 10:02发布

I have a jQuery dialog box that is meant to position in the middle of the screen. However, it seems slightly off-center vertically.

Here is the code:

$('#add_box').dialog({
    autoOpen: true,
    width: 300,
    modal: true,
    resizable: false,
    bgiframe:true
});

Any ideas why this won't center?

20条回答
Explosion°爆炸
2楼-- · 2019-01-08 10:33

to position the dialog in the center of the screen :

$('#my-selector').parent().position({
                    my: "center",
                    at: "center",
                    of: window
});
查看更多
一夜七次
3楼-- · 2019-01-08 10:36

Add this to your dialog declaration

my: "center",
at: "center",
of: window

Example :

$("#dialog").dialog({
       autoOpen: false,
        height: "auto",
        width: "auto",
        modal: true,
         my: "center",
         at: "center",
         of: window
})
查看更多
Root(大扎)
4楼-- · 2019-01-08 10:36

To fix this issue I made sure my body height was set to 100%.

body { height:100% }

This also maintains the center position while the user scrolls.

查看更多
神经病院院长
5楼-- · 2019-01-08 10:39

I had the same problem, which was fixed when I entered a height for the dialog:

$("#dialog").dialog({
    height: 500,
    width: 800
});
查看更多
一纸荒年 Trace。
6楼-- · 2019-01-08 10:43

For me jquery.dimensions.js was the Culprit

查看更多
Root(大扎)
7楼-- · 2019-01-08 10:47
$("#dialog").dialog({
       autoOpen: false,
        height: "auto",
        width: "auto",
        modal: true,
         my: "center",
         at: "center",
         of: window
})

This solution does work but only because of the newer jQuery versions ignoring this completely and falling back to the default, which is exactly this. So you can just remove position: 'center' from your options if you just want the pop up to be centered.

查看更多
登录 后发表回答