4

I have an image in a page and in the onmouseover event of that image I will call a JavaScript function to show a tooltip and in the onmouseout of the image, I will call a method to hide the tooltip. Now I found that when I place the cursor on the image, it's calling the method to show the tooltip div.

And if I move the mouse within the image, it's calling the onmouseout event (even if I am not out of the image). How can I stop this? I want onmouseout to be called when the cursor is out of the image. Any thoughts?

Here is how I call it:

 <img src="images/customer.png" onmouseout="HideCustomerInfo()" onmouseover="ShowCustomerInfo(485)" />

And in my JavaScript:

 function ShowCustomerInfo(id) {


 var currentCustomer = $("#hdnCustomerId").val();   
 if (currentCustomer != id) { // to stop making an ajax call everytime when the mouse move on the same image
     
     $.get('../Lib/handlers/userhandler.aspx?mode=custinfo&cid=' + id, function (data) {
         strHtml = data;
     });

     tooltip.show(strHtml);   // method in another jquery pluggin
     $("#hdnCustomerId").val(id);
 }
}

function HideCustomerInfo() {

 tooltip.hide();    // method in another jquery pluggin
 $("#hdnCustomerId").val(0); //setting in a hidden variable in the page

 }
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Shyju
  • 214,206
  • 104
  • 411
  • 497

4 Answers4

2

I am assuming you are mousing over your tooltip? and the tooltip has its markup outside the realm of the image?

So by mousing over the tooltip you are technically leaving the image. I have had similar things happen to me.

To get around this, try sticking a div around the image, and putting the mouse event on the div, then as a child element to the div have your tooltip, so even when you are then mousing over the tooltip, you are still inside the div.

That might work.

Dmitry Reutov
  • 2,995
  • 1
  • 5
  • 20
jimplode
  • 3,474
  • 3
  • 24
  • 42
  • Not mousing over tooltip.But mousing over the image where i attached the tooltip showing method on the mouseover event – Shyju Sep 27 '10 at 16:25
  • try binding to mouseleave instead of mouseout?? Is this using jquery?? – jimplode Sep 27 '10 at 16:36
  • 1
    I have tried your code and it works..... There must be something to do with the tooltip, I removed all the stuff in your functions and just had alerts, I had one fire on mouseover and the other fire when I left the image. I did not get hundreds of calls when moving over the image either. try moving the markup around the tooltip, so it displays the data away from the image. – jimplode Sep 27 '10 at 17:00
  • I did a close investication and realized that when i make a mouse move on the image,It is entering into the tooltip div and thus calling mouseout of the image.My tooltip was close to the cursor(so close to the image too). I just changed the css of the tooltip to make it 50 px away from the cursor so that my cursor wont enter back to the tooltip. Thanks jimplode for guiding this aspect. It worked – Shyju Sep 27 '10 at 17:22
1

If the image tht you are using to hover over has transparency (it's a png, right), then when you move the mouse over a transparent portion of the image you'll get an onmouseout event. yeah, it sucks. I have the same problem and no elegant solution for it yet.

1

Another easy way to get around this is by setting pointer-events: none on your tooltip

0

Are you sure HideCustomerInfo() is called by the onmouseout event handler of the image? Also, what browsers do you get that in? Do you get this behavior in one particular browser?

bugventure
  • 498
  • 2
  • 6