Created
May 25, 2018 17:07
-
-
Save BenFausch/7afacfafa84c72ce582e93700c923955 to your computer and use it in GitHub Desktop.
swipe detection in Vanilla
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
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