HTML Drag And Drop On Mobile Devices

后端 未结 8 918
栀梦
栀梦 2020-11-29 16:28

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 devic

相关标签:
8条回答
  • 2020-11-29 16:33

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

    The way it handles touch screens it that you need to touch the screen for about 1 second to start dragging an item.

    Also, they present a video test showing that this library is running faster than JQuery UI Sortable.

    0 讨论(0)
  • 2020-11-29 16:35

    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

    0 讨论(0)
  • 2020-11-29 16:37

    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.

    0 讨论(0)
  • 2020-11-29 16:37

    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

    0 讨论(0)
  • 2020-11-29 16:37

    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

    0 讨论(0)
  • 2020-11-29 16:48

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

    0 讨论(0)
提交回复
热议问题