jQuery UI Droppable恢复无法再次删除

我一直在尝试为单词学习应用程序创建拖放系统,但是在我恢复并尝试再次拖动它们之后,出现了可拖动元素无法重用的问题。

为还原可拖动对象,我使用了out方法,以便将其拖回到可放置区域之外时,它将还原到其先前位置。 我这样做是通过删除可拖动实例并将其重新添加进来的,如果我尝试将同一元素拖回可放置区域,它将无法放置。我已经尝试过尝试重新初始化可放置区域,但这似乎并没有改变任何东西。

$(document).ready(function () {
  createDraggable();
  createDroppable();
});

function createDraggable(){
  $(".word").draggable({
    containment: ".stage",revert: 'invalid',revertDuration: 0
  });
}

function disableOtherDraggable(except){
  $(".word:not(#" + except.attr('id') + ")").draggable('disable');
}

function createDroppable(){
  $('.drop').droppable({
    tolerance: 'touch',accept: '.word',drop: function(event,ui) {
      ui.draggable.position({
        my: "center",at: "center",of: $(this),using: function(pos) {
          $(this).animate(pos,200,"linear");
        }
      });
      $(ui.draggable).css('background',"transparent");
      disableOtherDraggable(ui.draggable);
    },out: function(event,ui) {
      ui.draggable.mouseup(function () {
        ui.draggable.removeAttr('style');
        $(".word").draggable("destroy");
        createDraggable();
      });
    }
  });
}

我希望能够让人们丢弃这些单词,并在需要时将其拖回。在完成此工作后,我将设置一个按钮来检查所输入的单词是否正确。

在此示例中可以拖动4个单词,但范围可以是3到5

更新

这是我为感兴趣的任何人工作的更新代码。我将舞台创建为可放置区域,然后根据需要将其打开和关闭。

$(function() {
  function createDraggable(o) {
    o.draggable({
      containment: ".stage",revertDuration: 0
    });
  }

  function toggleOtherDraggable() {
    $(".words .word").each(function(i,val){
     if(!$(val).hasClass('ui-dropped')) $(val).draggable('disable');
    });
  }

  function createLineDroppable(){
    $('.drop').droppable({
      tolerance: 'touch',ui) {
        ui.draggable.position({
          my: "center",using: function(pos) {
            $(this).animate(pos,"linear");
          }
        });
        $(ui.draggable).css('background','transparent');
        $(ui.draggable).addClass('ui-dropped');
        toggleOtherDraggable();
      },out: function(){
        $('#stage-drop').droppable('enable');
      }
    });
  }

  function createStageDroppable() {
    $('#stage-drop').droppable({
      tolerance: 'touch',disabled: true,ui) {
        $(ui.draggable).css('left','0');
        $(ui.draggable).css('top','0');
        $(ui.draggable).css('background','');
        $(ui.draggable).removeclass('ui-dropped');
        $('#stage-drop').droppable('disable');
        $(".words .word").draggable('enable');
      }
    });
  }

  createDraggable($(".words .word"));
  createLineDroppable();
  createStageDroppable();
});
zhb6434513 回答:jQuery UI Droppable恢复无法再次删除

您需要一个放置extends StdDeserializer<List<WeatherDto>>的位置,一个要禁用其他位置的位置,另一个要返回的位置。

考虑以下示例:

.word
$(function() {
  function createDraggable(o) {
    o.draggable({
      containment: ".stage"
    });
  }

  function toggleOtherDraggable() {
    if ($(".words .word").eq(0).hasClass("ui-draggable-disabled")) {
      $(".words .word").draggable('enable');
    } else {
      $(".words .word").draggable('disable');
    }
  }

  function createDropWord() {
    $(".words").droppable({
      tolerance: 'touch',accept: '.word',drop: function(event,ui) {
        var item = ui.draggable;
        item.removeAttr("style");
        $(this).append(item);
      }
    });
  }

  function createDropStage() {
    $('.drop').droppable({
      tolerance: 'touch',ui) {
        var item = ui.draggable;
        item.appendTo(this).position({
          my: "center",at: "center",of: $(this),using: function(pos) {
            $(this).animate(pos,200,"linear");
          }
        }).css('background',"transparent");
        toggleOtherDraggable();
        createDropWord()
      },out: function() {
        toggleOtherDraggable();
      }
    });
  }

  createDraggable($(".words > .word"));
  createDropStage();
});
.word {
  display: inline-block;
  padding: .25em;
}

.drop {
  width: 340px;
  height: 100px;
  background: #CCC;
  margin: 20px;
}

.word.ui-draggable-disabled {
  opacity: 0.45;
}

第一次将对象拖放到<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css"> <script src="https://code.jquery.com/jquery-1.12.4.js"></script> <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script> <div class="stage"> <div class="ui-widget words"> <div id="word-1" class="word ui-widget-content">Word 1</div> <div id="word-2" class="word ui-widget-content">Word 2</div> <div id="word-3" class="word ui-widget-content">Word 3</div> <div id="word-4" class="word ui-widget-content">Word 4</div> </div> <div class="drop"></div> </div>区域时,这将使放置的对象居中,并禁用其他可拖动对象,因此无法将其拖入。将项目拖出后,将重新启用它们

您必须使用.drop.append(),以便随后将放置的项目添加到容器中。这适用于两个元素。

.appendTo()选项仅在特定的丢弃情况下启用。如果可放置对象将不接受该项目或返回revert,则当以false为首选项时,该项目将被还原。

本文链接:https://www.f2er.com/3169135.html

大家都在问