Created
April 22, 2019 13:04
-
-
Save akhileshdarjee/da600943491ab5573a363bf2bdad246f to your computer and use it in GitHub Desktop.
Touch Swipe like Swipe Left, Swipe Right, Swipe Up and Swipe Down events for touch devices in Javascript
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 xDown = null; | |
var yDown = null; | |
document.addEventListener('touchstart', handleTouchStart, false); | |
document.addEventListener('touchmove', handleTouchMove, false); | |
function handleTouchStart(evt) { | |
xDown = evt.touches[0].clientX; | |
yDown = evt.touches[0].clientY; | |
}; | |
function handleTouchMove(evt) { | |
if (!xDown || !yDown) { | |
return; | |
} | |
var xUp = evt.touches[0].clientX; | |
var yUp = evt.touches[0].clientY; | |
var xDiff = xDown - xUp; | |
var yDiff = yDown - yUp; | |
if (Math.abs(xDiff) > Math.abs(yDiff)) { | |
if (xDiff > 0) { | |
// swipe right direction | |
} | |
else { | |
// swipe left direction | |
} | |
} | |
else { | |
if (yDiff > 0) { | |
// swipe down direction | |
} | |
else { | |
// swipe up direction | |
} | |
} | |
/* reset values */ | |
xDown = null; | |
yDown = null; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment