Last active
August 29, 2015 13:56
-
-
Save GlennTaylorDigital/8821183 to your computer and use it in GitHub Desktop.
Detect swipes using jquery - handy for sliders etc
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 = $('.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