简体   繁体   English

仅当光标距离div再距div 200px时才关闭div

[英]Close div only when cursor is further away then 200px from the div

I'm trying to close a popup DIV when a user clicks with his cursor on the outside of the popup DIV. 当用户用光标在弹出式DIV的外部单击时,我试图关闭弹出式DIV。 I use this JQuery to do so: 我使用此JQuery这样做:

$(document).mouseup(function (e) {
          var container = $(".polnamen");

          if (!container.is(e.target) // if the target of the click isn't the container...
              && container.has(e.target).length === 0) // ... nor a descendant of the container
          {
              container.hide(200);
          }
      });

This code works fine, however I only want the DIV to close when the cursor is further away then 200px from my DIV. 这段代码可以正常工作,但是我只希望当光标离我的DIV 距200px时关闭DIV。 Is this possible, and how should I do this? 这可能吗,我应该怎么做?

To do what you want, I think you have to wrap your div in another div which is 400px larger, and check if the click is in the new div. 要执行您想要的操作,我认为您必须将div包装在另一个400像素大的div中,然后检查点击是否在新的div中。

The HTML will be : HTML将是:

<div class="wrapper">
    <div class="polnamen">
        <!-- Your code here -->
    </div>
</div>

The CSS : CSS:

.wrapper {
    padding: 200px;
    position: relative;
    top: -200px;
    left: -200px;
}

The jQuery : jQuery的:

$(document).mouseup(function (e) {
          var container = $(".wrapper");

          if (!container.is(e.target) // if the target of the click isn't the container...
              && container.has(e.target).length === 0) // ... nor a descendant of the container
          {
              container.hide(200);
          }
      });

You can get the position of the click relative to the document from the pageX and pageY event properties when you receive the click. 收到点击时,您可以从pageXpageY事件属性获取点击相对于文档的位置。

You can get the position (relative to the document) and dimensions of the div from the jQuery offset (confusingly, it's offset not position ) , width , and height functions. 您可以从jQuery offset (令人困惑的是, offset不是positionwidthheight函数获取div的位置(相对于文档)和尺寸。

Then a bit of math tells you whether the click is far enough away. 然后,一些数学运算会告诉您单击是否足够远。

This code may help you.. 此代码可能会帮助您。

First find the distance of mouse cursor from element and if we find it 200px away then we need to fire our 首先找到鼠标光标与元素的距离,如果找到200px,则需要触发

var mX, mY, distance;    
$element  = $('#element');

function calculateDistance(elem, mouseX, mouseY) 
{
       return Math.floor(Math.sqrt(Math.pow(mouseX - (elem.offset().left+(elem.width()/2)), 2) + Math.pow(mouseY - (elem.offset().top+(elem.height()/2)), 2)));
}

$(document).mousemove(function(e) 
{  
    mX = e.pageX;
    mY = e.pageY;
    distance = calculateDistance($element, mX, mY);

    if(distance == 200)
    {
        // Start : Write your action here --------------- //
        var container = $(".polnamen");

      if (!container.is(e.target) // if the target of the click isn't the container...
          && container.has(e.target).length === 0) // ... nor a descendant of the container
      {
          container.hide(200);
      }
     // End : Write your action here --------------- // 
   }         
});

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

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