tags:

views:

57

answers:

2

I'm using jQuery.

How to deal with it easily?

+1  A: 

use offset();

var offset = $('#elem1').offset(); $('#elem2').css('top', offset.top );

http://docs.jquery.com/CSS/offset

FrankBr
A: 

Try this code:

var offset = $(<dom-element>).offset();
$(<div>).css({ top: (offset.top - $(<div>).height() - 20) + "px",
               left: offset.left });

For more information, check out the CSS documentation of jQuery.

Also, for most events you can call event.target to get the element generating event.

To get current mouse position use event.pageX and event.pageY. Note that the event must come from a mouse action: onmousemove, onmouseup, onmousedown, onclick, etc...

Xavi
How to do it according to event?
Where is the event coming from? In general you can call `event.target` to get DOM node generating the event.
Xavi
event has information about where the click happens,it's not the same as DOM node position.
Replace the above `$(<dom-element>).offset()` with `$(event.target).offset()`
K Prime
To get the current mouse position, you can use `event.pageX` and `event.pageY`.
Xavi