3

I am using JQueryUI Dialog and created this function below:

<script>
    $(document).ready(function() {
        $('#dialog').dialog({
                autoOpen:false,
                width:100,
                height:200,
                position:[2250,50]
           });


        $('.class112').click(function() {
           var msg = $(this).attr('id');
           $('#dialog').load('classSource/' + msg + '.html', function() {
           $('#dialog').dialog('open');
           });
        });
    });
</script>

and HTML code:

<p class="class112" id="class1">click!</p>

<div id="dialog" style="display: none;"></div>

It works fine but it creates dialog after calling 'open' function regardless of positioning. Lets say my computer's screen has x:1280 and y:760 pixels and body width and height are set to 3000px each in CSS file. Whenever 'open' function for dialog is fired, it doesn't get X position from when it was initialized before, remember :

position:[2250,50]

As a result it creates dialog at very right of the window not exactly at where X was declared to be. But Y comes out correctly because 50px is within the range of my screen resolution.

All I want is when I click 'click!' paragraph, I want the dialog box to appear in the initialized position and I might be able to see it after horizontal scroll. What should I do?

2 Answers 2

7

I think the Dialog widget uses the Position utility to position itself. Looking at the documentation, you may be able to use the collision option to control this behavior:

When the positioned element overflows the window in some direction, move it to an alternative position. Similar to my and at, this accepts a single value or a pair for horizontal/vertical, eg. "flip", "fit", "fit flip", "fit none".

http://jqueryui.com/demos/position/#option-collision

EDIT:

Yes, looking at the source of 1.8.16 the default option is "fit":

    position: {
        my: 'center',
        at: 'center',
        collision: 'fit',
        // ensure that the titlebar is never outside the document
        using: function(pos) {
            var topOffset = $(this).css(pos).offset().top;
            if (topOffset < 0) {
                $(this).css('top', pos.top - topOffset);
            }
        }
    },

You will want to override that to "none" probably.

3
  • Ok here is another question, when I scroll the window horizontally and fire the .dialog('open') function, it creates dialog in x+(horizontal scrolling amount). I want it only be created in specified x and y position. Can you please help me?
    – buri kuri
    Commented Nov 21, 2011 at 3:41
  • @burikuri Maybe instead of specifying x and y, you will have to also factor in the scroll amount and subtract it. Feel free to post a new question as well.
    – Dave L.
    Commented Nov 21, 2011 at 3:53
  • Yes I thought subtracting xscroll amount. Do you know how to get xscroll amount?
    – buri kuri
    Commented Nov 21, 2011 at 4:18
3

This should do it jQuery above 1.8:

//Overriding collision detection default settings of the jQuery dialog.
$.extend($.ui.dialog.prototype.options.position, { collision: 'none' });

Unfortunately, there is no way to change this setting on a separate dialog element, because of the way the '_position' function works in the source code. Meaning the following will NOT work:

$('#elem').dialog({
position: {
    collision: 'none'
    }
});

Though, you can also mess with source code of the UI dialog, if the future maintenance of the code is not a problem.

2
  • Verified. In 1.8.21 works great. Strange, but with no result in 1.8.9.
    – BasTaller
    Commented Mar 22, 2013 at 10:34
  • Thanks a lot @Andrei , it resolved my issue
    – Ron
    Commented Feb 25, 2021 at 21:15

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