Instantly share code, notes, and snippets.

Embed
What would you like to do?
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