Created
June 16, 2017 14:50
-
-
Save rigred/a1d373866f0c427cce00f7ff69d92729 to your computer and use it in GitHub Desktop.
native js swipe
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
var touchstartX = 0; | |
var touchstartY = 0; | |
var touchendX = 0; | |
var touchendY = 0; | |
var gesuredZone = document.getElementById('gesuredZone'); | |
gesuredZone.addEventListener('touchstart', function(event) { | |
touchstartX = event.changedTouches[0].screenX; | |
touchstartY = event.changedTouches[0].screenY; | |
}, false); | |
gesuredZone.addEventListener('touchend', function(event) { | |
touchendX = event.changedTouches[0].screenX; | |
touchendY = event.changedTouches[0].screenY; | |
handleGesure(); | |
}, false); | |
function handleGesure() { | |
var swiped = 'swiped: '; | |
if (touchendX < touchstartX) { | |
alert(swiped + 'left!'); | |
} | |
if (touchendX > touchstartX) { | |
alert(swiped + 'right!'); | |
} | |
if (touchendY < touchstartY) { | |
alert(swiped + 'down!'); | |
} | |
if (touchendY > touchstartY) { | |
alert(swiped + 'left!'); | |
} | |
if (touchendY == touchstartY) { | |
alert('tap!'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment