Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery UI dialog positioning

I am trying to use the jQuery dialog UI library in order to position a dialog next to some text when it is hovered over. The jQuery dialog takes a position parameter which is measured from the top left corner of the current viewport (in other words, [0, 0] will always put it in the upper left hand corner of your browser window, regardless of where you are currently scrolled to). However, the only way I know to retrieve the location is of the element relative to the ENTIRE page.

The following is what I have currently. position.top is calculated to be something like 1200 or so, which puts the dialog well below the rest of the content on the page.

$(".mytext").mouseover(function() {     position = $(this).position();     $("#dialog").dialog('option', 'position', [position.top, position.left]); } 

How can I find the correct position?

Thanks!

like image 969
Wickethewok Avatar asked Apr 13 '09 16:04

Wickethewok


People also ask

How to position jQuery dialog?

Syntax: $( ". selector" ). dialog({ position: { my: "left top", at: "left bottom", of: button } });

What is UI positioning?

The jQuery UI . position() method allows you to position an element relative to the window, document, another element, or the cursor/mouse, without worrying about offset parents.

What is jQuery ui dialog?

The jQuery UI dialog method is used to create a basic dialog window which is positioned into the viewport and protected from page content. It has a title bar and a content area, and can be moved, resized and closed with the 'x' icon by default.

How do you check if jQuery dialog is initialized?

A more reliable solution would be to add your own existence indicator at dialog initialization: $("#popup"). attr("_dialogInitialized", "yes"). dialog( { ... } )


2 Answers

As an alternative, you could use the jQuery UI Position utility e.g.

$(".mytext").mouseover(function() {     var target = $(this);     $("#dialog").dialog("widget").position({        my: 'left',        at: 'right',        of: target     }); } 
like image 175
Brian M. Hunt Avatar answered Sep 22 '22 23:09

Brian M. Hunt


Thanks to some answers above, I experimented and ultimately found that all you need to do is edit the "position" attribute in the Modal Dialog's definition:

position:['middle',20], 

JQuery had no problems with the "middle" text for the horizontal "X" value and my dialog popped up in the middle, 20px down from the top.

I heart JQuery.

like image 20
user1288395 Avatar answered Sep 24 '22 23:09

user1288395