Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery position() not working correctly in safari and chrome

I've seen this question posed once or twice before, but never with an answer that applies to my problem (as far as I know). I have a tooltip that appears when a link is clicked. I set the position of the tooltip based on the position of the link similar to this:

$('#tooltip').css('left', $(this).position().left);

This works great in FF, IE, etc., but in Chrome and Safari, the tooltip is always about 60 pixels or so further to the left than I want. I really don't like writing browser specific code, so is there any reason anyone knows that this would be happening? Thanks in advance.

UPDATE: I was able to fix this problem by removing the margin:0 auto style from the link. Soooo...that fixed it, but I still have no idea WHY this was a problem in safari and chrome.

like image 624
Munzilla Avatar asked Dec 21 '22 16:12

Munzilla


1 Answers

position() relates to the position relative to the containing DOM element, as opposed to the position on screen. The difference is probably caused by differences in the way the element hierarchy is rendered in Webkit browsers, rather than by a bug in jQuery.

You will have to check element hierarchies to find out which DOM element is causing your problem, or, if you want to position your tooltip relative to the position of an element within the window boundaries, use offset() instead.

like image 56
weltraumpirat Avatar answered Jan 13 '23 17:01

weltraumpirat