javascript - 可拖动元素被放到哪里了?

标签 javascript jquery jquery-ui

我有以下 draggable 调用,使用 :

$someElm.draggable({     
    helper: "clone",         
    stop: function (ev, ui) {
        /* where was this dropped? */            
    }                        
});                          

在停止处理程序中,我需要放置目标。我不使用 droppable,只使用draggable。

那么,我怎样才能获得放置目标呢?

ev.target 似乎是拖动的元素。

<小时/>

目标我的意思是可拖动对象被放置的顶部元素(拖动停止)。这可能与 ev.target.parent() 不同。

与点击处理程序建立关联:

div.foo > div.bar

监听 div.foo 上的点击将捕获 div.bar 上的点击,但 ev.target 将是 div .bar 如果点击是在 div.bar 元素上完成的(否则它将是 div.foo)。

最佳答案

ui 变量包含 offset 属性(具有 lefttop 值)。

使用 document.elementFromPoint 函数,我们可以获取该位置上的元素,该元素正是放置元素的位置:

$someElm.draggable({     
    helper: "clone",         
    stop: function (ev, ui) {
       var dropTarget = $(document.elementFromPoint(ui.offset.left, ui.offset.top));
    }                        
});                          

即使它看起来像一个黑客,它似乎是做到这一点的唯一方法(如果有更好的替代帖子答案/评论)。

Document.elementFromPoint(x, y)

x and y specify the coordinates to check, in CSS pixels relative to the upper-left corner of the document's containing window or frame.

Returns the element from the document whose elementFromPoint method is being called which is the topmost element which lies under the given point. To get an element, specify the point via coordinates, in CSS pixels, relative to the upper-left-most point in the window or frame containing the document.

关于javascript - 可拖动元素被放到哪里了?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26590182/

相关文章:

javascript - Chrome 在简单地添加和删除 SVG 元素时会泄漏内存

javascript - jQuery/CSS - 单击 iframe 内容内的按钮时更改 IFRAME 高度

Javascript ForEach 函数在 IE 中不起作用

javascript - 表格排序器不适用于 json

javascript - chrome.downloads.download 中文件名的问题字符

Javascript 舍入到最接近的最大值

javascript - Jquery 客户端验证不起作用

javascript - xhr.setRequestHeader() 合并我的标题

javascript - 如何在 jQuery Mobile UI 中禁用缓存

html - 谷歌地图加载偏离中心