Skip to content

Instantly share code, notes, and snippets.

@abachuk
Created September 26, 2013 18:18
Show Gist options
  • Save abachuk/6718305 to your computer and use it in GitHub Desktop.
Save abachuk/6718305 to your computer and use it in GitHub Desktop.
simple swipe wvents
$.fn.swipeEvents = function() {
return this.each(function() {
var startX,
startY,
$this = $(this);
$this.bind('touchstart', touchstart);
function touchstart(event) {
var touches = event.originalEvent.touches;
if (touches && touches.length) {
startX = touches[0].pageX;
startY = touches[0].pageY;
$this.bind('touchmove', touchmove);
}
event.preventDefault();
}
function touchmove(event) {
var touches = event.originalEvent.touches;
if (touches && touches.length) {
var deltaX = startX - touches[0].pageX;
var deltaY = startY - touches[0].pageY;
if (deltaX >= 50) {
$this.trigger("swipe_left");
//console.log('swipeleft');
}
if (deltaX <= -50) {
$this.trigger("swipe_right");
}
if (deltaY >= 50) {
$this.trigger("swipe_up");
}
if (deltaY <= -50) {
$this.trigger("swipe_down");
}
if (Math.abs(deltaX) >= 50 || Math.abs(deltaY) >= 50) {
$this.unbind('touchmove', touchmove);
}
}
event.preventDefault();
}
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment