114

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.

6
  • Which devices are you targeting?
    – Marko
    Commented Jul 3, 2010 at 23:46
  • 31
    @Marko - all of them. It isn't The Web if I exclude anyone.
    – Fenton
    Commented Jan 22, 2012 at 21:13
  • If you don't want jQuery at all, take a look at this github.com/capriza/mobile-touch
    – oriharel
    Commented Jul 7, 2015 at 19:45
  • 2
    Asked 10 years, 5 months ago Active 1 year, 2 months ago Viewed 131k times
    – Jason FB
    Commented Dec 1, 2020 at 21:16
  • 2
    It would be really nice if a SINGLE ONE of the many answers below actually discussed how to do it rather than advertise a third party library. And echoing @JasonFB, amazing that in - now - 2022, there is still no clear answer. Commented May 3, 2022 at 18:33

9 Answers 9

103

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/

9
  • 4
    Doesn't work on the stock Android 4.0 browser. It does work on mobile Chrome though.
    – Timmmm
    Commented Nov 4, 2012 at 12:14
  • do we have long press first before drag? because when we used full width in element that drag able then we want to scroll to bottom it automatically drags the element Commented Feb 4, 2014 at 5:16
  • 1
    make sure to make this your last <script> declaration in the head of your html file, especially jquery-ui, or it won't work
    – whyoz
    Commented Jun 5, 2014 at 0:27
  • 1
    What about people who don't want to include jQuery UI since it is a huge library just to handle touch drag and drop? Is there an alternative?
    – Kevin
    Commented Jan 29, 2018 at 21:00
  • 2
    touchpunch.furf.com is 404, the solution is missing
    – Bangyno
    Commented Sep 16, 2021 at 9:04
31

There is a 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 polyfill design.

1
  • 1
    no working on mobile devices
    – Dima Malko
    Commented Dec 3, 2021 at 12:58
9

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

4
  • License type? Free? I checked out the site, but it seems like they are selling the library.
    – Shmack
    Commented Jun 27, 2020 at 0:05
  • It's been 6 years since I answered this. I am not sure if they changed their license type. It used to be free back in 2014... Commented Jun 27, 2020 at 23:20
  • IDK how I overlooked this, but greensock.com/licensing
    – Shmack
    Commented Jun 29, 2020 at 19:41
  • Basically, its free provided that your product is free (with the exception that they permit one time charges), other wise, you have to get a business license with them.
    – Shmack
    Commented Jun 29, 2020 at 19:56
8
+50

The beta version of Sencha Touch has drag and drop support.

You can refer to their 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

3
  • This is very interesting, so I will download it and have a play - tested on Android and it mostly works, so just need to look at the details - thanks!
    – Fenton
    Commented Jul 7, 2010 at 13:38
  • 6
    Okay, I probably won't use that library in particular (what with it being over 200k when minified), but I can use the concepts contained within it to get the kind of idea. The general idea is that you make your page cannot be interpreted as greater than the size of the screen, which fools the mobile browser into not intercepting the swipes / drags!
    – Fenton
    Commented Jul 8, 2010 at 14:50
  • 1
    Sohnee, we're adding a builder for Touch 1.0 that strips out unused parts of the library, this will get the footprint down substantially. Also, gzipped, the full library is 50-80k. Commented Aug 1, 2010 at 22:25
7

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.

4
  • Look like this one is working even though it looks more like CDK Drag Drop library of Angular 10 though. Commented Jul 13, 2021 at 14:42
  • 1
    Tested on Firefox Nightly 92.0a1 on Android 9: seems to work, but long touch triggers scrolling and dragging at the same time, possibly because long touch is normally used for text selection
    – darw
    Commented Aug 2, 2021 at 10:41
  • IMO this answer is underappreciated. Library works for me on windows touchscreen (which many answers here did not), is opensource/mit, and comes with great examples and packages for most popular web frameworks. (react-sortablejs was exactly what i needed) Commented Aug 31, 2021 at 16:43
  • @Zoup, this is a very nice and useful advice. Commented Feb 4, 2023 at 19:23
5

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

2
1

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

1
  • It most definetly helped me, thank you. I was creating a sortable checklist and the tick box was not clicking as the div created by touch-punch was covering the checkbox. This solved it and it worked. thanks....
    – Mikeys4u
    Commented Jan 5, 2020 at 0:42
1

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');
    }
});
0

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.