0

这是我要做什么的一般概念的 jsFiddle:

http://jsfiddle.net/6xRXj/1/

HTML & JS:

<!DOCTYPE HTML>
<script>
function allowDrop(ev)
{
ev.preventDefault();
}

function drag(ev)
{
ev.dataTransfer.setData("Text",ev.target.id);
}

function drop(ev)
{
ev.preventDefault();
var data=ev.dataTransfer.getData("Text");
ev.target.appendChild(document.getElementById(data));
}
</script>

<body>

    <div id="bracelet">

<div id="div1" ondrop="drop(event)" ondragover="allowDrop(event)"></div>

<div id="div1" ondrop="drop(event)" ondragover="allowDrop(event)"></div>

<div id="div1" ondrop="drop(event)" ondragover="allowDrop(event)"></div>

    </div>

    <br/><br/><br/><br/><br/>

<div id="div2" ondrop="drop(event)" ondragover="allowDrop(event)">
<img id="drag1" src= "http://upload.wikimedia.org/wikipedia/commons/4/4f/Button-Red.svg" draggable="true" ondragstart="drag(event)" width="50" height="50">
</div>

    <div id="div2" ondrop="drop(event)" ondragover="allowDrop(event)">
<img id="drag2" src= "http://upload.wikimedia.org/wikipedia/commons/a/a8/Button-Blue.svg" draggable="true" ondragstart="drag(event)" width="50" height="50">
</div>

        <div id="div2" ondrop="drop(event)" ondragover="allowDrop(event)">
<img id="drag3" src= "http://upload.wikimedia.org/wikipedia/commons/c/ca/Button-Lightblue.svg" draggable="true" ondragstart="drag(event)" width="50" height="50">
</div>

            <div id="div2" ondrop="drop(event)" ondragover="allowDrop(event)">
<img id="drag4" src= "http://upload.wikimedia.org/wikipedia/commons/b/ba/Button-Purple.svg" draggable="true" ondragstart="drag(event)" width="50" height="50">
</div>

                <div id="div2" ondrop="drop(event)" ondragover="allowDrop(event)">
<img id="drag5" src= "http://upload.wikimedia.org/wikipedia/commons/6/68/Button-Orange.svg" draggable="true" ondragstart="drag(event)" width="50" height="50">
</div>

                    <div id="div2" ondrop="drop(event)" ondragover="allowDrop(event)">
<img id="drag6" src= "http://upload.wikimedia.org/wikipedia/commons/d/dc/Button-Green.svg" draggable="true" ondragstart="drag(event)" width="50" height="50">
</div>

                        <div id="div2" ondrop="drop(event)" ondragover="allowDrop(event)">
<img id="drag7" src= "http://upload.wikimedia.org/wikipedia/commons/4/4f/Button-Red.svg" draggable="true" ondragstart="drag(event)" width="50" height="50">
</div>

    <div id="div2" ondrop="drop(event)" ondragover="allowDrop(event)">
<img id="drag8" src= "http://upload.wikimedia.org/wikipedia/commons/a/a8/Button-Blue.svg" draggable="true" ondragstart="drag(event)" width="50" height="50">
</div>

        <div id="div2" ondrop="drop(event)" ondragover="allowDrop(event)">
<img id="drag9" src= "http://upload.wikimedia.org/wikipedia/commons/c/ca/Button-Lightblue.svg" draggable="true" ondragstart="drag(event)" width="50" height="50">
</div>

            <div id="div2" ondrop="drop(event)" ondragover="allowDrop(event)">
<img id="drag10" src= "http://upload.wikimedia.org/wikipedia/commons/b/ba/Button-Purple.svg" draggable="true" ondragstart="drag(event)" width="50" height="50">
</div>

                <div id="div2" ondrop="drop(event)" ondragover="allowDrop(event)">
<img id="drag11" src= "http://upload.wikimedia.org/wikipedia/commons/6/68/Button-Orange.svg" draggable="true" ondragstart="drag(event)" width="50" height="50">
</div>

                    <div id="div2" ondrop="drop(event)" ondragover="allowDrop(event)">
<img id="drag12" src= "http://upload.wikimedia.org/wikipedia/commons/d/dc/Button-Green.svg" draggable="true" ondragstart="drag(event)" width="50" height="50">
</div>

CSS:

#div1 {
    width:50px;
    height:50px;
    padding:10px;
    border:1px solid #aaaaaa;
    display: inline;
    float: left;
    margin-bottom: 25px;
    margin-left: 50px
}

#div2 {
    width:50px;
    height:50px;
    padding:10px;
    border:1px solid #aaaaaa;
    display: inline;
    float: left;
}

#bracelet {
    width:420px;
    height:75px;
    padding: 10px;
    margin-left: 30px;
    border:1px solid #aaaaaa;
    background-color: gray;   
}

我想要完成的是能够将图像移动到放置点,然后将其放置在其他任何地方并让它返回到原来的位置。目前它只能放置在指定的div上,但我希望它能够被放置在任何地方以返回到原来的位置。

任何帮助表示赞赏。

4

1 回答 1

1

dragend您可以通过使用事件来确定元素是否被放置在可放置元素之外。

<img ... draggable="true" ondragend="dragEnd(event)" ondragstart="drag(event)">
function dragEnd(ev){
    if(ev.dataTransfer.dropEffect == "none"){

        // REVERT BACK TO ORIGINAL POSITION

    }
}

然后,您必须决定如何指定“原始位置”。您可能会考虑将id原始父级的 存储在一个data-属性中,然后读取该属性并将其附加回该父级。另一种选择可能是data-在初始页面加载时甚至在第一次加载时通过 javascript 填充这些属性dragstart

编辑:工作示例:

http://jsfiddle.net/6xRXj/3/

您会注意到我必须消除您重复id的 s,因为它们必须是唯一的。

function drag(ev) {
    var el = ev.target;
    var parent = el.getAttribute("data-parent");

    // if the parent data attribute is not yet set, set it to current parent
    if(!parent){
        el.setAttribute("data-parent", el.parentNode.id);
    }

    ev.dataTransfer.setData("Text", el.id);
}

function dragEnd(ev){
    if(ev.dataTransfer.dropEffect == "none"){

        // get parent from data attribute
        var parent = document.getElementById(ev.target.getAttribute("data-parent"));

        // append back to the original parent            
        parent.appendChild(ev.target);
    }
}
于 2013-09-23T20:56:09.007 回答