javascript - jQuery 可调整大小 : How to get overlapping element during the resize drag

标签 javascript jquery jquery-ui resizable jquery-ui-resizable

当我使用 jQuery 可调整大小小部件拖动助手来调整某些元素的大小时,我需要获取对鼠标当前所在元素的引用。它的调整大小事件参数都没有给我这个引用;我也尝试过打电话

document.elementFromPoint(ui.position.left, ui.position.top)

但它给了我错误的元素(如果有的话)。

最佳答案

elementFromPoint 应该可以工作。但是像这样使用它时的一个问题是助手始终是鼠标下的元素。您可以做的就是隐藏助手,然后获取 elementFromPoint,然后再次显示它。它应该在大多数情况下都有效,除非您有许多重叠的元素。例如这样:

$('#resize').resizable({
  resize: function(event, ui) {
    $('.over').removeClass('over')
    ui.helper.hide();
    $(document.elementFromPoint(event.pageX, event.pageY)).addClass('over');
    ui.helper.show();
  }
})
.other {
  float: left;
  width: 100px;
  height: 100px;
  border: solid 1px lightgray;
}
#resize {
  border: solid 1px black;
  width: 50px;
  height: 50px;
  position: absolute;
}
div.over {
  background-color: lightgreen;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript" src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>

<link rel="stylesheet" type="text/css" href="//code.jquery.com/ui/1.11.4/themes/ui-lightness/jquery-ui.css">

<div id="resize"></div>

<div class="other"></div>
<div class="other"></div>
<div class="other"></div>
<div class="other"></div>
<div class="other"></div>
<div class="other"></div>
<div class="other"></div>
<div class="other"></div>
<div class="other"></div>
<div class="other"></div>
<div class="other"></div>

关于javascript - jQuery 可调整大小 : How to get overlapping element during the resize drag,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36799983/

相关文章:

javascript - 如何开发具有多个输出接口(interface)的Meteor JS应用程序?

javascript - 如何将两个具有相同功能的select html标签的两个JS结合起来?

jquery - 以编程方式选择 jquery ui 选项卡时如何调用 tabselect 事件

javascript - 将 Javascript Promise 放入函数中

javascript - 使用 Ajax 请求突出显示图像边框

javascript - 在nodejs中使用cv.readImage()和流

javascript - 将参数关闭函数添加到 JavaScript 中的字符串

jquery - CSS3(或 jQuery)更改内容顺序

javascript - 应用 ButtonSet 后禁用单个复选框

jquery-ui - jquery fullcalendar 隐藏特定时间