在mouseleave上将交互式框放置在画布顶部

时间:2015-05-29 06:16:27

标签: javascript html css

我正试图按照mousemove上的光标制作一个方框。但是,如果

,它应该停止跟随鼠标
  1. 用户点击框
  2. 用户的鼠标离开了周围的div
  3. 以下是当前设置的样子:

    box that follows cursor on mousemove

    $(function() {
      $(document).mousemove(function(e) {
    
        // assign the co-ords as vars so 
        var vert = e.pageY;
        var horz = e.pageX;
    
        // get co -ords for the center of the box
        centx = Math.ceil($('div').width() / 2);
        centy = Math.ceil($('div').height() / 2);
    
        // checking to see if the cursor is outside the boudries of the box and stoping the circle moving past them
        if (e.pageY + 53 > Math.ceil($('div').height())) var vert = Math.ceil($('div').height() - 47);
        if (e.pageY - 53 < 0) vert = 53;
        if (e.pageX + 53 > Math.ceil($('div').width())) var horz = Math.ceil($('div').width() - 47);
        if (e.pageX - 53 < 0) horz = 53;
    
        // work out the distance between the cursor and the center of box
        disx = horz - centx;
        disy = vert - centy;
    
        // work out the scale of the shadow
        sx = -disx * 0.1;
        sy = -disy * 0.1;
    
        // work out the shadow blur
        b = (Math.abs(sx) + Math.abs(sy)) * 0.5;
    
        //apply the css
        $('p').css({
          'top': vert - 53,
          'left': horz - 53,
          '-webkit-box-shadow': '#a0a0a0 ' + sx + 'px ' + sy + 'px ' + b + 'px, inset ' + sx + 'px ' + sy + 'px ' + b + 'px #e3e3e3'
        });
      });
    });
    body {
      padding: 0px;
    }
    div {
      position: relative;
      width: 400px;
      height: 400px;
      background-color: #f0f0f0;
    }
    p {
      background-color: orange;
      height: 100px;
      width: 100px;
      position: absolute;
      top: 100px;
      left: 100px;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
    <div>
      <p>set the location</p>
    </div>

    fiddle

1 个答案:

答案 0 :(得分:0)

修改

要在单击框后限制移动,只需使用辅助变量:

var cond = false;
$(yourElement).click(function () {
    cond = true;
})

然后,在mousemove函数的正文中:

if (cond) return false;

您应该使用Element.getBoundingClientRect检查鼠标是否在div中:

var boundary = document.getElementById("theDiv").getBoundingClientRect()
var vert = e.pageY;
var horz = e.pageX;

if (boundary.top <= vert && vert <= boundary.bottom &&
        boundary.left <= horz && horz <= boundary.right) {
    /* the rest of your code */
}

Element.getBoundingClientRect返回如下对象:

{
  bottom: 408,
  height: 400,
  left: 8,
  right: 408,
  top: 8,
  width: 400,
}

如果你想使用jQuery,你可以使用

var left = $(item).offset().left
  , top = $(item).offset().top
  , right = left + $(item).width()
  , bottom = top + $(item).height()
  ;

并设置类似的if条件。

<强>演示:

$(function() {
  var cond = false;
  $("#theDiv").click(function () {
    cond = true;
  })
  $(document).mousemove(function(e) {
    
    if (cond) return false;
    
    var boundary = document.getElementById("theDiv").getBoundingClientRect()

    // assign the co-ords as vars so 
    var vert = e.pageY;
    var horz = e.pageX;

    if (boundary.top <= vert && vert <= boundary.bottom && boundary.left <= horz && horz <= boundary.right) {

      // get co -ords for the center of the box
      centx = Math.ceil($('div').width() / 2);
      centy = Math.ceil($('div').height() / 2);

      // checking to see if the cursor is outside the boudries of the box and stoping the circle moving past them
      if (e.pageY + 53 > Math.ceil($('div').height())) var vert = Math.ceil($('div').height() - 47);
      if (e.pageY - 53 < 0) vert = 53;
      if (e.pageX + 53 > Math.ceil($('div').width())) var horz = Math.ceil($('div').width() - 47);
      if (e.pageX - 53 < 0) horz = 53;

      // work out the distance between the cursor and the center of box
      disx = horz - centx;
      disy = vert - centy;

      // work out the scale of the shadow
      sx = -disx * 0.1;
      sy = -disy * 0.1;

      // work out the shadow blur
      b = (Math.abs(sx) + Math.abs(sy)) * 0.5;

      //apply the css
      $('p').css({
        'top': vert - 53,
        'left': horz - 53,
        '-webkit-box-shadow': '#a0a0a0 ' + sx + 'px ' + sy + 'px ' + b + 'px, inset ' + sx + 'px ' + sy + 'px ' + b + 'px #e3e3e3'
      });
    }
  });
});
body {
  padding: 0px;
}
div {
  position: relative;
  width: 400px;
  height: 400px;
  background-color: #f0f0f0;
}
p {
  background-color: orange;
  height: 100px;
  width: 100px;
  position: absolute;
  top: 100px;
  left: 100px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div id="theDiv">
  <p>set the location</p>
</div>

fiddle