简体   繁体   English

在Phonegap android上可拖动的jQuery

[英]Draggable jQuery on Phonegap android

I'm having some problems while implementing jQuery Draggable http://jqueryui.com/demos/draggable/ on Android. 我在Android上实现jQuery Draggable http://jqueryui.com/demos/draggable/时遇到了一些问题。 I've created the html code and tested it using my browser. 我已经创建了html代码并使用我的浏览器对其进行了测试。 Then I compiled it on build.phonegap.com. 然后我在build.phonegap.com上编译它。 After getting the .apk I installed it on my xperia x8 which is using Android 2.2 Froyo. 获得.apk后,我将它安装在使用Android 2.2 Froyo的xp​​eria x8上。 The application ran well, but when I dragged the object it won't move.. Is there something wrong with my method? 应用程序运行良好,但是当我拖动对象时它不会移动..我的方法有问题吗? Thanks 谢谢

This code maps touchstart , touchmove and touchend to their appropriate mouse events: There is still a bug with touchstart / mousedown , because the coordinates cannot be mapped directly. 此代码将touchstarttouchmovetouchend到适当的鼠标事件: touchstart / mousedown仍然存在错误,因为无法直接映射坐标。

My idea to fix this is to delay the mousedown dispatch until a touchmove occurs and then dispatch both mouse events with the coordinates of touchmove . 我想解决这个问题的方法是延迟mousedown dispatch,直到发生touchmove ,然后用touchmove的坐标发送两个鼠标事件。

var mouseEventTypes = {
    touchstart : "mousedown",
    touchmove : "mousemove",
    touchend : "mouseup"
};

for (originalType in mouseEventTypes) {
    document.addEventListener(originalType, function(originalEvent) {
        event = document.createEvent("MouseEvents");
        touch = originalEvent.changedTouches[0];
        event.initMouseEvent(mouseEventTypes[originalEvent.type], true, true,
                window, 0, touch.screenX, touch.screenY, touch.clientX,
                touch.clientY, touch.ctrlKey, touch.altKey, touch.shiftKey,
                touch.metaKey, 0, null);
        originalEvent.target.dispatchEvent(event);
    });
}

I use some script but forget where i take it: 我使用了一些脚本,但忘记了我的用法:

 *
 * Copyright 2011, Dave Furfero
 * Dual licensed under the MIT or GPL Version 2 licenses.
 *
 * Depends:
 *  jquery.ui.widget.js
 *  jquery.ui.mouse.js
 */
(function(b){b.support.touch="ontouchend" in document;if(!b.support.touch){return;}var c=b.ui.mouse.prototype,e=c._mouseInit,a;function d(g,h){if(g.originalEvent.touches.length>1){return;}g.preventDefault();var i=g.originalEvent.changedTouches[0],f=document.createEvent("MouseEvents");f.initMouseEvent(h,true,true,window,1,i.screenX,i.screenY,i.clientX,i.clientY,false,false,false,false,0,null);g.target.dispatchEvent(f);}c._touchStart=function(g){var f=this;if(a||!f._mouseCapture(g.originalEvent.changedTouches[0])){return;}a=true;f._touchMoved=false;d(g,"mouseover");d(g,"mousemove");d(g,"mousedown");};c._touchMove=function(f){if(!a){return;}this._touchMoved=true;d(f,"mousemove");};c._touchEnd=function(f){if(!a){return;}d(f,"mouseup");d(f,"mouseout");if(!this._touchMoved){d(f,"click");}a=false;};c._mouseInit=function(){var f=this;f.element.bind("touchstart",b.proxy(f,"_touchStart")).bind("touchmove",b.proxy(f,"_touchMove")).bind("touchend",b.proxy(f,"_touchEnd"));e.call(f);};})(jQuery);

It work perfect with jquery-ui. 它与jquery-ui完美配合。 U just need to add it to your html code like js library 你只需要将它添加到你的html代码,如js库

I used mixture of solutions above and realized that it helped with draggable elements, but it also made some elements in my project without ability to trigger click events on them, because the code above was preventing touchend events. 我使用上述解决方案的混合物,并意识到它有助于可拖动元素,但它也使我的项目中的一些元素无法触发它们上的点击事件,因为上面的代码阻止了touchend事件。 I solved this problem. 我解决了这个问题。 My version keeps draggable elements working and also keeps ability of other document elements to receive click events: 我的版本使可拖动元素工作,并保持其他文档元素接收点击事件的能力:

