Skip to content

Commit e757296

Browse files
committed
v2.0.0: Use $.events.special
1 parent c5e0df4 commit e757296

File tree

2 files changed

+56
-52
lines changed

2 files changed

+56
-52
lines changed

README.md

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,15 @@ Based on Andreas Waltl's [jQuery TouchWipe](http://www.netcu.de/jquery-touchwipe
88
How to use
99
----------
1010

11-
$(".selector").detectSwipe({
12-
threshold: 20, // The number of pixels your finger must move to trigger a swipe event. Defaults is 20.
13-
})
14-
.on('swipeleft', function(){ /*...*/ })
15-
.on('swiperight', function(){ /*...*/ })
16-
.on('swipeup', function(){ /*...*/ })
17-
.on('swipedown', function(){ /*...*/ });
11+
$(".selector").on('swipeleft', function(){ /*...*/ })
12+
.on('swiperight', function(){ /*...*/ })
13+
.on('swipeup', function(){ /*...*/ })
14+
.on('swipedown', function(){ /*...*/ });
1815

1916
This won't have any effect on non-touch devices. You can rely on:
2017

21-
$.fn.detectSwipe.enabled // true on touch devices, false otherwise
18+
$.detectSwipe.enabled // true on touch devices, false otherwise
19+
20+
Global setting:
21+
22+
$.detectSwipe.threshold // The number of pixels your finger must move to trigger a swipe event. Defaults is 20.

jquery.detect_swipe.js

Lines changed: 47 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,61 @@
11
/**
2-
* jquery.detectSwipe v1.0
2+
* jquery.detectSwipe v2.0
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($) {
8-
$.fn.detectSwipe = function(settings) {
9-
var config = {
10-
threshold: 20,
11-
};
128

13-
if (settings) $.extend(config, settings);
9+
$.detectSwipe = {
10+
version: '2.0.0',
11+
enabled: 'ontouchstart' in document.documentElement,
12+
threshold: 20
13+
};
1414

15-
this.each(function() {
16-
var startX;
17-
var startY;
18-
var isMoving = false;
15+
var startX,
16+
startY,
17+
isMoving = false;
1918

20-
function onTouchMove(e) {
21-
e.preventDefault();
22-
if(isMoving) {
23-
var x = e.touches[0].pageX;
24-
var y = e.touches[0].pageY;
25-
var dx = startX - x;
26-
var dy = startY - y;
27-
var dir;
28-
if(Math.abs(dx) >= config.threshold) {
29-
dir = dx > 0 ? 'left' : 'right'
30-
}
31-
else if(Math.abs(dy) >= config.threshold) {
32-
dir = dy > 0 ? 'down' : 'up'
33-
}
34-
if(dir) {
35-
this.removeEventListener('touchmove', onTouchMove);
36-
isMoving = false;
37-
$(this).trigger('swipe' + dir);
38-
}
39-
}
19+
function onTouchMove(e) {
20+
e.preventDefault();
21+
if(isMoving) {
22+
var x = e.touches[0].pageX;
23+
var y = e.touches[0].pageY;
24+
var dx = startX - x;
25+
var dy = startY - y;
26+
var dir;
27+
if(Math.abs(dx) >= $.detectSwipe.threshold) {
28+
dir = dx > 0 ? 'left' : 'right'
4029
}
41-
42-
function onTouchStart(e) {
43-
if (e.touches.length == 1) {
44-
startX = e.touches[0].pageX;
45-
startY = e.touches[0].pageY;
46-
isMoving = true;
47-
this.addEventListener('touchmove', onTouchMove, false);
48-
}
30+
else if(Math.abs(dy) >= $.detectSwipe.threshold) {
31+
dir = dy > 0 ? 'down' : 'up'
4932
}
50-
if ($.fn.detectSwipe.enabled) {
51-
this.addEventListener('touchstart', onTouchStart, false);
33+
if(dir) {
34+
this.removeEventListener('touchmove', onTouchMove);
35+
isMoving = false;
36+
$(this).trigger('swipe', dir).trigger('swipe' + dir);
5237
}
53-
});
38+
}
39+
}
5440

55-
return this;
56-
};
57-
$.fn.detectSwipe.enabled = 'ontouchstart' in document.documentElement;
41+
function onTouchStart(e) {
42+
if (e.touches.length == 1) {
43+
startX = e.touches[0].pageX;
44+
startY = e.touches[0].pageY;
45+
isMoving = true;
46+
this.addEventListener('touchmove', onTouchMove, false);
47+
}
48+
}
49+
50+
function setup() {
51+
this.addEventListener('touchstart', onTouchStart, false);
52+
}
53+
54+
$.event.special.swipe = { setup: setup };
55+
56+
$.each(['left', 'up', 'down', 'right'], function () {
57+
$.event.special['swipe' + this] = { setup: function(){
58+
$(this).on('swipe', $.noop);
59+
} };
60+
});
5861
})(jQuery);

0 commit comments

Comments
 (0)