Skip to content

Instantly share code, notes, and snippets.

@jonhoo
Created March 26, 2012 14:20
Show Gist options
  • Save jonhoo/2205417 to your computer and use it in GitHub Desktop.
Save jonhoo/2205417 to your computer and use it in GitHub Desktop.
Swipe and click handlers in simple JS
try{
document.createEvent("TouchEvent");
// Here we're ensured touch capabilities
var scroll = document.getElementById('myid');
var start = null;
var clickIfEnd = true;
scroll.addEventListener('touchstart', function (e) {
start = e.touches[0].pageX;
clickIfEnd = true;
e.preventDefault();
}, false);
scroll.addEventListener('touchmove', function (e) {
this.scrollLeft = start - e.touches[0].pageX;
clickIfEnd = false;
e.preventDefault();
}, false);
scroll.addEventListener('touchend', function (e) {
if (clickIfEnd) {
if (e) {
clickIfEnd = e.target;
// Add a delay before treating this as a click, in case it was accidental
setTimeout(arguments.callee, 100);
} else {
// clickIfEnd is the element that was clicked!
}
}
}, false);
} catch(e){}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment