I have a hidden DIV which contains a toolbar-like menu.
I have a number of DIVs which are enabled to show the menu DIV when the mouse hovers over them.
Is there a built-in function which will move the menu DIV to the top right of the active (mouse hover) DIV? I'm looking for something like $(menu).position("topright", targetEl);
jQuery position() MethodThe position() method returns the position (relative to its parent element) of the first matched element. This method returns an object with 2 properties; the top and left positions in pixels.
position: absolute will position the element by coordinates, relative to the closest positioned ancestor, i.e. the closest parent which isn't position: static . Have your four divs nested inside the target div, give the target div position: relative , and use position: absolute on the others.
First set position of the parent DIV to relative (specifying the offset, i.e. left , top etc. is not necessary) and then apply position: absolute to the child DIV with the offset you want. It's simple and should do the trick well.
Absolute In position: relative , the element is positioned relative to itself. However, an absolutely positioned element is relative to its parent. An element with position: absolute is removed from the normal document flow. It is positioned automatically to the starting point (top-left corner) of its parent element.
tl;dr: (try it here)
If you have the following HTML:
<div id="menu" style="display: none;"> <!-- menu stuff in here --> <ul><li>Menu item</li></ul> </div> <div class="parent">Hover over me to show the menu here</div>
then you can use the following JavaScript code:
$(".parent").mouseover(function() { // .position() uses position relative to the offset parent, var pos = $(this).position(); // .outerWidth() takes into account border and padding. var width = $(this).outerWidth(); //show the menu directly over the placeholder $("#menu").css({ position: "absolute", top: pos.top + "px", left: (pos.left + width) + "px" }).show(); });
But it doesn't work!
This will work as long as the menu and the placeholder have the same offset parent. If they don't, and you don't have nested CSS rules that care where in the DOM the #menu
element is, use:
$(this).append($("#menu"));
just before the line that positions the #menu
element.
But it still doesn't work!
You might have some weird layout that doesn't work with this approach. In that case, just use jQuery.ui's position plugin (as mentioned in an answer below), which handles every conceivable eventuality. Note that you'll have to show()
the menu element before calling position({...})
; the plugin can't position hidden elements.
Update notes 3 years later in 2012:
(The original solution is archived here for posterity)
So, it turns out that the original method I had here was far from ideal. In particular, it would fail if:
Luckily, jQuery introduced methods (position()
and outerWidth()
) way back in 1.2.6 that make finding the right values in the latter case here a lot easier. For the former case, append
ing the menu element to the placeholder works (but will break CSS rules based on nesting).
NOTE: This requires jQuery UI (not just jQuery).
You can now use:
$("#my_div").position({ my: "left top", at: "left bottom", of: this, // or $("#otherdiv") collision: "fit" });
For fast positioning (jQuery UI/Position).
You can download jQuery UI here.
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