Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save akhileshdarjee/da600943491ab5573a363bf2bdad246f to your computer and use it in GitHub Desktop.
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
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