我希望能够检测到鼠标离开窗口,以便在用户的鼠标移动到其他位置时停止事件触发。
有什么好的想法吗?
我希望能够检测到鼠标离开窗口,以便在用户的鼠标移动到其他位置时停止事件触发。
有什么好的想法吗?
在 HTML 页面上实现拖放行为通常需要这种类型的行为。以下解决方案已在 MS Windows XP 机器上的 IE 8.0.6、FireFox 3.6.6、Opera 10.53 和 Safari 4 上进行了测试。
首先是 Peter-Paul Koch 的一个小函数;用于跨浏览器事件处理程序:
function addEvent(obj, evt, fn) {
if (obj.addEventListener) {
obj.addEventListener(evt, fn, false);
}
else if (obj.attachEvent) {
obj.attachEvent("on" + evt, fn);
}
}
然后使用此方法将事件处理程序附加到文档对象的mouseout事件:
addEvent(document, "mouseout", function(e) {
e = e ? e : window.event;
var from = e.relatedTarget || e.toElement;
if (!from || from.nodeName == "HTML") {
// stop your drag event here
// for now we can just use an alert
alert("left window");
}
});
最后,这是一个包含嵌入式调试脚本的HTML页面:
<html>
<head>
<script type="text/javascript">
function addEvent(obj, evt, fn) {
if (obj.addEventListener) {
obj.addEventListener(evt, fn, false);
}
else if (obj.attachEvent) {
obj.attachEvent("on" + evt, fn);
}
}
addEvent(window,"load",function(e) {
addEvent(document, "mouseout", function(e) {
e = e ? e : window.event;
var from = e.relatedTarget || e.toElement;
if (!from || from.nodeName == "HTML") {
// stop your drag event here
// for now we can just use an alert
alert("left window");
}
});
});
</script>
</head>
<body></body>
</html>
$(document).mouseleave(function () {
console.log('out');
});
每当鼠标不在您希望的页面上时,此事件将被触发。只需更改函数以执行所需操作。
您还可以使用:
$(document).mouseenter(function () {
console.log('in');
});
当鼠标再次进入页面时触发。
为了检测鼠标离开事件,而不考虑滚动条、自动完成字段或检查:
document.addEventListener("mouseleave", function(event){
if(event.clientY <= 0 || event.clientX <= 0 || (event.clientX >= window.innerWidth || event.clientY >= window.innerHeight))
{
console.log("I'm out");
}
});
条件说明:
event.clientY <= 0 is when the mouse leave from the top
event.clientX <= 0 is when the mouse leave from the left
event.clientX >= window.innerWidth is when the mouse leave from the right
event.clientY >= window.innerHeight is when the mouse leave from the bottom
========================编辑===============================
似乎在新版本的Firefox浏览器中,document.addEventListener(“mouseleave”)不会触发,mouseleave需要附加到像body或子元素这样的元素。
我建议使用相反的方式
document.body.addEventListener("mouseleave")
或者
window.addEventListener("mouseout")
这个对我有用:
addEvent(document, 'mouseout', function(evt) {
if (evt.toElement == null && evt.relatedTarget == null) {
alert("left window");
}
});
if (!evt.toElement && !evt.relatedTarget)
。 - Pavel Ye您可以在 html
标签中使用 mouseleave
(以及 mouseenter
来检测进入时)(已在 Chrome 91 和 Firefox 90 中进行了测试)
尝试在下面的代码片段中悬停进入和退出。
document.documentElement.addEventListener('mouseleave', () => console.log('out'))
document.documentElement.addEventListener('mouseenter', () => console.log('in'))
使用onMouseLeave事件可以防止冒泡,并使您轻松检测到鼠标离开浏览器窗口。
<html onmouseleave="alert('You left!')"></html>
document.onmouseleave = function() { alert('You left!') };
不要使用document.body
,因为它会在滚动条上方触发并缩小body!不需要编写防止元素触发的代码。好的解释请参考:https://developer.mozilla.org/en-US/docs/Web/API/Element/mouseleave_event - user985399document.documentElement.addEventListener('mouseleave', function() { console.log('OUT'); })
或document.body.addEventListener('mouseleave', function() { console.log('OUT'); })
这样的脚本在Chrome 90.0.4430.93上无法正常工作。 - Brigowindow.onblur
和 document.mouseout
。
window.onblur
在以下情况下被触发:
Ctrl+Tab
或 Cmd+Tab
切换到另一个窗口。window.onblur = function(){
console.log("Focus Out!")
}
document.mouseout
被触发的情况:
Ctrl+Tab
或 Cmd+Tab
切换到另一个窗口。基本上,无论何时您的光标离开了文档,都会触发该事件。
document.onmouseleave = function(){
console.log("Mouse Out!")
}
window.onblur
。+1 - Radvylf Programs我尝试了上述所有方法,但是没有一个像预期的那样有效。经过一些研究,我发现e.relatedTarget是在鼠标离开窗口之前的html。
所以...最终我得到了这个:
<s>document.body.addEventListener('mouseout', function(e) {
if (e.relatedTarget === document.querySelector('html')) {
console.log('We\'re OUT !');
}
});</s>
2019更新
(如用户1084282所发现)
document.body.addEventListener('mouseout', function(e) {
if (!e.relatedTarget && !e.toElement) {
console.log('We\'re OUT !');
}
});
if(!e.relatedTarget && !e.toElement) { ... }
,然后它就可以工作了。 - user993683if(!e.relatedTarget && !e.toElement)
而不是实际答案中的条件,它可以在Chrome、Firefox和IE Edge上正常工作。它可以检测鼠标离开文档主体。 - Haijerome以上所列答案均无法解决我的问题。我现在使用以下方法:
document.addEventListener('dragleave', function(e){
var top = e.pageY;
var right = document.body.clientWidth - e.pageX;
var bottom = document.body.clientHeight - e.pageY;
var left = e.pageX;
if(top < 10 || right < 20 || bottom < 10 || left < 10){
console.log('Mouse has moved out of window');
}
});
我正在使用这种拖放文件上传小部件。它并不是绝对准确的,只有当鼠标离窗口边缘一定距离时才会被触发。
https://dev59.com/CnNA5IYBdhLWcg3wh-cC#3187524
我跳过了旧版本浏览器,所以我将代码缩短以适应现代浏览器(IE9+)
document.addEventListener("mouseout", function(e) {
let t = e.relatedTarget || e.toElement;
if (!t || t.nodeName == "HTML") {
console.log("left window");
}
});
document.write("<br><br>PROBLEM<br><br><div>Mouseout trigg on HTML elements</div>")
在这里,您可以看到浏览器支持的情况。
我认为这很简短
但是问题仍然存在,因为"mouseout"会在文档中的所有元素上触发。
为了防止它发生,请使用mouseleave(IE5.5+)。请参见链接中的良好解释。
以下代码可在不触发要在内部或外部的元素的情况下工作。也尝试在文档外拖放并释放。
var x = 0
document.addEventListener("mouseleave", function(e) { console.log(x++)
})
document.write("<br><br>SOLUTION<br><br><div>Mouseleave do not trigg on HTML elements</div>")
document.body
上,因为窗口滚动条可能会缩小页面主体,并在鼠标指针位于滚动条上方但您想要滚动而不想触发mouseLeave
事件时触发。请像示例中那样将其设置在document
上。