Skip to content

Instantly share code, notes, and snippets.

@ChrisMBarr
Created November 18, 2012 21:06
Show Gist options
  • Save ChrisMBarr/4107472 to your computer and use it in GitHub Desktop.
Save ChrisMBarr/4107472 to your computer and use it in GitHub Desktop.
Detect Touch Support
function isTouchDevice(){
try{
document.createEvent("TouchEvent");
return true;
}catch(e){
return false;
}
}
function touchScroll(id){
if(isTouchDevice()){ //if touch events exist...
var el=document.getElementById(id);
var scrollStartPos=0;
document.getElementById(id).addEventListener("touchstart", function(event) {
scrollStartPos=this.scrollTop+event.touches[0].pageY;
event.preventDefault();
},false);
document.getElementById(id).addEventListener("touchmove", function(event) {
this.scrollTop=scrollStartPos-event.touches[0].pageY;
event.preventDefault();
},false);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment