You need to check the purpose of the event to make sure the mouse leaves the entire page.
Js
$(function() { var $window = $(window), $html = $('html'); $window.on('mouseleave', function(event) { if (!$html.is(event.target)) return; $('.comeback').removeClass('hidden'); }); $window.on('mouseenter', function(event) { $('.comeback').addClass('hidden'); }); });
HTML
<div> <div> Test </div> <div class="comeback"> Come back! </div> <div> Test </div> </div>
CSS
.hidden { display: none; }
The test case includes some nesting of elements to make sure that it really works.
source share