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条回答
姐就是有狂的资本
2楼-- · 2019-01-08 10:25

This issue is often related to opening the dialog via an anchor tag (<a href='#' id='openButton'>Open</a>) and not preventing the default browser behaviour in the handler e.g.

$('#openButton').click(function(event) {
   event.preventDefault();
   //open dialog code...
});

This usually removes the need for any positioning / scrolling plugins.

查看更多
狗以群分
3楼-- · 2019-01-08 10:26

I was facing the same issue of having the dialog not opening centered and scrolling my page to the top. The tag that I'm using to open the dialog is an anchor tag:

<a href="#">View More</a>

The pound symbol was causing the issue for me. All I did was modify the href in the anchor like so:

<a href="javascript:{}">View More</a>

Now my page is happy and centering the dialogs.

查看更多
forever°为你锁心
4楼-- · 2019-01-08 10:27

Just solved the same problem, the issue was that i did not imported some js files, like widget.js :)

查看更多
混吃等死
5楼-- · 2019-01-08 10:28

Simply add below CSS line in same page.

.ui-dialog 
{
position:fixed;
}
查看更多
在下西门庆
6楼-- · 2019-01-08 10:29

I had to add this to the top of my HTML file: <!doctype html>. I did not need to set the position property. This is with jQuery 3.2.1. In 1.7.1, that was not needed.

查看更多
可以哭但决不认输i
7楼-- · 2019-01-08 10:32

Are you adding jquery.ui.position.js to your page? I had the same problem, checked the source code here and realized I didn't add that js to my page, after that.. dialog magically centered.

查看更多
登录 后发表回答