views:

23

answers:

3

I am working om a menu bar, each menu bar item is an image, when user places mouse over menu item a div with submenu will appear.

I want to place div directly under the appropriate image item (no space, and div will hover above all elements), with right side alignment, meaning the right top corner of div should be under bottom right corner of image.

Because I can't and don't want to hard code position of divs, i want to do it dynamically.

For now I have this:

$('img').each(function(){                     
   jQuery(this).mouseenter(function(){
     var menuItem = $('#' + this.id + '_menu'); //get the needed div 
     var imgRight = this.offset() + this.width();


   });
 });
+1  A: 

The offset() method has top and left properties, you need use them, example:

var imgRight = this.offset().left + this.width();
var imgTop = this.offset().top + this.height();

After that, you will have to give the absolute positioning to the DIVs to place them below the images:

menuItem.css({
  position:'absolute',
  top: imgTop,
  left: imgLeft,
  zIndex:5000
});

So your code becomes:

$('img').each(function(){                     
   jQuery(this).mouseenter(function(){

   var menuItem = $('#' + this.id + '_menu'); //get the needed div 
   var imgRight = this.offset().left + this.width();
   var imgTop = this.offset().top + this.height();

     menuItem.css({
       position:'absolute',
       top: imgTop,
       left: imgLeft,
       zIndex:5000
     });

     // now show the corresponding div
     menuItem.show('slow');

   });
});

More Info:

http://api.jquery.com/offset/

Sarfraz
and how to place div under the image? Do i need to calculate the position or there is a better way?
ilann
@ilann: See my updated answer please.
Sarfraz
do I need z-index for hover?
ilann
@ilann: Yes you need them with a greater value, i have added that in my answer.
Sarfraz
the left position of the div is not correct. the div's right border is not aligned with img's right
ilann
@@ilann:: You should post your working demo in something like jsbin.com for us to see.
Sarfraz
i will do it but latter :)
ilann
never mind, got it to work. thanks
ilann
@ilann: You are welcome :)
Sarfraz
A: 

You shouldn't have to hard code or calculate the position of these items. Any of the following CSS rules should achieve your goal: position: relative; right: 0 or float: right:.

It'd be good to see some of your markup for additional testing. www.jsfiddle.net is a great resource for this.

Jason McCreary
A: 

there are 2 ways to do this: the correct-way or the cheat way... The correct way: you need to get the top and clientheight of the actuating object - clientheights no prob just call it - but the top means you must get the to of all the parent objects too - use this:

function J_pos(o)

{ var x,y; y=o.offsetTop; x=o.offsetLeft; o=o.offsetParent; while(o) { y+=o.offsetTop; x+=o.offsetLeft; o=o.offsetParent; } return [x,y]; };

Now w top and client height you do this: < div style=top:"+(p[0]+obj.clientHeight)+";left:"+p[1]

The cheat-way (not so dynamic - but quick): put a tag like a span around the actuating (mouseover) object - make it position relative - place a div inside it - make div id=ABC position:absolute;left:0;display:none now when mouseover make document.getElementById("ABC").style.display="" and bottom:0 - boom baby dusted. Downside to this is you have to manually do it for each instance - but if you only got 3 or so well bingo

Johnny Darvall