-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
56 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,58 +1,61 @@ | ||
/** | ||
* jquery.detectSwipe v1.0 | ||
* jquery.detectSwipe v2.0 | ||
* 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($) { | ||
$.fn.detectSwipe = function(settings) { | ||
var config = { | ||
threshold: 20, | ||
}; | ||
|
||
if (settings) $.extend(config, settings); | ||
$.detectSwipe = { | ||
version: '2.0.0', | ||
enabled: 'ontouchstart' in document.documentElement, | ||
threshold: 20 | ||
}; | ||
|
||
this.each(function() { | ||
var startX; | ||
var startY; | ||
var isMoving = false; | ||
var startX, | ||
startY, | ||
isMoving = false; | ||
|
||
function onTouchMove(e) { | ||
e.preventDefault(); | ||
if(isMoving) { | ||
var x = e.touches[0].pageX; | ||
var y = e.touches[0].pageY; | ||
var dx = startX - x; | ||
var dy = startY - y; | ||
var dir; | ||
if(Math.abs(dx) >= config.threshold) { | ||
dir = dx > 0 ? 'left' : 'right' | ||
} | ||
else if(Math.abs(dy) >= config.threshold) { | ||
dir = dy > 0 ? 'down' : 'up' | ||
} | ||
if(dir) { | ||
this.removeEventListener('touchmove', onTouchMove); | ||
isMoving = false; | ||
$(this).trigger('swipe' + dir); | ||
} | ||
} | ||
function onTouchMove(e) { | ||
e.preventDefault(); | ||
if(isMoving) { | ||
var x = e.touches[0].pageX; | ||
var y = e.touches[0].pageY; | ||
var dx = startX - x; | ||
var dy = startY - y; | ||
var dir; | ||
if(Math.abs(dx) >= $.detectSwipe.threshold) { | ||
dir = dx > 0 ? 'left' : 'right' | ||
} | ||
|
||
function onTouchStart(e) { | ||
if (e.touches.length == 1) { | ||
startX = e.touches[0].pageX; | ||
startY = e.touches[0].pageY; | ||
isMoving = true; | ||
this.addEventListener('touchmove', onTouchMove, false); | ||
} | ||
else if(Math.abs(dy) >= $.detectSwipe.threshold) { | ||
dir = dy > 0 ? 'down' : 'up' | ||
} | ||
if ($.fn.detectSwipe.enabled) { | ||
this.addEventListener('touchstart', onTouchStart, false); | ||
if(dir) { | ||
this.removeEventListener('touchmove', onTouchMove); | ||
isMoving = false; | ||
$(this).trigger('swipe', dir).trigger('swipe' + dir); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
return this; | ||
}; | ||
$.fn.detectSwipe.enabled = 'ontouchstart' in document.documentElement; | ||
function onTouchStart(e) { | ||
if (e.touches.length == 1) { | ||
startX = e.touches[0].pageX; | ||
startY = e.touches[0].pageY; | ||
isMoving = true; | ||
this.addEventListener('touchmove', onTouchMove, false); | ||
} | ||
} | ||
|
||
function setup() { | ||
this.addEventListener('touchstart', onTouchStart, false); | ||
} | ||
|
||
$.event.special.swipe = { setup: setup }; | ||
|
||
$.each(['left', 'up', 'down', 'right'], function () { | ||
$.event.special['swipe' + this] = { setup: function(){ | ||
$(this).on('swipe', $.noop); | ||
} }; | ||
}); | ||
})(jQuery); |