I reccomend using position fixed rather than absolute, since that will bind the popup to the window, and not the parent element.
when showing the popup you set the top and left properties to be the offset off the image plus what ever offset you want for the popup.
simple jquery:
popup.offset(image.offset());
I run this function (requires jquery). it set the position to be that of the anchor element, and adjust the popup if the element is too close to the bottom of the window:
function showPopup() {
var offset, difference;
offset = image.offset();
popheight = popup.height();
difference = Math.min(0, $(window).height() - (popup.height() + offset.top + 15))
// the diffrence betwenn the bottom edge of the popup, and the windows lower edge (+15 to add a bit of space)
if (difference < 0) {
offset.top += difference;
}
popup.offset(offset);
screenCover.show().click(hidePopup);
}
The screenCover in the last line is a fullscreen div I use to catch clicks out side the popup (since my the popup requires user interaction)
the hidePopup function:
function hidePopup() {
popup.hide();
screenCover.off('click').hide();
}