How can I position my jQuery dialog to center?

2019-01-11 05:15发布

I have tried following code, but it only positions dialogs left upper corner position to center, and that makes element to be aligned to right. How can I center dialog to real center which counts elements width, so that center line will cut dialog to 50% 50% halfs?

$('.selector').dialog({ position: 'center' });

http://docs.jquery.com/UI/Dialog#option-position

21条回答
Ridiculous、
2楼-- · 2019-01-11 05:53

If you are using individual jquery files or a custom jquery download either way make sure you also have jquery.ui.position.js added to your page.

查看更多
Bombasti
3楼-- · 2019-01-11 05:53

For Win7/IE9 environment just set in your css file:

.ui-dialog {
   top: 100px;
   left: 350px !important;
}
查看更多
劫难
4楼-- · 2019-01-11 05:55

To fix center position, I use:

open : function() {
    var t = $(this).parent(), w = window;
    t.offset({
        top: (w.height() / 2) - (t.height() / 2),
        left: (w.width() / 2) - (t.width() / 2)
    });
}
查看更多
Emotional °昔
5楼-- · 2019-01-11 05:57

The latest jQuery UI has a position component:

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

Doc: http://jqueryui.com/demos/position/
Get: http://jqueryui.com/download

查看更多
我欲成王,谁敢阻挡
6楼-- · 2019-01-11 05:58

Following position parameter worked for me

position: { my: "right bottom", at: "center center", of: window },

Good luck!

查看更多
仙女界的扛把子
7楼-- · 2019-01-11 06:00

Because the dialog need a position, You need to include the js position

<script src="jquery.ui.position.js"></script>
查看更多
登录 后发表回答