jQuery droppable - allow only one item to be dropped on an element and then make the dropped one draggable again

穿精又带淫゛_ 提交于 2019-11-28 14:49:11

As I mentioned in my comment, you can disable or destroy Droppable when the item is dropped. This will prevent further drops. You can also change the accept option so that it will not accept any more if you like.

Here is an example of the first solutions: https://jsfiddle.net/Twisty/upfy74tw/12/

JavaScript

$(function() {
  function handleRevert(e) {
    var $t = $(this);
    if (e === false) {
      $t.data("uiDraggable").originalPosition = $t.data("orDraggable");
      return true
    }
  }

  function handleDropEvent(event, ui) {
    var $self = $(this);
    var $item = ui.draggable;
    $item.position({
      my: 'left top',
      at: 'left top',
      of: $self
    });
    $self.droppable("destroy");
    $item.appendTo($self).attr("style", "");
    $item.draggable("destroy");
    $item.draggable({
      start: function() {
        $self.droppable({
          drop: handleDropEvent
        });
      }
    })
  }

  $(".draggable").draggable({
    revert: handleRevert
  });

  $(".draggable").data("orDraggable", $(".draggable").offset());

  $(".droppable").droppable({
    drop: handleDropEvent
  });
});

There are a few things going on in your drop event.

  1. Destroy droppable
  2. Align the draggable and then append it to the container
  3. Destroy draggable
  4. Assign Draggable and re-assign Droppable at start of drag event

Hope that helps.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!