Skip to content

Instantly share code, notes, and snippets.

@courgette
Forked from opi/gist:5003488
Created February 21, 2013 09:55
Show Gist options
  • Save courgette/5003591 to your computer and use it in GitHub Desktop.
Save courgette/5003591 to your computer and use it in GitHub Desktop.
/**
* 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