Edit: look here for a live example: http://jsfiddle.net/jPUbh/
Change your html to this:
<div id="placeholder" style="position:relative">
<div id="placeholder-text">Hover over me to show the menu here</div>
<div style="position: absolute; display: none; top: 20px; left: 0px" id="menu">
</div>
</div>
and your js:
$("#placeholder-text").mouseover(showMenu);
var showMenu = function(ev) {
$("#menu").show();
}
When the parent element (int this case div # placeholder) is set to relative, the block-level elements inside it that are set to absolute will be positioned relative to the parent. Therefore, change the top and left properties of the div # menu to suit your needs. Now, if div # placeholder moves, then the div # menu moves with it.
And your JS is much cleaner with CSS!
The ides are completely universal, change them as you wish :-) And update JS if you change them in html!
source
share