Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added support for a swipe tolerance: a default distance the finger ha…

…s to be swiped for a swipe to register.
  • Loading branch information...
commit 5c7c0941bbb765c2aab9e7437a588e1712c44ed5 1 parent 07641ca
@robschmitt robschmitt authored
Showing with 19 additions and 8 deletions.
  1. +19 −8 lib/jquery.doubletap.js
View
27 lib/jquery.doubletap.js
@@ -1,4 +1,9 @@
(function($) {
+
+ var defaults = {
+ 'swipeTolerance': 40
+ };
+
var touchStatus = function(target, touch) {
this.target = $(target);
this.touch = touch;
@@ -6,6 +11,7 @@
this.startY = this.currentY = touch.screenY;
this.eventType = null;
}
+ touchStatus.options = {};
touchStatus.latestTap = null;
touchStatus.prototype.move = function(touch) {
@@ -17,15 +23,15 @@
var offsetX = this.currentX - this.startX;
var offsetY = this.currentY - this.startY;
if(offsetX == 0 && offsetY == 0) {
- this.checkForDoubleTap()
- } else if(Math.abs(offsetY) > Math.abs(offsetX)) {
+ this.checkForDoubleTap();
+ } else if(Math.abs(offsetY) > touchStatus.options.swipeTolerance && Math.abs(offsetY) > Math.abs(offsetX)) {
this.eventType = offsetY > 0 ? 'swipedown' : 'swipeup';
this.target.trigger('swipe', [this])
- } else {
+ } else if(Math.abs(offsetX) > touchStatus.options.swipeTolerance) {
this.eventType = offsetX > 0 ? 'swiperight' : 'swipeleft';
this.target.trigger('swipe', [this])
}
- this.target.trigger(this.eventType, [this])
+ if(this.eventType) this.target.trigger(this.eventType, [this])
this.target.trigger('touch', [this])
}
@@ -38,7 +44,8 @@
touchStatus.latestTap = new Date()
}
- var swipeEvents = function(elements) {
+ var swipeEvents = function(elements, options) {
+ touchStatus.options = $.extend(defaults, options);
elements.bind('touchstart', this.touchStart);
elements.bind('touchmove', this.touchMove);
elements.bind('touchcancel', this.touchCancel);
@@ -97,9 +104,13 @@
// swipedown
// tap
// doubletap
- $.fn.addSwipeEvents = function(callback) {
- new swipeEvents(this);
- if(callback) this.bind('touch', callback)
+ $.fn.addSwipeEvents = function(options, callback) {
+ if (!callback && jQuery.isFunction(options)) {
+ callback = options;
+ options = null;
+ }
+ new swipeEvents(this, options);
+ if(callback) this.bind('touch', callback);
return this;
}
})(jQuery);
Please sign in to comment.
Something went wrong with that request. Please try again.