How can I make a jQuery UI 'draggable()' div draggable for touchscreen?

后端 未结 7 661
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-28 00:31

I have a jQuery UI draggable() that works in Firefox and Chrome. The user interface concept is basically click to create a \"post-it\" type item.

Basica

相关标签:
7条回答
  • 2020-11-28 01:03

    jQuery ui 1.9 is going to take care of this for you. Heres a demo of the pre:

    https://dl.dropbox.com/u/3872624/lab/touch/index.html

    Just grab the jquery.mouse.ui.js out, stick it under the jQuery ui file you're loading, and that's all you should have to do! Works for sortable as well.

    This code is working great for me, but if your getting errors, an updated version of jquery.mouse.ui.js can be found here:

    Jquery-ui sortable doesn't work on touch devices based on Android or IOS

    0 讨论(0)
  • 2020-11-28 01:06

    I was struggling with a similar problem yesterday. I already had a "working" solution using jQuery UI's draggable together with jQuery Touch Punch, which are mentioned in other answers. However, using this method was causing weird bugs in some Android devices for me, and therefore I decided to write a small jQuery plugin that can make HTML elements draggable by using touch events instead of using a method that emulates fake mouse events.

    The result of this is jQuery Draggable Touch which is a simple jQuery plugin for making elements draggable, that has touch devices as it's main target by using touch events (like touchstart, touchmove, touchend, etc.). It still has a fallback that uses mouse events if the browser/device doesn't support touch events.

    0 讨论(0)
  • 2020-11-28 01:12

    This project should be helpful - maps touch events to click events in a way that allows jQuery UI to work on iPad and iPhone without any changes. Just add the JS to any existing project.

    http://code.google.com/p/jquery-ui-for-ipad-and-iphone/

    0 讨论(0)
  • 2020-11-28 01:16

    Caution: This answer was written in 2010 and technology moves fast. For a more recent solution, see @ctrl-alt-dileep's answer below.


    Depending on your needs, you may wish to try the jQuery touch plugin; you can try an example here. It works fine to drag on my iPhone, whereas jQuery UI Draggable doesn't.

    Alternatively, you can try this plugin, though that might require you to actually write your own draggable function.

    As a sidenote: Believe it or not, we're hearing increasing buzz about how touch devices such as the iPad and iPhone is causing problems both for websites using :hover/onmouseover functions and draggable content.

    If you're interested in the underlying solution for this, it's to use three new JavaScript events; ontouchstart, ontouchmove and ontouchend. Apple actually has written an article about their use, which you can find here. A simplified example can be found here. These events are used in both of the plugins I linked to.

    0 讨论(0)
  • 2020-11-28 01:17

    You can try using jquery.pep.js:

    jquery.pep.js is a lightweight jQuery plugin which turns any DOM element into a draggable object. It works across mostly all browsers, from old to new, from touch to click. I built it to serve a need in which jQuery UI’s draggable was not fulfilling, since it didn’t work on touch devices (without some hackery).

    Website, GitHub link

    0 讨论(0)
  • 2020-11-28 01:22

    After wasting many hours, I came across this!

    jquery-ui-touch-punch

    It translates tap events as click events. Remember to load the script after jquery.

    I got this working on the iPad and iPhone

    $('#movable').draggable({containment: "parent"});
    
    0 讨论(0)
提交回复
热议问题