Actually by clicking on an element the event mouseleave
is fired. Would that be a bug once the mouse has not left the body? Or the event click
is triggering the event? I did a brief survey and saw comments that this would actually be a bug, at last...
To solve this I made this code that cancels the event mouseleave
by clicking and restoring it after a brief delay ten thousandths of a second (10 ms). This delay is necessary because if you restore the event immediately it will be triggered anyway.
But I changed the focus of events to document
instead of the body
, which is more interesting because it will detect the mouse leaving the document area instead of the body
, which is not the same thing. The body
page is just where the tag <body>
has content, already the document
is the entire area of the window where the page is displayed, regardless of whether it has content or not.
I commented on the lines of if
not to display error in snippet because
localStorage. When copying code, uncomment lines.
Behold:
jQuery(document).ready(function(){
var t = jQuery(this);
t.on("click", function(){
t.off("mouseleave");
setTimeout(function(){
t.on("mouseleave", function(){
console.log("saiu"); // apenas para ilustrar, pode remover esta linha
//if (!jQuery('body').hasClass('leave-on') && localStorage.getItem("leave") != "closed") {
// jQuery('body').addClass('leave-on');
//}
});
}, 10);
});
// disparo o evento para ativar o mouseleave
t.trigger("click");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Ao clicar na div abaixo o evento mouseleave não é disparado e o console.log não será exibido:
<div style="width: 50px; height: 50px; background: red;">Clique aqui</div>
This site is in Portuguese, so next time, please post in Portuguese. I have already translated your question.
– Victor Stafusa