HTML Drag And Drop On Mobile Devices

JavascriptHtmlJquery UiMobile Devices

Javascript Problem Overview


When you add drag and drop to a web page using JavaScript, such as jQuery UI draggable and droppable, how do you get this to work when viewed via a browser on a mobile device - where the touch-screen actions for dragging are intercepted by the phone for scrolling around the page etc?

All solutions welcome... my initial thoughts are:

  1. Have a button for mobile devices that "lifts" the item to be dragged and then get them to click the zone they want to drop the item on.

  2. Write an app that does this for mobile devices rather then try and get the web page to work on them!

  3. Your suggestions and comments please.

Javascript Solutions


Solution 1 - Javascript

jQuery UI Touch Punch just solves it all.

It's a Touch Event Support for jQuery UI. Basically, it just wires touch event back to jQuery UI. Tested on iPad, iPhone, Android and other touch-enabled mobile devices. I used jQuery UI sortable and it works like a charm.

http://touchpunch.furf.com/

Solution 2 - Javascript

There is a new polyfill for translating touch events to drag-and-drop, such that HTML5 Drag And Drop is utilizable on mobile.

The polyfill was introduced by Bernardo Castilho on this post.

Here's a demo from that post.

The post also presents several considerations of the folyfill design.

Solution 3 - Javascript

I needed to create a drag and drop + rotation that works on desktop, mobile, tablet including windows phone. The last one made it more complicated (mspointer vs. touch events).

The solution came from The great Greensock library

It took some jumping through hoops to make the same object draggable and rotatable but it works perfectly

Solution 4 - Javascript

The beta version of http://www.sencha.com/products/touch/">Sencha Touch has drag and drop support.

You can refer to their http://www.sencha.com/deploy/touch/examples/dragdrop/">DnD Example. This only works on webkit browsers by the way.

Retrofitting that logic into a web page is probably going to be difficult. As I understand it they disable all browser panning and implement panning events entirely in javascript, allowing correct interpretation of drag and drop.

Update: the original example link is dead, but I found this alternative:
https://github.com/kostysh/Drag-Drop-example-for-Sencha-Touch

Solution 5 - Javascript

You might as well give a try to Tim Ruffle's drag-n-drop polyfill, certainly similar to Bernardo Castilho's one (see @remdevtec answer).

Simply do npm install mobile-drag-drop --save (other installation methods available, e.g. with bower)

Then, any element interface relying on touch detection should work on mobile (e.g. dragging only an element, instead of scrolling + dragging at the same time).

Solution 6 - Javascript

The Sortable JS library is compatible with touch screens and does not require jQuery.

The library size is 43KB.

The official website states in a video that this library is running faster than JQuery UI Sortable.

Solution 7 - Javascript

here is my solution:

$(el).on('touchstart', function(e) {
    var link = $(e.target.parentNode).closest('a')	
    if(link.length > 0) {
    	window.location.href = link.attr('href');
    }
});

Solution 8 - Javascript

Jquery Touch Punch is great but what it also does is disable all the controls on the draggable div so to prevent this you have to alter the lines... (at the time of writing - line 75)

change

if (touchHandled || !self._mouseCapture(event.originalEvent.changedTouches[0])){

to read

if (touchHandled || !self._mouseCapture(event.originalEvent.changedTouches[0]) || event.originalEvent.target.localName === 'textarea'
        || event.originalEvent.target.localName === 'input' || event.originalEvent.target.localName === 'button' || event.originalEvent.target.localName === 'li'
        || event.originalEvent.target.localName === 'a'
        || event.originalEvent.target.localName === 'select' || event.originalEvent.target.localName === 'img') {

add as many ors as you want for each of the elements you want to 'unlock'

Hope that helps someone

Solution 9 - Javascript

For vue 3, there is https://github.com/SortableJS/vue.draggable.next

For vue 2, it's https://github.com/SortableJS/Vue.Draggable

The latter you can use like this:

<draggable v-model="myArray" group="people" @start="drag=true" @end="drag=false">
   <div v-for="element in myArray" :key="element.id">{{element.name}}</div>
</draggable>

These are based on sortable.js

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionFentonView Question on Stackoverflow
Solution 1 - JavascriptvichsuView Answer on Stackoverflow
Solution 2 - JavascriptguysignerView Answer on Stackoverflow
Solution 3 - JavascriptTomer AlmogView Answer on Stackoverflow
Solution 4 - JavascriptJoeri SebrechtsView Answer on Stackoverflow
Solution 5 - JavascriptFrosty ZView Answer on Stackoverflow
Solution 6 - JavascriptZoupView Answer on Stackoverflow
Solution 7 - JavascriptAlper AydingülView Answer on Stackoverflow
Solution 8 - JavascripttedbakerView Answer on Stackoverflow
Solution 9 - JavascriptcslottyView Answer on Stackoverflow