简体   繁体   中英

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)?

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.

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.

Since most of mobile browser still do not support HTML5 drag drop, so just in case some still looking solution, I have created a simple drag and drop that works in most of the mobile browser using Raphael. http://xtrace.blogspot.de/2013/01/simple-drag-and-drop-with-raphael.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM