jQuery draggable + droppable: how to snap dropped element to dropped-on element

前端 未结 7 1280
无人及你
无人及你 2020-11-30 19:42

I have my screen divided into two DIVs. In the left DIV I have a few 50x50 pixel DIVs, in the right DIV I have an empty g

7条回答
  •  执笔经年
    2020-11-30 20:21

    I found that Keith's method worked for me. Since his answer doesn't include an example implementation, I'll post mine:

    $('.dropTarget').droppable({
        drop: function(ev, ui) {
            var dropped = ui.draggable;
            var droppedOn = $(this);
            $(dropped).detach().css({top: 0,left: 0}).appendTo(droppedOn);
        }
    });
    

    or, slightly more concisely:

    $('.dropTarget').droppable({
        drop: function(ev, ui) {
            $(ui.draggable).detach().css({top: 0,left: 0}).appendTo(this);
        }
    });
    

提交回复
热议问题