JQuery tooltip – follow mouse

jquerytooltip

I'm working a but of code that is supposed to add a "tooltip" that follows the mouse as you hover over a div. (it's supposed to do this to 3 sets of 8 divs)

The content of the tooltip is placed in a span, which in turn is inside the 'li' that has the .hover and .mousemove attached to it.

A "live example" of what I'm trying to accomplish can be seen here. (still in development, tooltip should be seen when hovering the red bars).

http://staging2.e2e.be/ciber4/15-werfzones–de-werven-op-een-rij/zoek-op-planning

Basically, i just need the tooltip to be relative the red blocks, and not relative to the body?

Also made a fiddle with the corresponding code.

http://jsfiddle.net/JDST8/

Best Answer

You can use $(selector).position() to get the X/Y coords of the bar. It returns an object with "top" and "left" keys containing the element's full page offset.

Here's a fiddle with a working example: http://jsfiddle.net/xJSMu/

PS: You got your x and y offsets the wrong way round in your fiddle - by names anyway.

PPS: I've also added some selector caching (Rule of thumb: if you use it more than once, cache it instead of calling the jQuery constructor again).