jqueryjquery-eventsmouseleave

Mousemove event fires, when it shouldn't


I have a problem with .mouseleave(). I want to show a popup when the user leaves the document with his mouse, but the mouseleave event keeps firing on entering the document, too.

This only happens in Chrome, in Mozzila everything is right.

So this is the code:

$(document).mouseleave(function (e) {
    alert('The mouse has left the building!');
});

One more thing: the event fires when I scroll, too. I could understand that one, because basically I'm leaving the document, but could I stop that?


Solution

  • Here is an approach that uses jQuery.hover() (demo)

    $(document).hover(function () {
      $('body').css({'background-color': 'red'});
    }, function () {
      $('body').css({'background-color': 'green'});
    });