JavaScript,检测鼠标光标何时不在任何元素上
JavaScript, detect when the mouse cursors is not over any element
也许我忽略了一个简单的解决方案,但是我在我的研究中找不到任何可以回答这个问题的东西。
我想知道我的鼠标光标是否悬停在任何元素上
例如,假设我的页面有一个文本框。当鼠标光标移到文本框上时,没有任何反应。当鼠标光标离开文本框时,将触发此事件。
我尝试了以下方法,但事件总是会触发。
window.addEventListener('mousemove', function(event){
if (event.target === document.body){
console.log('fire');
}
});
一如既往,性能是主要考虑因素。
尝试关注
window.addEventListener('mousemove', function(event){
if (event.target.tagName === "HTML" || event.target.tagName === "BODY"){
console.log('fire');
}
});
<div>My content</div>
也许我忽略了一个简单的解决方案,但是我在我的研究中找不到任何可以回答这个问题的东西。
我想知道我的鼠标光标是否悬停在任何元素上
例如,假设我的页面有一个文本框。当鼠标光标移到文本框上时,没有任何反应。当鼠标光标离开文本框时,将触发此事件。
我尝试了以下方法,但事件总是会触发。
window.addEventListener('mousemove', function(event){
if (event.target === document.body){
console.log('fire');
}
});
一如既往,性能是主要考虑因素。
尝试关注
window.addEventListener('mousemove', function(event){
if (event.target.tagName === "HTML" || event.target.tagName === "BODY"){
console.log('fire');
}
});
<div>My content</div>