2012-04-23 7 views
1

jqueryのPhoneGapのアプリ内のJavaScriptで長押しを時限:Long Press in JavaScript?はここJavaScriptで長押しを行うための良い例があり

しかし、それはプレスの期間を知るために用意されていません。

私がプレスの長さに基づいて異なることをしたいのであれば、そのポストのパターンは使用できません。

私は変数( 'mousedown') に現在の時刻を保存し、( 'mouseup')で時間差を計算することで同様のことをしようとしていました。 通常のjavasriptページ内では、これは正常なブラウザで正常に動作します。

しかし、私のphonegapアプリでは何かが起こります。 は、指が長い時間(例えば5秒..)の間、指に触れているとmouseupイベントが呼び出されないようです。

これはネイティブモバイルブラウザの動作ですか?何とかそれを無効にすることはできますか?

私はjQueryモバイルではなくプレーンjQueryを使用しています。

答えて

0

tapholdvmouseuphandleTouchEnd() line 752)イベントがjQuery mobileソースコードにどのように実装されているかをご覧ください。

すでにテストされ実装されているので、jqueryの代わりにjquery mobileを使用して修正することをお勧めします(既に各モバイルブラウザに関連したすべての 'quirks'を処理しているので)。

0


あなたが識別するために、時間を確認することができますクリックするか、あなたには、いくつかの理由のためのjQuery Mobileを使用していない場合は、このソリューションが便利であることを長く押して、[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); 
} 
} 
0

注意。私は記事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); 
    }); 
} 

}の部分を加え 。

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

関連する問題