views:

46

answers:

1

Basically I would like the dialog to remain centered when resizing the window (or switching to full screen window).

Also the dialog is draggable, and ideally I'd prefer if it remained in it's dragged position on the page when resizing the window. But if it resets back to the center then I would settle for that too.

I tried setting "position: relative" on the ".ui-dialog" class, however this produces a big empty gap at the bottom of the page; I have been unable to figure out what is causing this using the DOM inspector tools.

+2  A: 

You can do it like this:

$("#dialog").dialog({
    drag: function() {
        $.data(this, 'dragged', true);
    },
    open: function() {
        $.data(this, 'dragged', false);        
    }
});
$(window).resize(function() {
    $(".ui-dialog-content").each(function() {
        if(!$.data(this, 'dragged'))
            $(this).dialog('option', 'position', 'center');
    });
});

You can try out a demo here, this re-centers the dialog on window.resize only if you haven't dragged it since it was open. When we open, we set a the data for dragged to false, if we drag it, set it to true...and if it's still false (hasn't been dragged yet), the re-center happens, otherwise we leave it alone in it's current position.

Good question/idea on the dragging detection by the way, this would improve an application I'm currently working on, going to use this myself :)

Nick Craver
Thanks - that seems to work OK, but still not quite there.I forgot to mention my container div is centered on the page (margin: 0 auto). This is why the problem seems to occur. Whilst the container div always stays centered on the page, the dialog does not stay relative to the container.
GSTAR
@GSTAR - If that's the case you'll need to change `$(this).dialog('option', 'position', 'center');` with code that positions the dialog as you want...it only has so many built-in positions, outside of that you'll need to adjust it manually. You can see the valid position options here: http://jqueryui.com/demos/dialog/#option-position
Nick Craver
Yeah I have tried the position options, however the problem is that it positions the dialog in relation to the window rather than the container div. I.E. if I specify "position: [350, 150]" those values refer to the window and not the container.
GSTAR
BTW if I do something like "left: 50%" on the ".ui-dialog" class it does not allow me to drag the dialog left or right on the page.
GSTAR
@GSTAR - You can use [`.offset()`](http://api.jquery.com/offset/) for the container and add those values in :) Use `.left` and `.top`, that'll get you the top left corder of the container, and add any amount you want to that.
Nick Craver
Cheers Nick - could really do with a sample code here, as I cannot make any sense of the example on that page..
GSTAR
Do I still need to use the window.resize function? As this seems to cause IE 6/7 to hang..
GSTAR
Nick? Anyone...?
GSTAR