Edit for updated question: Since you're using <area>
it's a different story, and fetching from the coords
attribute is much easier, like this:
var position = $(this).attr('coords').split(',');
x = +position[0] - 50;
y = +position[1] - 170;
The offsets are just to account for the hard-coded width/height of the tooltip itself. In addition to the above, you want to use top
and left
rather than margin-top
and margin-left
. Also to account for the #content
<div>
's position in the page, give it a relative
position for the tooltip to sit in, like this:
#content { position: relative; }
Then...instead of .after()
, use .append()
so it gets added inside that parent.
You can test the result here.
For original question:
The object .position()
returns has top
and left
properties...but you want .offset()
here anyway (it's relative to the document, where .position()
is relative to the offset parent), so it should look like this:
var position = $(this).offset(),
x = position.left,
y = position.top; //not right!
Or this:
var position = $(this).offset();
var x = position.left;
var y = position.top;
...but without a single var
comma-separated statement, or a var
on each line, you're also creating (or trying to) global variables, which will blow up in IE.