简体   繁体   English

如何检测鼠标光标是否超出元素?

[英]How to detect if mouse cursor is out of element?

I have a listener which runs when I click on document. 当我单击文档时,我有一个运行的侦听器。

document.addEventListener('click', print);

function print(element)
{  
  doSomething();
}

It creates div id=panel, where I print some information. 它会创建div id = panel,在其中打印一些信息。

When I run the print function I would like to detect whether I clicked outside of the div#panel (The panel exists when I click second time). 当我运行打印功能时,我想检测是否在div#panel之外单击(该面板在我第二次单击时存在)。

I wish not to use the mouseout event listener because I think it is redundant to use listener for mouse movements when the event click is already fired. 我不希望使用mouseout事件侦听器,因为当事件单击已经触发时,使用侦听器进行鼠标移动是多余的。

How to detect when I clicked out of div#panel? 如何检测我何时从div#panel中单击?

You can check the target of jQuery's click event, which element it was: 您可以检查jQuery click事件的target ,它是哪个元素:

$(document).click(function(e) {
    var target = $(e.target);

    if( !target.is("#panel") && target.closest("#panel").length === 0 ) {
        // click was not on or inside #panel
    }
});

Your event handler gets passed an event object, not an element. 您的事件处理程序将传递一个事件对象,而不是元素。 Since you are listening for the click event, the event will be of type MouseEvent and that event object will have a target property which you can use to check if the target element matches your desired element. 由于您正在监听click事件,因此该事件的类型为MouseEvent,并且该事件对象将具有target属性,您可以使用该属性检查目标元素是否与所需元素匹配。

function handler(event) {
    if (event.target == document.getElementById("panel")) {    
        // Do stuff
    }
}

document.addEventListener('click', handler);

Edit: I intentionally gave the vanilla JS answer since your own code fragments don't use jQuery. 编辑:由于您自己的代码片段不使用jQuery,所以我故意给了香草JS答案。 But jQuery wouldn't change anything as its event handling API is almost just a thin wrapper over JS. 但是jQuery不会改变任何东西,因为它的事件处理API几乎只是JS的薄包装。

I am just using event from the click. 我只是使用点击事件。 Here it is 这里是

var elem=document.getElementById("elem");
var rects=elem.getBoundingClientRect();//get the bounds of the element

document.addEventListener('click', print);

function print(e)
{  
 //check if click position is inside or outside target element
 if(e.pageX<= rects.left +rects.width && e.pageX>= rects.left &&  e.pageY<= rects.top +rects.height && e.pageY>= rects.top){
    console.log("Inside element");
 }   
 else{
   console.log("Outside element");
}
}

JS Bin link : https://jsbin.com/pepilehigo/edit?html,js,console,output JS Bin链接: https : //jsbin.com/pepilehigo/edit? html,js,console, output

A different approach, using only javascript is: 仅使用javascript的另一种方法是:

 function print(evt) { if (!(evt.target.tagName == 'DIV' && evt.target.classList.contains('myDiv'))) { var div = document.createElement('div'); div.classList.add('myDiv'); div.textContent="new div"; document.body.appendChild(div); } } window.onload = function() { document.addEventListener('click', print); } 
  .myDiv { border:1px solid green; } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM