【问题标题】:When I make a draggable clone and drop it in a droppable I cannot drag it again当我制作一个可拖动的克隆并将其放入可放置的对象中时,我无法再次拖动它
【发布时间】:2010-10-26 10:21:40
【问题描述】:

当我制作一个可拖动的克隆并将其放入可放置的对象中时,我无法再次拖动它。我怎么做?其次,我只能弄清楚如何让我们 .append 将克隆添加到 droppable 中。但随后它会在任何现有元素而不是放置位置之后捕捉到左上角。

$(document).ready(function() {
    $("#container").droppable({
        drop: function(event, ui) {
            $(this).append($(ui.draggable).clone());
        }
    });
    $(".product").draggable({
        helper: 'clone'
    });
});
</script>

<div id="container">
</div>
<div id="products">
    <img id="productid_1" src="images/pic1.jpg" class="product" alt="" title="" />
    <img id="productid_2" src="images/pic2.jpg" class="product" alt="" title="" />
    <img id="productid_3" src="images/pic3.jpg" class="product" alt="" title="" />
</div>

【问题讨论】:

    标签: jquery drag-and-drop clone jquery-ui-draggable jquery-ui-droppable


    【解决方案1】:

    一种方法是:

    $(document).ready(function() {
        $("#container").droppable({
            accept: '.product',
            drop: function(event, ui) {
                $(this).append($("ui.draggable").clone());
                $("#container .product").addClass("item");
                $(".item").removeClass("ui-draggable product");
                $(".item").draggable({
                    containment: 'parent',
                    grid: [150,150]
                });
            }
        });
        $(".product").draggable({
            helper: 'clone'
        });
    });
    

    但我不确定它是否是干净整洁的编码。

    【讨论】:

    • 晚会有点晚了,但是$(this).append($("ui.draggable").clone());,应该是$(this).append($(ui.draggable).clone()); 吗? (不带引号)因为您是从回调参数加载对象?
    【解决方案2】:

    我通过 Google 找到了这个问题。我也无法阻止位置捕捉到容器,直到我在追加时将 'ui.draggable' 更改为 'ui.helper':

    $(this).append($(ui.helper).clone());
    

    【讨论】:

      【解决方案3】:

      对于那些试图重新定位丢弃的物品的人。看看这里。

      Jquery drag /drop and clone.

      我实际上不得不使用看起来像这样的代码

      $(item).css('position', 'absolute');
      $(item).css('top', ui.position.top - $(this).position().top);
      $(item).css('left', ui.position.left - $(this).position().left);
      

      去做。

      【讨论】:

        【解决方案4】:

        这是我的解决方案,它允许拖放克隆,然后根据需要通过另一个拖放替换它。它还有一个回调函数参数,可以将拖放的 div 对象传回,这样你就可以在拖放后对 jquery 选择的 div 做一些事情。

        refreshDragDrop = function(dragClassName,dropDivId, callback) {
          $( "." + dragClassName ).draggable({
            connectToSortable: "#" + dropDivId,
            helper: "clone",
            revert: "invalid"
          });
          $("#" + dropDivId).droppable({
            accept: '.' + dragClassName,
            drop: function (event, ui) {
              var $this = $(this),
                maxItemsCount = 1;
              if ($this.children('div').length == maxItemsCount ){
                //too many item,just replace
                $(this).html($(ui.draggable).clone());
                //ui.sender.draggable('cancel');
              } else {
                $(this).append($(ui.draggable).clone());
              }
              if (typeof callback == "function") callback($this.children('div'));
            }
          });
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-01-23
          • 2011-07-30
          • 2011-12-30
          相关资源
          最近更新 更多