javascript - how to drag sortable divs on to a foreign element -
javascript - how to drag sortable divs on to a foreign element -
i drag divs of sortable container , drop them on droppable div (which not sortable). have tried using html5 drag , drop events, none of them getting fired. importantly, dragstart event on sortable container not getting fired. can suggest me solution. want dragstart event fired. here finish code:
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>jquery ui sortable - handle empty divsts</title> <script src="//code.jquery.com/jquery-1.10.2.js"></script> <script src="//code.jquery.com/ui/1.11.2/jquery-ui.js"></script> <style> .draggabledivs{ background-color: orange; border: solid black; } </style> </head> <body> <div id="sortable" style="float: left;"> <div class="draggabledivs" draggable="true">can dropped !!..</div> <div class="draggabledivs" draggable="true">..on empty list</div> <div class="draggabledivs" draggable="true">item 3</div> <div class="draggabledivs" draggable="true">item 4</div> <div class="draggabledivs" draggable="true">item 5</div> </div> <div id="droptarget" style="width: 500px; height: 500px; background-color: skyblue; float: left; text-align: center"> <h4>drop here</h4> </div> <script> $("#sortable").sortable(); document.getelementbyid('sortable').addeventlistener('dragstart', function(evt) { console.log("the 'dragstart' event fired."); evt.datatransfer.setdata('text', evt.target.textcontent); }, false); </script> </body> </html>
you can accomplish functionality follows using jquery ui:
basically clone element beingness dragged , append droppable on drop event, since jquery ui throws errors @ if remove element beingness dragged.
then remove actual element in stop event callback.
class="snippet-code-js lang-js prettyprint-override">$("#sortable").sortable(); $("#droptarget").droppable({ accept: "div", drop: function(event, ui) { ui.draggable.clone().appendto(this); ui.draggable.remove(); } })
class="snippet-code-css lang-css prettyprint-override">#sortable { float: left; } .draggabledivs { background-color: orange; border: solid black; } #droptarget { width: 500px; height: 500px; float: left; text-align: center; background-color: skyblue; }
class="snippet-code-html lang-html prettyprint-override"><link href="http://code.jquery.com/ui/1.9.2/themes/base/jquery-ui.css" rel="stylesheet" /> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="http://code.jquery.com/ui/1.9.2/jquery-ui.js"></script> <div id="sortable"> <div class="draggabledivs">can dropped !!..</div> <div class="draggabledivs">..on empty list</div> <div class="draggabledivs">item 3</div> <div class="draggabledivs">item 4</div> <div class="draggabledivs">item 5</div> </div> <div id="droptarget"> <h4>drop here</h4> </div>
side notes:
don't mix native drag , drop jquery ui. don't mix inline styles , internal/external styles. avoid using inline styles @ if possible. why utilize css? javascript jquery html jquery-ui
Comments
Post a Comment