Skip to content

Commit 9e2d8c9

Browse files
committed
Fix bug where multiple small swipes would be considered as an actual swipe.
Improve listener handling
1 parent 5bd6a52 commit 9e2d8c9

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

jquery.detect_swipe.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
/**
2-
* jquery.detectSwipe v2.1
2+
* jquery.detectSwipe v2.1.1
33
* jQuery Plugin to obtain touch gestures from iPhone, iPod Touch, iPad and Android
44
* http://github.com/marcandre/detect_swipe
55
* Based on touchwipe by Andreas Waltl, netCU Internetagentur (http://www.netcu.de)
66
*/
77
(function($) {
88

99
$.detectSwipe = {
10-
version: '2.1.0',
10+
version: '2.1.1',
1111
enabled: 'ontouchstart' in document.documentElement,
1212
preventDefault: true,
1313
threshold: 20
@@ -17,6 +17,12 @@
1717
startY,
1818
isMoving = false;
1919

20+
function onTouchEnd() {
21+
this.removeEventListener('touchmove', onTouchMove);
22+
this.removeEventListener('touchend', onTouchEnd);
23+
isMoving = false;
24+
}
25+
2026
function onTouchMove(e) {
2127
if ($.detectSwipe.preventDefault) { e.preventDefault(); }
2228
if(isMoving) {
@@ -32,8 +38,7 @@
3238
dir = dy > 0 ? 'down' : 'up'
3339
}
3440
if(dir) {
35-
this.removeEventListener('touchmove', onTouchMove);
36-
isMoving = false;
41+
onTouchEnd.call(this);
3742
$(this).trigger('swipe', dir).trigger('swipe' + dir);
3843
}
3944
}
@@ -45,13 +50,18 @@
4550
startY = e.touches[0].pageY;
4651
isMoving = true;
4752
this.addEventListener('touchmove', onTouchMove, false);
53+
this.addEventListener('touchend', onTouchEnd, false);
4854
}
4955
}
5056

5157
function setup() {
5258
this.addEventListener('touchstart', onTouchStart, false);
5359
}
5460

61+
function teardown() {
62+
this.removeEventListener('touchstart', onTouchStart);
63+
}
64+
5565
$.event.special.swipe = { setup: setup };
5666

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

0 commit comments

Comments
 (0)