javascript - Drag and copy with a swap function -
i have created user interface has working drag , copy in javascript. problem div getting copied has hidden div need swap once reach dropzone. upon dragging visible div gets new id hidden 1 doesn't , when swap them swaps hidden div in it's original position. here javascript code:
these work drag , copy functions.
function allowdrop(ev) { /* default handling not allow dropping elements. */ /* here allow preventing default behaviour. */ ev.preventdefault(); } function drag(ev) { /* here specified should dragged. */ /* data dropped @ place mouse button released */ /* here, want drag element itself, set it's id. */ ev.datatransfer.setdata("text/html", ev.target.id); } function drop(ev) { ev.preventdefault(); var data=ev.datatransfer.getdata("text/html"); /* if use dom manipulation functions, default behaviour not copy alter , move elements. appending ".clonenode(true)", not move original element, create copy. */ var nodecopy = document.getelementbyid(data).clonenode(true); nodecopy.id = "newid"; /* cannot use same id */ ev.target.appendchild(nodecopy); } </script> function works divs swap <script type="text/javascript"> function swapdivswithclick(div1,div2) { d1 = document.getelementbyid(div1); d2 = document.getelementbyid(div2); if( d2.style.display == "none" ) { d1.style.display = "none"; d2.style.display = "block"; } else { d1.style.display = "block"; d2.style.display = "none"; } } </script>
is there way drag hidden div along visible one? can hidden div assigned new number can swapped?
thank you
i managed solve little riddle. divs swap when dragged drop zone there's no need have them swap on mouse click. anyway here javascript functions used, had modify drag , copy previous 1 posted.
<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).clonenode(true)); } </script> {{--works swap functions--}} <script type="text/javascript"> function swapdivswithclick(div1,div2) { d1 = document.getelementbyid(div1); d2 = document.getelementbyid(div2); if( d2.style.display == "none" ) { d1.style.display = "none"; d2.style.display = "block"; } else { d1.style.display = "block"; d2.style.display = "none"; } } </script>
here 1 set of components incase helps else:
<div id="swapper-two" style="display:none; padding:0px;"> <table align="center" border="0" cellpadding="0" cellspacing="0" width="600px" draggable="true" ondragstart="drag(event)" id="drag1"> <tr> <td align="left" bgcolor="40374b" style="padding: 0px 0px 0px 60px;"> <a href="" style="font-family:helvetica neue"><img src="" valign="top" alt="" draggable="false" color: #ffffff; font-family: helvetica neue, helvetica, arial, sans-serif"</a> <td style="color:#ffffff; padding:0px 60px 5px 0px; font-family: helvetica neue, helvetica, arial, sans-serif; font-size: 12px;" bgcolor="40374b" align="right" valign="bottom"> <p><a href="" span style="text-decoration: none; color: #ffffff; font-family: helvetica neue, helvetica, arial, sans-serif" >login      </a><a href="" style="text-decoration: none; color: #ffffff; font-family: helvetica neue, helvetica, arial, sans-serif">my account</a></p> </td> </tr> </table> </div> <div class="img" draggable="true" ondragstart="drag(event)" id="drag9"> <div id="swapper-one" style="display:block; padding:0px;" > <table align="center" border="0" cellpadding="0" cellspacing="0" bgcolor="" draggable="true" ondragstart="drag(event)" id="drag1"> <tr> <td align="center"> <img src="" draggable="false" alt="" width="610" height="270" id="img-one" /> </td> </tr> </table> </div> </div>
Comments
Post a Comment