(function() {
function init() {
    var mouseEventTypes = {
        touchstart : "mousedown",
        touchmove : "mousemove",
        touchend : "mouseup"
    };

    for (originalType in mouseEventTypes) {
        document.addEventListener(originalType, function(originalEvent) {
            if(originalEvent.type == 'click')
                return;
            if (originalEvent.type != 'touchstart' && originalEvent.type !='touchend'){
                originalEvent.preventDefault();
            }
            event = document.createEvent("MouseEvents");
            touch = originalEvent.changedTouches[0];
            event.initMouseEvent(mouseEventTypes[originalEvent.type], true, true, window, 0, touch.screenX, touch.screenY, touch.clientX, touch.clientY, touch.ctrlKey, touch.altKey, touch.shiftKey, touch.metaKey, 0, null);
            originalEvent.target.dispatchEvent(event);
            event.preventDefault();         
        });
    }
}

init();
})();

Maybe it will help someone with similar issue. 也许它会帮助有类似问题的人。 This problem with draggable elements occured only on my Galaxy Tab GT-P5110, Android version 4.0.4. 可拖动元素的问题仅发生在我的Galaxy Tab GT-P5110,Android版本4.0.4上。

To make clicks also work... 使点击也有效......

            var mouseEventTypes = {
                touchstart : "mousedown",
                touchmove : "mousemove",
                touchend : "mouseup",
                click : clickEvent
            };

            for(originalType in mouseEventTypes) {
                document.addEventListener(originalType, function(originalEvent) {
                    if (originalEvent.type != 'click' && originalEvent.type != 'touchstart')
                        originalEvent.preventDefault();
                    event = document.createEvent("MouseEvents");
                    touch = originalEvent.changedTouches[0];
                    event.initMouseEvent(mouseEventTypes[originalEvent.type], true, true, window, 0, touch.screenX, touch.screenY, touch.clientX, touch.clientY, touch.ctrlKey, touch.altKey, touch.shiftKey, touch.metaKey, 0, null);
                    originalEvent.target.dispatchEvent(event);
                    event.preventDefault();
                });

you can check out this question for a couple of suggestions and explanation of what is/isn't possible right now. 你可以查看这个问题,以获得一些建议,并解释现在不可能的内容。 It's my opinion that jQuery Mobile will probably have this as a possibility sooner or later. 我认为jQuery Mobile迟早会有这种可能性。

As an extension to Nappy's answer: 作为Nappy答案的延伸:

I have tried his code snippet on an app I am building for Android devices. 我在我为Android设备构建的应用上尝试了他的代码片段。 It compiles fine and I am able to drag (in my case, a square) around, however the touchmove mouse event type only fires for 2 seconds and then stops. 它编译得很好,我可以拖动(在我的情况下,一个正方形),但是touchmove鼠标事件类型只会触发2秒然后停止。

This gives the impression that the object is not draggable, however on release, the object is moved to that location. 这给人的印象是对象不可拖动,但是在释放时,对象被移动到该位置。

To fix this a simple one line of code is needed within the event listener: 要解决此问题,在事件侦听器中需要一行简单的代码:

    <script type="text/javascript" charset="utf-8">

    // Wait for PhoneGap to load
    //
    document.addEventListener("deviceready", onDeviceReady, false);

    // PhoneGap is ready
    //
    function onDeviceReady() {

    $(".shape").draggable();

    var mouseEventTypes = {
    touchstart : "mousedown",
    touchmove : "mousemove",
    touchend : "mouseup"
    };

for (originalType in mouseEventTypes) {
    document.addEventListener(originalType, function(originalEvent) {

originalEvent.preventDefault(); originalEvent.preventDefault();

        event = document.createEvent("MouseEvents");
        touch = originalEvent.changedTouches[0];
        event.initMouseEvent(mouseEventTypes[originalEvent.type], true, true,
                window, 0, touch.screenX, touch.screenY, touch.clientX,
                touch.clientY, touch.ctrlKey, touch.altKey, touch.shiftKey,
                touch.metaKey, 0, null);
        originalEvent.target.dispatchEvent(event);
             event.preventDefault();

    });
}
    }

    </script>

The override of preventDefault fixes the issue with the mousedown handler not firing. preventDefault的覆盖修复了mousedown处理程序未触发的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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