Skip to content

Instantly share code, notes, and snippets.

@BenFausch
Created May 25, 2018 17:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BenFausch/7afacfafa84c72ce582e93700c923955 to your computer and use it in GitHub Desktop.
Save BenFausch/7afacfafa84c72ce582e93700c923955 to your computer and use it in GitHub Desktop.
swipe detection in Vanilla
let touchstartX = 0;
let touchstartY = 0;
let touchendX = 0;
let touchendY = 0;
const gestureZone = document.getElementById('gestureZone');
gestureZone.addEventListener('touchstart', function(event) {
touchstartX = event.changedTouches[0].screenX;
touchstartY = event.changedTouches[0].screenY;
}, false);
gestureZone.addEventListener('touchend', function(event) {
touchendX = event.changedTouches[0].screenX;
touchendY = event.changedTouches[0].screenY;
handleGesture();
}, false);
function handleGesture() {
if (touchendX <= touchstartX) {
console.log('Swiped left');
}
if (touchendX >= touchstartX) {
console.log('Swiped right');
}
if (touchendY <= touchstartY) {
console.log('Swiped up');
}
if (touchendY >= touchstartY) {
console.log('Swiped down');
}
if (touchendY === touchstartY) {
console.log('Tap');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment