Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
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