I’m trying to create an effect similar to a tooltip with jQuery.
I’m reusing the same <div> element (id="tooltip") to display the tooltip. Therefore, I need to constantly move this <div> element every time the user moves the cursor over an element which should display a tooltip. When I use the offset() property, it works fine the first time but then if I move the mouse repeatedly from one icon to the other, the tooltip starts to jump to different locations.
You can try it out here:
Try moving the mouse from one icon to the other repeatedly, you’ll notice that the tooltip jumps around incorrectly. It should always appear immediately to the right of the icon which is hovered over.
There are several problems in your code.
Divs aren’t self-closing tags:
When you have empty divs, add the following declaration in your CSS to ensure they have a body:
The jQuery
offset()function seems buggy in your case, because of your hidden#tooltip. Prefer the following code to change its offset:Additionally, the
o‘s creation seems very useless. You just have to modify directly theoffsetvariable:Finally, try not to access several times to the same element with a selector. jQuery will have to find your element in the whole DOM tree everytime you call it.
You can find a working jsfiddle here.