拖动;掉落:如何让图像回到原来的位置?

时间:2013-09-23 20:23:26

标签: html css drag-and-drop

这是我所做的一般概念的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上,但是我希望它可以被丢弃到任何地方以返回原来的位置。

感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

您可以使用dragend事件确定元素是否已在droppable元素之外删除。

<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,因为它们必须是唯一的。

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);
    }
}