【发布时间】:2014-01-17 21:18:06
【问题描述】:
我正在使用以下函数(在this really helpful post 中找到)来检测鼠标是否离开了窗口:
var addEvent = function (obj, evt, fn)
{
if (obj.addEventListener)
{
obj.addEventListener(evt, fn, false);
}
else if (obj.attachEvent)
{
obj.attachEvent("on" + evt, fn);
}
};
addEvent(document, "mouseout", function (e)
{
e = e ? e : window.event;
var from = e.relatedTarget || e.toElement;
if (!from || from.nodeName == "HTML")
{
console.log("left the window");
}
});
但是,当鼠标非常缓慢地离开窗口或鼠标在离开窗口之前非常接近窗口边界时,这将不起作用。
有没有办法解决这个问题,使用 jQuery 或纯 Javascript?
【问题讨论】:
标签: javascript jquery mouseout onmouseout