Skip to content

Commit

Permalink
Fix bug where multiple small swipes would be considered as an actual …
Browse files Browse the repository at this point in the history
…swipe.

Improve listener handling
  • Loading branch information
marcandre committed Mar 26, 2014
1 parent 5bd6a52 commit 9e2d8c9
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions jquery.detect_swipe.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
/**
* jquery.detectSwipe v2.1
* jquery.detectSwipe v2.1.1
* jQuery Plugin to obtain touch gestures from iPhone, iPod Touch, iPad and Android
* http://github.com/marcandre/detect_swipe
* Based on touchwipe by Andreas Waltl, netCU Internetagentur (http://www.netcu.de)
*/
(function($) {

$.detectSwipe = {
version: '2.1.0',
version: '2.1.1',
enabled: 'ontouchstart' in document.documentElement,
preventDefault: true,
threshold: 20
Expand All @@ -17,6 +17,12 @@
startY,
isMoving = false;

function onTouchEnd() {
this.removeEventListener('touchmove', onTouchMove);
this.removeEventListener('touchend', onTouchEnd);
isMoving = false;
}

function onTouchMove(e) {
if ($.detectSwipe.preventDefault) { e.preventDefault(); }
if(isMoving) {
Expand All @@ -32,8 +38,7 @@
dir = dy > 0 ? 'down' : 'up'
}
if(dir) {
this.removeEventListener('touchmove', onTouchMove);
isMoving = false;
onTouchEnd.call(this);
$(this).trigger('swipe', dir).trigger('swipe' + dir);
}
}
Expand All @@ -45,13 +50,18 @@
startY = e.touches[0].pageY;
isMoving = true;
this.addEventListener('touchmove', onTouchMove, false);
this.addEventListener('touchend', onTouchEnd, false);
}
}

function setup() {
this.addEventListener('touchstart', onTouchStart, false);
}

function teardown() {
this.removeEventListener('touchstart', onTouchStart);
}

$.event.special.swipe = { setup: setup };

$.each(['left', 'up', 'down', 'right'], function () {
Expand Down

0 comments on commit 9e2d8c9

Please sign in to comment.