Created
September 26, 2013 18:18
-
-
Save abachuk/6718305 to your computer and use it in GitHub Desktop.
simple swipe wvents
This file contains hidden or 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
$.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