javascript - how to make multiple draggable clones?

╄→гoц情女王★ 提交于 2019-12-12 03:56:59

问题


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:


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/



来源:https://stackoverflow.com/questions/16055184/javascript-how-to-make-multiple-draggable-clones

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!