diff --git a/README.md b/README.md index 5d259f1..8e54594 100644 --- a/README.md +++ b/README.md @@ -77,7 +77,10 @@ However, there are exceptions to this pattern. Please see below. ### touchy-longpress phase: "start" or "end" -(No data object) + +data: + +* startPoint ### touchy-drag @@ -159,6 +162,7 @@ As shown in the example, the settings are accessed through the camelCased name o * requiredTouches: 1 * msThresh: 800 (the number of milliseconds the user must touch the element before the event is fired) * triggerStartPhase: false (whether to trigger the event during the start phase) +* moveTolerance: 2 (distance in pixel allowed during the delay, use negative value for infinite distance) ### touchy-drag diff --git a/jquery.touchy.js b/jquery.touchy.js index 21fd041..12d5484 100644 --- a/jquery.touchy.js +++ b/jquery.touchy.js @@ -47,10 +47,12 @@ requiredTouches: 1, msThresh: 800, triggerStartPhase: false, + moveTolerance: 2, data: { - startDate: null + startDate: null, + startPoint: null }, - proxyEvents: ["TouchStart", "TouchEnd"] + proxyEvents: ["TouchStart", "TouchEnd", "TouchMove"] }, drag: { // the user touches the element and then moves his or her finger across the screen preventDefault: { @@ -175,10 +177,10 @@ }; data.startDate = e.timeStamp; if (settings.triggerStartPhase) { - $target.trigger('touchy-longpress', ['start', $target]); + $target.trigger('touchy-longpress', ['start', $target, data]); } data.timer = setTimeout($.proxy(function(){ - $target.trigger('touchy-longpress', ['end', $target]); + $target.trigger('touchy-longpress', ['end', $target, data]); }, this), settings.msThresh); break; @@ -293,6 +295,20 @@ } break; + //////////////// LONG PRESS //////////////// + case 'longpress': + if (settings.moveTolerance >= 0) { + var distance = Math.sqrt( Math.pow( (touches[0].pageX - data.startPoint.x), 2 ) + Math.pow( (touches[0].pageY - data.startPoint.y), 2 ) ); + if (settings.moveTolerance == 0 || distance > settings.moveTolerance) { + // Invalidate the longpress + clearTimeout(data.timer); + $.extend(data, { + "startDate":null + }); + } + } + break; + //////////////// ROTATE //////////////// case 'rotate': var lastMovePoint,