jQuery droppable - receiving events during drag over (not just on initial drag over)

前端 未结 5 1843
忘掉有多难
忘掉有多难 2021-01-31 09:50

I am using jQuery droppable (in conjunction with jQuery draggable) to allow the user to add rows to an HTML table by dragging items from a list and dropping them on the table.

5条回答
  •  無奈伤痛
    2021-01-31 10:35

    I just hit this problem. Not much is required if you just implement hit testing in the 'drag' event. Here I've just tagged all my drop targets with .myDropTarget, so it's easy to find them all, loop through them and check whether the mouse is over them.

    Something like this:

    thingToBeDragged.draggable({
        drag: function(evt) {
    
            $('.myDropTarget').removeClass('topHalf bottomHalf').each(function() {
                var target = $(this), o = target.offset(),
                    x = evt.pageX - o.left, y = evt.pageY - o.top;
    
                if (x > 0 && y > 0 && x < target.width() && y < target.height()) {
    
                    // mouse is over this drop target, but now you can get more
                    // particular: is it in the top half, bottom half, etc.
    
                    if (y > target.height() * 0.5) {
                        target.addClass('topHalf');
                    } else {
                        target.addClass('bottomHalf');
                    }
                }
            });
        },
        stop: function() {
            var droppedOn = $('.topHalf, .bottomHalf');
        }
    });
    

提交回复
热议问题