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!
Syntax: $( ". selector" ). dialog({ position: { my: "left top", at: "left bottom", of: button } });
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.
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.
A more reliable solution would be to add your own existence indicator at dialog initialization: $("#popup"). attr("_dialogInitialized", "yes"). dialog( { ... } )
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 }); }
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With