Positioning jQuery dialog
Asked Answered
V

8

15

I want to position my jQuery dialog x-pixels away from the right border of the browser. Is this anyhow possible?

http://jqueryui.com/demos/dialog/

The position option doesn't seem to have that kind of setup, but is there any other way to do it?

Vullo answered 16/1, 2010 at 23:59 Comment(1)
possible duplicate of jQuery UI dialog positioningErechtheus
A
10

If you make your dialog box's position:absolute, then its taken about of the regular page flow, and you can use the left and top property to place it anywhere on the page.

$('.selector').dialog({ dialogClass: 'myPosition' });

and define the myPosition css class as:

.myPosition {
    position: absolute;
    right: 200px; /* use a length or percentage */
}

You can set the top, left, right, and bottom properties for myPosition using either a length such as in pixels or percentage.

Anacoluthia answered 17/1, 2010 at 0:10 Comment(1)
Don't forget that it will position it relative to the first parent element whose position is specified. If none are found, it is positioned relative to the body tag. Therefore, for a dialog like this, it's important to always append it to the body tag.Mccombs
F
16

This keeps dialog div in fixed position

this works for me in IE FF chrome and safari

jQuery("#dialogDiv").dialog({
    autoOpen: false, 
    draggable: true,
    resizable: false,
    height: 'auto',
    width: 500,
    modal: false,
    open: function(event, ui) {
        $(event.target).parent().css('position', 'fixed');
        $(event.target).parent().css('top', '5px');
        $(event.target).parent().css('left', '10px');
    }

});

when you want dialog box open just call

$('#dialogDiv').dialog('open');
Fortress answered 21/2, 2012 at 16:2 Comment(1)
+1 this worked better for me, because I need to be able to move the form around. Setting the position with a css class makes it frozen.Grane
A
10

If you make your dialog box's position:absolute, then its taken about of the regular page flow, and you can use the left and top property to place it anywhere on the page.

$('.selector').dialog({ dialogClass: 'myPosition' });

and define the myPosition css class as:

.myPosition {
    position: absolute;
    right: 200px; /* use a length or percentage */
}

You can set the top, left, right, and bottom properties for myPosition using either a length such as in pixels or percentage.

Anacoluthia answered 17/1, 2010 at 0:10 Comment(1)
Don't forget that it will position it relative to the first parent element whose position is specified. If none are found, it is positioned relative to the body tag. Therefore, for a dialog like this, it's important to always append it to the body tag.Mccombs
A
9

Most of these answers seemed to be workarounds to me, and I wanted to find the official jQuery way to do it. After reading through the .position() docs, I found that it can indeed be done in the initialization of the jQuery widget:

$("#dialog").dialog({
    title:title,
    position:{my:"right top",at:"right+100 top+100", of:"body"},
    width:width,
    height:height
})

Where the +100 is the distance from the right and top respectively

Antaeus answered 19/6, 2015 at 23:11 Comment(0)
S
5

I understand the answer is already accepted but just in case if any one need more info: http://salman-w.blogspot.co.uk/2013/05/jquery-ui-dialog-examples.html

$(function() {
            $("#dialog").dialog({
                position: {
                    my: "right bottom",
                    at: "right bottom",
                    of: window
                }
            });
        });
Schoolfellow answered 11/2, 2015 at 13:52 Comment(0)
P
4

with this code u can specify ur top and left position:

$('#select_bezeh_window').dialog({
    modal:true,
    resizable:false,
      draggable:false,
      width:40+'%',
      height:500 ,
      position:{
          using: function( pos ) {
                $( this ).css( "top", 10+'px' );
                $( this ).css( "left", 32+'%' );
          }
       }
 });
Petigny answered 3/12, 2016 at 19:57 Comment(0)
P
3

look here: http://jqueryui.com/demos/dialog/#option-position

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');
Piranesi answered 17/1, 2010 at 0:2 Comment(0)
E
2

This worked for me to display the dialog at the top-right corner with 10px offset: position: "right-10 top+10":

$( "#my-dialog" ).dialog({
    resizable: false,
    height:"auto",
    width:350,
    autoOpen: false,
    position: "right-10 top+10"
});
Estrange answered 4/3, 2013 at 12:21 Comment(0)
R
0

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)
    });
}
Radicle answered 2/8, 2013 at 14:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.