1

我正在使用 phonegap 开发的 android 应用程序中使用 jquery-ui 实​​现拖放功能。一切正常。我用于jquery.ui.touch-punch.js触摸事件。问题是我在 android 平板电脑中通过触摸拖动时没有得到帮助。助手在桌面浏览器中工作正常。

4

2 回答 2

4

这是一个工作示例:http: //jsfiddle.net/Gajotres/rjC9q/

我已经在 Android 4.1.1 和 iPad 6.0 上对其进行了测试。

要使其工作,您还需要添加以下代码:

    /iPad|iPhone|Android/.test( navigator.userAgent ) && (function( $ ) {

var proto =  $.ui.mouse.prototype,
_mouseInit = proto._mouseInit;

$.extend( proto, {
    _mouseInit: function() {
        this.element
        .bind( "touchstart." + this.widgetName, $.proxy( this, "_touchStart" ) );
        _mouseInit.apply( this, arguments );
    },

    _touchStart: function( event ) {
        /* if ( event.originalEvent.targetTouches.length != 1 ) {
            return false;
        } */

        this.element
        .bind( "touchmove." + this.widgetName, $.proxy( this, "_touchMove" ) )
        .bind( "touchend." + this.widgetName, $.proxy( this, "_touchEnd" ) );

        this._modifyEvent( event );

        $( document ).trigger($.Event("mouseup")); //reset mouseHandled flag in ui.mouse
        this._mouseDown( event );

        //return false;           
    },

    _touchMove: function( event ) {
        this._modifyEvent( event );
        this._mouseMove( event );   
    },

    _touchEnd: function( event ) {
        this.element
        .unbind( "touchmove." + this.widgetName )
        .unbind( "touchend." + this.widgetName );
        this._mouseUp( event ); 
    },

    _modifyEvent: function( event ) {
        event.which = 1;
        var target = event.originalEvent.targetTouches[0];
        event.pageX = target.clientX;
        event.pageY = target.clientY;
    }

});

})( jQuery );

我不是这段代码的作者,它是Oleg Slobodskoi

于 2013-03-06T13:21:09.837 回答
0

jquery-ui在 jquery-mobile 中?没门!他们没有针对jquery-ui移动设备进行优化。所以不要指望它会顺利运行。

您可能需要考虑其他库,例如

于 2013-03-06T11:55:30.403 回答