12

How can I position the jquery UI dialog SPECIFICALLY, so that it goes to a position not defined by center, top, etc.

Thanks, I have tried to be as specific as posible.

H Bellamy
  • 22,405
  • 23
  • 76
  • 114

3 Answers3

25

Using the position option : http://jqueryui.com/position/

Specifies where the dialog should be displayed. Possible values:

1) a single string representing position within viewport: 'center', 'left', 'right', 'top', 'bottom'.
2) an array containing an x,y coordinate pair in pixel offset from left, top corner of viewport (e.g. [350,100])
3) an array containing x,y position string values (e.g. ['right','top'] for top right corner).

For example : $( ".selector" ).dialog( "option", "position", [350,100] );

Margo
  • 672
  • 3
  • 13
  • 30
Guillaume Cisco
  • 2,859
  • 24
  • 25
17

This isn't an exact answer to your question, but you can mix 'top' with pixel values, like this:

$('#widget').dialog({
  position: ['top', 100]
});

This will position the dialog centered along the X axis, 100 pixels down from the top.

camainc
  • 3,750
  • 7
  • 35
  • 46
  • Just so you know, the `'top'` component isn't doing anything in this example. When you pass an array to the position option, it is expecting an [`[x,y]` coordinate pair](http://api.jqueryui.com/dialog/#option-position). The string can't get parsed as an integer, so the default horizontal positioning stays as center. You can replace the `'top'` part with any string and it will still work the same way: [**Fiddle Demo**](http://jsfiddle.net/KyleMit/978UW/) – KyleMit Nov 27 '13 at 01:03
7

If you want to use absolute positioning, the dialog's position option is what you need. If you need to position relative to other elements, there's another easy technique you use, jquery UI's $('selector').position(options); (seen at: http://jqueryui.com/demos/position/)

For example:

// div to position against
var $div = $('#someDiv');

// Open dialog (positioning won't work on hidden elements)
$dialog.dialog('open');

// position newly opened dialog (using its parent container) below $div.
$dialog.dialog('widget').position({
  my: "left top",
  at: "left bottom",
  of: $div
});
Michael R
  • 1,753
  • 20
  • 18
  • @Cheekysoft Your comment seems to imply that this form is deprecated. This is not using an array, but an object parameter, which is the desired parameter type for the position method. You are correct in saying that the array and string parameter forms are deprecated. – Michael R Dec 23 '13 at 20:35