css - html span could drag into its friends -
i have 2 divs both contains spans, want these spans drag , drop between these 2 divs , should not drop other elements, strangth thing these spans drop other span, i.e. if span inside dragable div, span can drop it. here codes:
<div class="row"> <div id="div1" ondrop="drop(event)" ondragover="allowdrop(event)" style=" background-color:#555;"></div> <div id="div2" ondrop="drop(event)" ondragover="allowdrop(event)" style=" background-color:#bbb;"> <span id="drag1" draggable="true" ondragstart="drag(event)" style="background-color:#f00;">drag1</span> <span id="drag2" draggable="true" ondragstart="drag(event)" style="background-color:#0f0;">drag2</span> <span id="drag3" draggable="true" ondragstart="drag(event)" style="background-color:#00f;">drag3</span> </div> </div> <style type="text/css"> .row div span { display: block; height: 30px; float: left; padding: 5px; margin: 0 5px; color: #fff; } .row div { height: 40px; margin: 5px; padding: 5px; } </style> <script type="text/javascript"> 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"); var id = document.getelementbyid(data); ev.target.appendchild(id); return data; } </script>
and here running result: screenshot
as can see in pic, span "drag2" (green) has been droped "drag1" (red), not allowed us, spans should allowed drop parent divs, there way avoid that. thanks
Comments
Post a Comment