简体   繁体   English

使用jQuery UI添加另一个图像后删除克隆的图像

[英]Remove cloned image after adding another image with jQuery UI

I have a nice solution from my previous question that successfully clones images after being dropped. 我从上一个问题中得到了一个很好的解决方案,即在删除后成功克隆图像。

Here is the code: 这是代码:

$(function() {
var makeDraggable = function(element) {
    element.draggable({ 
        revert: "invalid",
        appendTo: "#droppable",
        helper: "clone" 
    });
}
$( "#droppable" ).droppable({
    activeClass: "ui-state-default",
    hoverClass: "ui-state-hover",
    drop: function(event, ui) {
        var newClone = $(ui.helper).clone();
        makeDraggable(newClone);
        $(this).after(newClone);
    }
});
// Initalise the program by making first draggable:
makeDraggable($(".draggable img"));

But the problem is I want to show only one image at a time in the targeted area. 但问题是我想在目标区域一次只显示一个图像。 But currently all the dropped images are shown. 但目前显示所有丢弃的图像。

More specifically when user drops an image in the targeted area and later drags another image, the previous image should be removed from the dropped or targeted area and only the new image should be visible in the targeted area. 更具体地说,当用户在目标区域中放下图像并随后拖动另一图像时,应该从放下的或目标区域中移除先前的图像,并且在目标区域中仅应该看到新图像。 See this demo: jsFiddle example 看到这个演示: jsFiddle示例

How do I solve this? 我该如何解决这个问题?

In the drop method instead of using .after use .append() like this 在drop方法中,而不是像这样使用.after使用.append()

drop: function(event, ui) {
  var newClone = $(ui.helper).clone();
  makeDraggable(newClone);
  $(this).children(':not(span)').remove(); // <--- this will remove all the elements which are not a span before appending
  $(this).append(newClone);
}

Also, instead of writing drop inside div#droppable use a span like this 另外,不要在div内部写drop drop#droppable使用这样的span

<div id="droppable">
  <span>drop</span>
</div>

Here is a demo http://jsfiddle.net/dhirajbodicherla/e3pf6ays/14/ 这是一个演示http://jsfiddle.net/dhirajbodicherla/e3pf6ays/14/

You were adding the clones one after the other with the below code. 您使用以下代码一个接一个地添加克隆。

$(this).after(newClone);

All you need to do is: empty the droppable container and then add the new clones, like below: 您需要做的就是:清空droppable容器,然后添加新的克隆,如下所示:

drop: function(event, ui) {
    var newClone = $(ui.helper).clone();
    makeDraggable(newClone);
    $(this).empty().append(newClone);
}

Updated fiddle 更新了小提琴

Instead of simply emptying the droppable target area like the other answers do, I would add a class to the dropped items and then remove them upon a dragstart event of a new draggable . 我不会像其他答案那样简单地清空droppable目标区域,而是在已删除的项目中添加一个类,然后在新的draggabledragstart事件中删除它们。 Also, it's nice to add a little fadeOut() event when a new draggable is selected. 此外,在选择新的draggable时添加一个小的fadeOut()事件会很不错。 However, as Ishettyl pointed out, one must also fadeIn() the element again if the user decides not to drop the draggable. 但是,正如Ishettyl指出的那样,如果用户决定不删除可拖动的元素,那么还必须再次对该元素进行fadeIn() This can be done using a custom revert function (see below). 这可以使用自定义revert功能完成(见下文)。

The result is then something like this: 结果是这样的:

在此输入图像描述

In my opinion this looks more elegant and doesn't confuse the user whether more items are allowed. 在我看来,这看起来更优雅,并且不会混淆用户是否允许更多项目。

Code

$(function() {
    $(".draggable img").draggable({ 
        revert: function (socketObj) {
            if (!socketObj) {
                if ($(this).hasClass("droppedItems")) {
                    $(this).fadeOut(function() {
                        $(this).remove();
                    });
                } else {
                    $(".droppedItems").fadeIn();
                    return true;   
                }
            }
        },
        helper: "clone",
        start: function(event, ui) {
            $(".droppedItems").fadeOut();
        }
    }); 
    $( "#droppable" ).droppable({
        activeClass: "ui-state-default",
        hoverClass: "ui-state-hover",
        drop: function(event, ui) {
            $(".droppedItems").remove();
            var newClone = $(ui.helper).clone();
            newClone.addClass("droppedItems");
            $(this).append(newClone);
        }
    });
});

DEMO DEMO

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM