简体   繁体   English

放置在表格单元格的边框上时未触发放置事件

[英]Drop event not fired when dropping on border of a table cell

I am using jQuery UI draggable plugin to drag divs into a table. 我正在使用jQuery UI可拖动插件将div拖动到表中。

My problem is that sometimes when dragging into a table cell I experience that the drop event is not triggered. 我的问题是,有时当拖动到表格单元格时,我会遇到未触发放置事件的情况。 It seems to happen when dropped exactly on the one pixel that separates two cells (vertical or horizontal). 当恰好落在分隔两个单元(垂直或水平)的一个像素上时,似乎会发生这种情况。 I've tried to set the border to 0 but still I have the problem. 我尝试将边框设置为0,但仍然有问题。

Check out this simplified example . 看看这个简化的例子

Working demo http://jsfiddle.net/YNUzx/1/ or http://jsfiddle.net/YNUzx/2/ 工作演示 http://jsfiddle.net/YNUzx/1/ http://jsfiddle.net/YNUzx/2/

Tolerance option should be set to touch for better drag functionality :) Tolerance选项应设置为touch以获得更好的拖动功能:)

or use pointer as tolerance: mouse pointer overlaps the droppable 使用pointer作为公差:鼠标指针与可放置对象重叠

Good read: http://jqueryui.com/demos/droppable/#option-tolerance 好的阅读: http : //jqueryui.com/demos/droppable/#option-tolerance

Behaviour : Now when you will drag say "1" to the table cell a touch will trigger the drop in the table, rest you can see that in demo. 行为 :现在,当您将“ 1”拖到表格单元格时,触摸会触发表格中的放置,其余的可以在演示中看到。

Hope this helps, 希望这可以帮助,

Following line does the trick: tolerance: "touch", 以下代码可以解决问题: tolerance: "touch",

code

$(".dragbox").draggable({
    revert: true
});

$("table tr td").droppable({
    accept: ".dragbox",
    tolerance: "touch",
    drop: function(event, ui) {
        $(this).css({
            'background-color': 'white'
        }).html(ui.draggable.find('span').text());
    },
    over: function(event, ui) {
        $(this).css({
            'background-color': '#DCFFDB'
        });
    },
    out: function(event, ui) {
        $(this).css({
            'background-color': 'white'
        });
    }
});​

What I suggest is use the 'touch' tolerance option but remove the margin of the draggables, since they are creating problem for the proper functionality. 我建议使用“触摸”公差选项,但要删除可拖动对象的边距,因为它们为适当的功能造成了问题。 Instead use a wrapper div for the draggables and give that div some margin. 而是将包装div用于可拖动对象,并为该div提供一定的边距。 This might help http://jsfiddle.net/YNUzx/3/ 这可能会帮助http://jsfiddle.net/YNUzx/3/

Also I would recommend that you increase the width and height of td to see the proper working. 另外,我建议您增加td的宽度和高度,以查看其正常工作。

EDIT: try this http://jsfiddle.net/YNUzx/4/ I think I got the solution 编辑:试试这个http://jsfiddle.net/YNUzx/4/我想我已经解决了

Thanks to a comment by Tejasva Dhyani and chat with Tats_innit I found the answer: 感谢Tejasva Dhyani的评论并与Tats_innit聊天,我找到了答案:

jQuery gets confused (and rightfully so) when dropping while the draggable div is just between two droppable elements (cells). 当拖放时jQuery感到困惑(理所当然),而可拖动div恰好位于两个可放置元素(单元)之间。

Ie if dropping while 15px is over one cell and 15px is over another the drop event is not triggered. 也就是说,如果在一个单元格上的15px上方和另一个像素上的15px上方进行放置时,则不会触发放置事件。

All I did was to set the width and height to an uneven number, see jsfiddle here : 我所做的就是将宽度和高度设置为一个不均匀的数字, 请参见jsfiddle此处

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

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