33

I have a dialog that gets filled by an ajax call. I want to limit the max-height of dialog and also allow it to be scroll-able if this max-height is exceeded. The code below does exactly what I want.

The catch is I can not move the top of the dialog from the top position. I can move it left and right. I can't use center either as the dialog is displayed in a large scroll-able window. If I use firebug I can adjust the top property but cannot find where it is being set to zero.

$("#your-dialog-id").dialog({
    open: function(event, ui) {
        $(this).css({'max-height': 500, 'overflow-y': 'auto'});
    },
    autoOpen:false,
    modal: true,
    resizable: false,
    draggable: false,
    width: '690',
    closeOnEscape: true,
    position: 'top'
});

I want to adjust the dialog's y position so it is 20px from the top of the window. Any idea what I can do?

5
  • I don't understand what the actual question is.
    – j08691
    Commented Jun 27, 2012 at 16:22
  • @j08691 I want to adjust the dialog y position so it draws 20px from the top of the window. I can only change the y position to top, center or bottom. Commented Jun 27, 2012 at 16:38
  • Wouldn't a top margin of 20px accomplish that?
    – j08691
    Commented Jun 27, 2012 at 16:41
  • 2
    unfortunately no: but this sorted the problem jsfiddle.net/chrisloughnane/wApSQ/3 position: ['center',20] Commented Jun 27, 2012 at 23:02
  • 1
    Your solution worked for me! Thanks. I have been looking for this as well.
    – M.Ob
    Commented Jan 9, 2013 at 12:15

3 Answers 3

71

Changing the last value solved the problem:

position: ['center',20] 

http://jsfiddle.net/chrisloughnane/wApSQ/3

5
  • This ia the perfect one
    – M Gaidhane
    Commented Feb 5, 2014 at 9:09
  • Would the points fishers leave this answer alone. It does not need your attention or corrections. Commented Dec 8, 2015 at 11:50
  • can you explain how this is working , i couldn't find any documentation regarding this ? Commented Mar 29, 2016 at 10:07
  • @varunsinghal65 I have not been able to find any documentation on this either. Commented Apr 12, 2016 at 13:34
  • @HimalayaGarg The original question is 4+ years old,jQuery has moved on a lot since then and I can see below you already found an up to date solution. :) Commented Jan 8, 2018 at 10:56
12

The easiest way is:

$("#dialog").dialog({ position: { my: "center", at: "top" } });
1
  • For anyone that this didn't work for, in order to get it to position horizontally in the center and vertically at the top, this worked for me. I don't really understand why though: $("#dialog").dialog({ position: { of: window, my: "top", at: "top" } }); (jQueryUI version 1.12.1)
    – Gabe
    Commented May 14, 2019 at 12:46
5

using Jquery UI 1.11.4

        var Y = window.pageYOffset;

        $( "#dialogJQ" ).dialog({
            modal: true,
            closeOnEscape: false,                
            width:'auto',
            dialogClass: 'surveyDialog',
            open: function(event, ui) {
                $(this).parent().css({'top': Y+20});
            },
        });
2
  • This is the correct version for modern jQuery UI. Note for width and height "auto" is now the solution rather than autoResize:true, I mention as many, like me, will find themselves trying to align the dialog and resize dynamically (size and margin and commonly done together). Commented Mar 12, 2018 at 10:41
  • This did not work for me with v1.12.1. It initially set the position where I indicated, then it immediately visually moves back to the spot it was before. Obviously this implies something is running after the open function.
    – Mmm
    Commented Aug 24, 2021 at 22:08

Not the answer you're looking for? Browse other questions tagged or ask your own question.