javascript - how to make multiple draggable clones

2019-09-08 17:01发布

now I'm trying to make a simple drag and drop game.

The first time I drag and drop a clone works fine, but it doesn't allow me to drag a clone anymore.

So I want to create as many clones as I drag.... and I have no idea how to do it.

Please take a look at my code first.

    function init(){
    var xCoordinate;
    var yCoordinate;
    var itemName;

    $('#burger, #chicken, #fries, #hotdog, #soda').draggable({
        containment: '#screen',
        start: getPosition,
        helper: 'clone',
        stop: dragStop,
        revert: 'invalid'
    });

    $('#A, #B, #C').droppable({
        drop: itemDrop
    });
}

function getPosition(event, ui){
    xCoordinate = ui.offset.left;
    yCoordinate = ui.offset.top;
};

"function getPosition" is simply to get x and y coordinates of the original draggable item so that the clone can be placed at the same position.

I understand I have to comand 'duplicate clones!!' under the droppable function, but don't know how to do it.

1条回答
够拽才男人
2楼-- · 2019-09-08 17:31

The code should provide a function for the drop property. This function should clone the helper and append it to the droppable. I have provided a basic example which you can apply to your implementation.

HTML

<ul>
    <li>Item 1</li>
    <li>Item 2</li>
</ul>
<div id="drop"></div>

Javascript

$("li").draggable(
    {helper: "clone"}
);

$("#drop").droppable({
    accept: "li",
    drop: function(event,ui){
        console.log(ui.helper);
       $(this).append($(ui.helper).html());    
    }
});

Working Example http://jsfiddle.net/2W4jA/

查看更多
登录 后发表回答