| 1 |
/** |
| 2 |
* jquery.detectSwipe v2.1.3 |
| 3 |
* jQuery Plugin to obtain touch gestures from iPhone, iPod Touch, iPad and Android |
| 4 |
* http://github.com/marcandre/detect_swipe |
| 5 |
* Based on touchwipe by Andreas Waltl, netCU Internetagentur (http://www.netcu.de) |
| 6 |
*/ |
| 7 |
|
| 8 |
(function (factory) { |
| 9 |
if (typeof define === 'function' && define.amd) { |
| 10 |
// AMD. Register as an anonymous module. |
| 11 |
define(['jquery'], factory); |
| 12 |
} else if (typeof exports === 'object') { |
| 13 |
// Node/CommonJS |
| 14 |
module.exports = factory(require('jquery')); |
| 15 |
} else { |
| 16 |
// Browser globals |
| 17 |
factory(jQuery); |
| 18 |
} |
| 19 |
}(function($) { |
| 20 |
|
| 21 |
$.detectSwipe = { |
| 22 |
version: '2.1.2', |
| 23 |
enabled: 'ontouchstart' in document.documentElement, |
| 24 |
preventDefault: true, |
| 25 |
threshold: 20 |
| 26 |
}; |
| 27 |
|
| 28 |
var startX, |
| 29 |
startY, |
| 30 |
isMoving = false; |
| 31 |
|
| 32 |
function onTouchEnd() { |
| 33 |
this.removeEventListener('touchmove', onTouchMove); |
| 34 |
this.removeEventListener('touchend', onTouchEnd); |
| 35 |
isMoving = false; |
| 36 |
} |
| 37 |
|
| 38 |
function onTouchMove(e) { |
| 39 |
if ($.detectSwipe.preventDefault) { e.preventDefault(); } |
| 40 |
if(isMoving) { |
| 41 |
var x = e.touches[0].pageX; |
| 42 |
var y = e.touches[0].pageY; |
| 43 |
var dx = startX - x; |
| 44 |
var dy = startY - y; |
| 45 |
var dir; |
| 46 |
if(Math.abs(dx) >= $.detectSwipe.threshold) { |
| 47 |
dir = dx > 0 ? 'left' : 'right' |
| 48 |
} |
| 49 |
else if(Math.abs(dy) >= $.detectSwipe.threshold) { |
| 50 |
dir = dy > 0 ? 'up' : 'down' |
| 51 |
} |
| 52 |
if(dir) { |
| 53 |
onTouchEnd.call(this); |
| 54 |
$(this).trigger('swipe', dir).trigger('swipe' + dir); |
| 55 |
} |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
function onTouchStart(e) { |
| 60 |
if (e.touches.length == 1) { |
| 61 |
startX = e.touches[0].pageX; |
| 62 |
startY = e.touches[0].pageY; |
| 63 |
isMoving = true; |
| 64 |
this.addEventListener('touchmove', onTouchMove, false); |
| 65 |
this.addEventListener('touchend', onTouchEnd, false); |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
function setup() { |
| 70 |
this.addEventListener && this.addEventListener('touchstart', onTouchStart, false); |
| 71 |
} |
| 72 |
|
| 73 |
function teardown() { |
| 74 |
this.removeEventListener('touchstart', onTouchStart); |
| 75 |
} |
| 76 |
|
| 77 |
$.event.special.swipe = { setup: setup }; |
| 78 |
|
| 79 |
$.each(['left', 'up', 'down', 'right'], function () { |
| 80 |
$.event.special['swipe' + this] = { setup: function(){ |
| 81 |
$(this).on('swipe', $.noop); |
| 82 |
} }; |
| 83 |
}); |
| 84 |
})); |