1

ここに Javascript で長押しを行う良い例があります: JavaScript で長押し?

しかし、プレスの期間を知ることはできません。

プレスの長さに基づいて別のことをしたい場合は、その投稿のパターンを使用できません.

on('mousedown') 現在の時間を変数に保存してから時差を計算することで、同様のことをしようとしていましたon('mouseup')。これは、「通常の」ブラウザの通常の Javasript ページ内で正常に機能します。

ただし、私の phonegap アプリ内で何かが発生しますmouseup。指が画面上に長時間 (たとえば 5 秒間) 保持されていると、イベントが呼び出されないように見えます。

これはネイティブのモバイル ブラウザの動作ですか? どうにかして上書きすることはできますか?

jQuery モバイルではなく、普通の jQuery を使用しています。

アイデアはありますか?

4

3 に答える 3

0


クリックや長押しを特定する時間を確認できる【jQuery】

function AddButtonEventListener() {
try {
var mousedowntime;
var presstime;
$("button[id$='" + buttonID + "']").mousedown(function() {
    var d = new Date();
    mousedowntime = d.getTime();
});
$("button[id$='" + buttonID + "']").mouseup(function() {
    var d = new Date();
    presstime = d.getTime() - mousedowntime;
    if (presstime > 999/*You can decide the time*/) {
        //Do_Action_Long_Press_Event();
    }
    else {
        //Do_Action_Click_Event();
    }
});
}
catch (err) {
    alert(err.message);
}
} 
于 2012-07-12T02:35:39.247 に答える
0

tapholdand vmouseup(handleTouchEnd()行 752) イベントがjQuery モバイルソース コードでどのように実装されているかを確認できます。

すでにテストおよび実装されているため、jquery の代わりに jquery mobile を使用して変更することをお勧めします (各モバイル ブラウザーに関連するすべての「癖」を既に処理しているため)、必要に応じてコードを変更します。

于 2012-04-23T07:10:57.937 に答える
0

このソリューションは、何らかの理由で jQuery Mobile を使用しない場合に便利です。Fast Touch Event Handlingの記事を使用して、コードを追加しました

$.event.special.tap = {
distanceThreshold: 10,
timeThreshold: 350,
setup: function () {
    var self = this,
        $self = $(self);

    // Bind touch start
    $self.on('touchstart', function (startEvent) {
        // Save the target element of the start event
        var target = startEvent.target,
            touchStart = startEvent.originalEvent.touches[0],
            startX = touchStart.pageX,
            startY = touchStart.pageY,
            threshold = $.event.special.tap.distanceThreshold,
            timeout,
            expired = false;

        function timerFired() {
            expired = true;
        }

        function removeTapHandler() {
            clearTimeout(timeout);
            $self.off('touchmove', moveHandler).off('touchend', tapHandler).off('touchcancel', removeTapHandler);
        };

        function tapHandler(endEvent) {
            removeTapHandler();
            if (target == endEvent.target) {
                if (expired) {
                    $.event.simulate('longtap', self, endEvent);
                } else {
                    $.event.simulate('tap', self, endEvent);
                }
            }
        };

        // Remove tap and move handlers if the touch moves too far
        function moveHandler(moveEvent) {
            var touchMove = moveEvent.originalEvent.touches[0],
                moveX = touchMove.pageX,
                moveY = touchMove.pageY;

            if (Math.abs(moveX - startX) > threshold || Math.abs(moveY - startY) > threshold) {
                removeTapHandler();
            }
        };

        // Remove the tap and move handlers if the timeout expires
        timeout = setTimeout(timerFired, $.event.special.tap.timeThreshold);

        // When a touch starts, bind a touch end and touch move handler
        $self.on('touchmove', moveHandler).on('touchend', tapHandler).on('touchcancel', removeTapHandler);
    });
}

};

だから、今私はタップとロングタップのイベントを持っています

于 2013-09-03T10:31:42.633 に答える