Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I position my jQuery dialog to center?

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

like image 696
newbie Avatar asked Dec 03 '09 12:12

newbie


People also ask

What is dialog in jquery?

A dialog box is a floating window with a title and content area. This window can be moved, resized, and of course, closed using "X" icon by default. jQueryUI provides dialog() method that transforms the HTML code written on the page into HTML code to display a dialog box.


2 Answers

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

like image 89
WhyNotHugo Avatar answered Oct 13 '22 06:10

WhyNotHugo


I'm pretty sure you shouldn't need to set a position:

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

should center by default.

I did have a look at the article, and also checked what it says on the official jquery-ui site about positioning a dialog : and in it were discussed 2 states of: initialise and after initialise.

Code examples - (taken from jQuery UI 2009-12-03)

Initialize a dialog with the position option specified.

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

Get or set the position option, after init.

//getter var position = $('.selector').dialog('option', 'position'); //setter $('.selector').dialog('option', 'position', 'top'); 

I think that if you were to remove the position attribute you would find it centers by itself else try the second setter option where you define 3 elements of "option" "position" and "center".

like image 44
Luke Duddridge Avatar answered Oct 13 '22 07:10

Luke Duddridge