简体   繁体   English

jQuery UI可拖动:在拖动停止时获取底层元素

[英]jQuery UI draggable: get the underlaying element at drag-stop

Is it possible to get the underlaying element of an dragged element? 是否可以获取拖动元素的底层元素?

My specific task is to drag a td element. 我的具体任务是拖动td元素。 If the dragstop event is fired I want the underlaying element. 如果触发了dragstop事件,则需要底层元素。 The underlaying element is also a td . 底层元素也是td

How could I achiev this? 我怎么能做到这一点?

Here is my HTML code: 这是我的HTML代码:

<table class="grid">
     <tr>
         <td class="value" colspan="4"></td>
         <td></td>
     </tr>
     <tr>
         <td></td>
         <td></td>
         <td></td>
         <td></td>
         <td></td>
     </tr>
</table>

The jQuery part is here: jQuery部分在这里:

$('.value').draggable({
    cursor: "move",
    containment: ".grid",
    snap: "td",
    start: function(event, ui) {
        console.log("start", event, ui, $(this));
    },
    stop: function(event, ui) {
        console.log("stop", event, ui, $(this));
    }
});

If I drag the td with the class value and I stop dragging I want the td element under the value element. 如果我将td与类value一起拖动,然后停止拖动,则希望td元素位于value元素下方。

Thanks in advance. 提前致谢。

There is a neat little function wich will find you the element on a specific function: document.elementFromPoint(x, y); 有一个整洁的小函数,可以找到特定函数的元素: document.elementFromPoint(x, y);

On SO I found a good solution to determine all elements on that position. 因此,我找到了一个很好的解决方案来确定该位置上的所有元素。 I refer to this answer in my code: 我在我的代码中提到了这个答案:

$('.value').draggable({
    cursor: "move",
    containment: ".grid",
    snap: "td",
    start: function(event, ui) {
    },
    stop: function(event, ui) {

        var el = allElementsFromPoint(event.pageX, event.pageY);
        var td = $(el).filter('td').not($(this));
        td.css({'backgroundColor': 'red'});
    }
});

Demo 演示

So the function will return all elements that lies at the specified position. 因此,该函数将返回位于指定位置的所有元素。 To get the wanted td filter the elements to only contain td s and remove the dragged td . 为了得到想要的td过滤器,这些元素仅包含td然后删除拖动的td

Reference 参考

.elementFromPoint() .elementFromPoint()

.filter() 。过滤()

.not() 。不()

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

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