使用 jQuery 拖放元素

drag and drop elements with jQuery

也许你能帮我,我想创建一个带有可拖动(不是克隆)div 的列,我可以将其放入由四个 div 组成的容器中 (bootstrap col-md-3 ).可拖动元素有两种尺寸:col-md-3 或 col-md-6。我发现了类似的东西 (http://jsfiddle.net/py3DE/) 但我无法重写它以使其正常工作

    $(".source .item").draggable({ revert: "invalid", appendTo: 'body', helper: 'clone',
    start: function(ev, ui){ ui.helper.width($(this).width()); }                    // ensure helper width
});

$(".target .empty").droppable({ tolerance: 'pointer', hoverClass: 'highlight',
    drop: function(ev, ui){
        var item = ui.draggable;
        if (!ui.draggable.closest('.empty').length) item = item.clone().draggable();// if item was dragged from the source list - clone it
        this.innerHTML = '';                                                        // clean the placeholder
        item.css({ top: 0, left: 0 }).appendTo(this);                                // append item to placeholder
    }
});

$(".target").on('click', '.closer', function(){
    var item = $(this).closest('.item');
    item.fadeTo(200, 0, function(){ item.remove(); })
});

如果您将 'item.clone().draggable()' 更新为 'item.draggable()',它不会克隆。

js code

  if (!ui.draggable.closest('.empty').length) item = item.draggable()