如何通过向上移动到地址栏来检测鼠标离开页面? [英] How can I detect a mouse leaving a page by moving up to the address bar?

查看:59
本文介绍了如何通过向上移动到地址栏来检测鼠标离开页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个jQuery事件,当访客离开页面时会弹出一个对话框.我正在使用e.pageY来检测鼠标位置.当鼠标位于Y:小于2时,将弹出对话框.问题是,当我向下滚动浏览页面并决定离开页面时,由于鼠标不在Y位置,小于2,所以不显示弹出窗口.我该如何解决.即,当我离开页面并仅将鼠标悬停在地址栏上时,尽管向下滚动,仍会弹出一个窗口.

I have created a jQuery event that pops up a dialog when a visitor is leaving a page. I am using the e.pageY to detect the mouse position. when the mouse is at Y: less than 2, the dialog pops up. the problem is, when the I scroll down through the page and decided to leave the page, the pop up does not show since the mouse is not at Y: less than 2. How can I fix that. i.e. when I leave the page and just hover over the address bar, a pop up appears despite scrolling down.

下面是我的代码和一个有效的示例.

Here my code and a working example at the bottom.

var mouseLastYPos = null;
$(document).mousemove(function(e){ 
    if(mouseLastYPos){ 
        if (e.pageY < mouseLastYPos && e.pageY <= 2){

           $('#mystuff').show();

        }
    }
    mouseLastYPos = e.pageY;
});​

工作示例: http://jsfiddle.net/bmHbt/

推荐答案

旧问题,但我想我也应该分享我的代码,也许有人觉得它有用.

Old Question, but I think I should share my code also, maybe someone finds it useful.

$(function(){
    var mouseY = 0;
    var topValue = 0;
    window.addEventListener("mouseout",function(e){
        mouseY = e.clientY;
        if(mouseY<topValue) {
            alert("Do something here!!!");
        }
    },
    false);
});

JSFIDDLE链接

这篇关于如何通过向上移动到地址栏来检测鼠标离开页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