Be sure to take a look at the updates on http://jsfiddle.net/CBf4C/3/
To determine the position of the clicked element (utilize .position()
), retrieve the dimensions of the tooltip using .outerWidth()
and .outerHeight()
, and then perform calculations based on these values.
The provided code snippet is as follows:
$('a[title]').click(function(e) {
//fade the tooltip and display information
$('body').append('<div class="tooltip"><div class="tipBody"></div></div>');
tip = $(this).attr('title');
var tooltip = $('.tooltip'); // store a reference to the tooltip for later use
tooltip.fadeTo(300, 0.9).children('.tipBody').html(tip);
// calculate the position of the tooltip
var el = $(this),
pos = el.position(), // obtain the position of the clicked element
w = el.outerWidth(), // get the width of the clicked element in order to determine its center
newtop = pos.top - tooltip.outerHeight(), // compute the top position of the tooltip
newleft = pos.left + (w / 2) - (tooltip.outerWidth() / 2); // calculate the left position of the tooltip
// set the position
$('.tooltip').css('left', newleft);
$('.tooltip').css('top', newtop);
hideTip = false;
});