[英]JS/Jquery trigger when mouse is leaving viewport?
鼠標離開視口時如何觸發功能?
我從“ html”中收聽“ mouseleave”事件。 但是在Firefox中,“ mouseleave”也會在兩種情況下觸發。 1.是警報彈出時(我現在通過添加模糊/焦點偵聽器來解決)2.當我右鍵單擊頁面時,鼠標移至顯示菜單。
似乎firefox將此行為視為“ mouseleave”,即使我的鼠標仍在頁面中。
這是我的代碼。
$('html').bind('mouseleave',bouncehandler);
var visted = 1;
var bouncehandler = function(e){
var yheight = $(window).height();
if(e.pageX<$('body').width() && e.pageY < yheight ){
alert('leaving');
$('html').unbind('mouseleave',bouncehandler);
}
}
$(window).blur(function(){
$('html').unbind('mouseleave',bouncehandler);
}).focus(function(){
if(visited){
$('html').bind('mouseleave',bouncehandler);
}
});
我怎么解決這個問題 ? 謝謝
您只能在視口中獲取鼠標位置和任何鼠標事件。
您可以設置在瀏覽器關閉時執行的功能,如下所示:
function confirmExit(){
alert("confirm exit is being called");
return false;
}
window.onbeforeunload = confirmExit;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.