2010-03-16 146 views
23

我有一个div,其中应用了jQuery UI Draggable。我想要做的是单击并拖动它,然后创建一个保存在dom中但不会在删除时删除的克隆。jQuery UI:从原始div拖放和克隆,但保留克隆

想象一副扑克牌,我的盒子元素是套牌,我想从牌组中拉出扑克牌/ div,并将它们放在我的页面周围,但它们将是原始div的克隆。我只是想确保你不能创建另一个克隆的div的克隆。

我用以下,没有工作像我想:

$(".box").draggable({ 
     axis: 'y', 
     containment: 'html', 
     start: function(event, ui) { 
      $(this).clone().appendTo('body'); 
     } 
}); 

我想通了,我的解决方案:

$(".box-clone").live('mouseover', function() { 
    $(this).draggable({ 
     axis: 'y', 
     containment: 'html' 
    }); 
}); 
$(".box").draggable({ 
    axis: 'y', 
    containment: 'html', 
    helper: 'clone' 
    stop: function(event, ui) { 
     $(ui.helper).clone(true).removeClass('box ui-draggable ui-draggable-dragging').addClass('box-clone').appendTo('body'); 
    } 
}); 
+4

您可以发布您的解决方案作为答案,然后接受它。 :) – 2010-03-17 01:32:22

+18

你*应*发布您的解决方案作为答案,然后接受它:) – Anurag 2010-03-17 01:34:05

回答

19

以下是我最终做过的工作:

$(".box-clone").live('mouseover', function() { 
    $(this).draggable({ 
     axis: 'y', 
     containment: 'html' 
    }); 
}); 
$(".box").draggable({ 
    axis: 'y', 
    containment: 'html', 
    helper: 'clone', 
    stop: function(event, ui) { 
     $(ui.helper).clone(true).removeClass('box ui-draggable ui-draggable-dragging').addClass('box-clone').appendTo('body'); 
    } 
}); 
2

这里是他的解决方案:

$(".box-clone").live('mouseover', function() { 
    $(this).draggable({ 
     axis: 'y', 
     containment: 'html' 
    }); 
}); 
$(".box").draggable({ 
    axis: 'y', 
    containment: 'html', 
    helper: 'clone' 
    stop: function(event, ui) { 
     $(ui.helper).clone(true).removeClass('box ui-draggable ui-draggable-dragging').addClass('box-clone').appendTo('body'); 
    } 
}); 
+0

伟大的,谢谢:) – vondip 2011-01-20 06:42:28

7

如果你特林移动元素(比如< LI/>)从#source < UL/>到#destination < UL/>,并且希望他们能够克隆(而不是移动),并且仍然在右边排序,我发现这个解决方案:

$(function() { 

    $("#source li").draggable({ 
     connectToSortable: '#destination', 
     helper: 'clone' 
    }) 

    $("#destination").sortable(); 

    }); 

我知道这似乎超简单,但它为我工作! :)

+0

+1它的辉煌 – Val 2014-05-07 09:39:20

+1

这对我很好,但我如何获得克隆的对象? – arpo 2015-05-01 16:06:39

0

下面是我如何得到它的工作: PS:'标记'是要拖动的对象,'地图'是目标容器。

$(document).ready(function() { 
    //source flag whether the drag is on the marker tool template 
    var template = 0; 
    //variable index 
    var j = 0; 
    $(".marker").draggable({ 
     helper: 'clone', 
     snap: '.map', 
     start: function(event, ui) { 
      //set the marker tool template 
      template = 1; 
     } 
    }); 
    $(".map").droppable({ 
     accept: ".marker", 
     drop: function(event, ui) { 
      $(this).find('.map').remove(); 
      var item = $(ui.helper); 
      var objectid = item.attr('id'); 
      //if the drag is on the marker tool template 
      if (template == 1) { 
       var cloned = $(item.clone()); 
       cloned.attr('id', 'marker' + j++); 
       objectid = cloned.attr('id'); 
       cloned.draggable({ 
        containment: '.map', 
        cursor: 'move', 
        snap: '.map', 
        start: function(event, ui) { 
         //Reset the drag source flag 
         template = 0; 
        } 
       }); 
       cloned.bind("contextmenu", function(e) { 
        cloned.remove(); 
        return false; 
       }); 
       $(this).append(cloned); 
      } 
      i++; 
      var offsetXPos = parseInt(ui.offset.left - $(this).offset().left); 
      var offsetYPos = parseInt(ui.offset.top - $(this).offset().top); 
      alert("Dragged " + objectid + " to (" + offsetXPos + "," + offsetYPos + ")"); 
     } 
    }); 
});