Skip to content

Instantly share code, notes, and snippets.

@crot4lus
Last active August 29, 2015 13:56
Show Gist options
  • Save crot4lus/8821183 to your computer and use it in GitHub Desktop.
Save crot4lus/8821183 to your computer and use it in GitHub Desktop.
Detect swipes using jquery - handy for sliders etc
$this = $('.my-swipe-area');
var touch = {
start: false,
stop: false
};
$this.on('touchstart', function(e){
touch.start = e.originalEvent.touches[0].pageX;
});
$this.on('touchend', function(e){
touch.end = e.originalEvent.changedTouches[0].pageX ;
if(touch.end <= touch.start){
// Swiped left
}else{
// Swiped right
}
});
// touchcancel needs to be used for supporting browsers
// such as Chrome on Android
$this.on('touchcancel', function(e){
touch.end = e.originalEvent.changedTouches[0].pageX ;
if(touch.end <= touch.start){
// Swiped left
}else{
// Swiped right
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment