【发布时间】:2020-02-29 09:29:52
【问题描述】:
我一直在尝试为单词学习应用程序创建拖放系统,但在我恢复并尝试再次拖动它们后遇到了可拖动元素无法重复使用的问题。
为了恢复可拖动对象,我使用了 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",
revert: 'invalid',
revertDuration: 0
});
}
function toggleOtherDraggable() {
$(".words .word").each(function(i, val){
if(!$(val).hasClass('ui-dropped')) $(val).draggable('disable');
});
}
function createLineDroppable(){
$('.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');
$(ui.draggable).addClass('ui-dropped');
toggleOtherDraggable();
},
out: function(){
$('#stage-drop').droppable('enable');
}
});
}
function createStageDroppable() {
$('#stage-drop').droppable({
tolerance: 'touch',
accept: '.word',
disabled: true,
drop: function(event, 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();
});
【问题讨论】:
-
我觉得这可能与 revert: 'invalid' 有关。当我第二次尝试删除它时,它可能会认为该词无效,只是不确定为什么类没有改变,所以它仍然应该接受它。
-
你也可以显示你的 HTML 代码吗?
-
欢迎来到 Stack Overflow。我没有看到 drpped 项目附加到新空间的位置。我怀疑您的禁用功能过于贪婪并且禁用太多,因为丢弃的项目不在应有的位置。
标签: javascript jquery jquery-ui jquery-ui-droppable