Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML5 Drag & Drop for Mobile

This is my implementation of WHATWG HTML5 Drag and Drop:

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.innerHTML+=" <p>"+document.getElementById(data).innerHTML+"</p>";
}
.div {
    width:350px;height:70px;padding:10px;border:1px solid #aaaaaa;
}
<div class="div" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
    <div class="div" ondrop="drop(event)" ondragover="allowDrop(event)">
    <button id="drag1" draggable="true" ondragstart="drag(event)" width="336" height="69">Code</button>
    <button id="drag2" draggable="true" ondragstart="drag(event)" width="336" height="69">Code</button>
</div>

It works fine in Google Chrome, but not in iOS or Android. I have read other topics about this but most of them suggest using jQuery or a JavaScript plugin.

Is it possible to make the HTML drag and drop implementation work on iOS and Android (mobile devices)?

like image 293
VCNinc Avatar asked Dec 20 '22 18:12

VCNinc


2 Answers

I had the same need and wrote a polyfill that enables HTML5 drag/drop on mobile devices (Android and IOS):

https://github.com/Bernardo-Castilho/dragdroptouch

Hope this works for you.

like image 149
Bernardo Avatar answered Jan 08 '23 18:01

Bernardo


Unfortunately Drag and Drop is not currently supported by any mobile browsers except Internet Explorer Mobile 10 onwards and a single deprecated Presto version of Opera (which has now been replaced by Webkit based versions). See http://caniuse.com/#feat=dragndrop

The best solution would be to detect support for drag and drop (ensuring that this does not give you false positives in mobile versions of browsers that do support the API) and then use JavaScript to polyfill on mobile devices. This will also help provide support in legacy browser versions that pre-date native support for drag and drop.

You can use the excellent YepNope conditional loading library to run the test and then conditionally load one or more scripts to provide JavaScript support for drag and drop, or you could use Modernizr to both carry out the test and load the scripts.

like image 28
pwdst Avatar answered Jan 08 '23 19:01

pwdst