|
1 | 1 | /** |
2 | | - * jquery.detectSwipe v1.0 |
| 2 | + * jquery.detectSwipe v2.0 |
3 | 3 | * jQuery Plugin to obtain touch gestures from iPhone, iPod Touch, iPad and Android |
4 | 4 | * http://github.com/marcandre/detect_swipe |
5 | 5 | * Based on touchwipe by Andreas Waltl, netCU Internetagentur (http://www.netcu.de) |
6 | 6 | */ |
7 | 7 | (function($) { |
8 | | - $.fn.detectSwipe = function(settings) { |
9 | | - var config = { |
10 | | - threshold: 20, |
11 | | - }; |
12 | 8 |
|
13 | | - if (settings) $.extend(config, settings); |
| 9 | + $.detectSwipe = { |
| 10 | + version: '2.0.0', |
| 11 | + enabled: 'ontouchstart' in document.documentElement, |
| 12 | + threshold: 20 |
| 13 | + }; |
14 | 14 |
|
15 | | - this.each(function() { |
16 | | - var startX; |
17 | | - var startY; |
18 | | - var isMoving = false; |
| 15 | + var startX, |
| 16 | + startY, |
| 17 | + isMoving = false; |
19 | 18 |
|
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' |
40 | 29 | } |
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' |
49 | 32 | } |
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); |
52 | 37 | } |
53 | | - }); |
| 38 | + } |
| 39 | + } |
54 | 40 |
|
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 | + }); |
58 | 61 | })(jQuery); |
0 commit comments