Created
March 26, 2012 14:20
-
-
Save jonhoo/2205417 to your computer and use it in GitHub Desktop.
Swipe and click handlers in simple JS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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