Skip to content

Instantly share code, notes, and snippets.

@opi
Last active December 14, 2015 01:08
Show Gist options
  • Save opi/5003488 to your computer and use it in GitHub Desktop.
Save opi/5003488 to your computer and use it in GitHub Desktop.
Touch Event, vanilla !
/**
* Touch events
*/
// wrapper is a DOM element
wrapper.touch = {}
wrapper.get(0).ontouchstart = function(e) {
// Store start position
wrapper.touch.x = e.touches[0].clientX;
};
wrapper.get(0).ontouchmove = function(e) {
// only deal with one finger
if (e.touches.length == 1 && !wrapper.is(":animated")) {
// Get delta
var deltaX = wrapper.touch.x - e.touches[0].clientX;
// Move foward
if (deltaX > 30) {
e.preventDefault();
// DO WHAT YOU NEED WHEN MOVING RIGHT
}
// Move backward
else if (deltaX < -30) {
e.preventDefault();
// DO WHAT YOU NEED WHEN MOVING LEFT
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment