Skip to content

Instantly share code, notes, and snippets.

@timwhitlock
Created November 14, 2012 16:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timwhitlock/4073106 to your computer and use it in GitHub Desktop.
Save timwhitlock/4073106 to your computer and use it in GitHub Desktop.
Abstracting touch and pointer events with an iterator function
function onTouchStart( event ){
eachTouchEvent( event, function( i, ev ){
ev.clientX; // <- do something with this
} );
return true;
}
function eachTouchEvent( event, callback ){
// microsoft's pointer events aren't multi
if( navigator.msPointerEnabled ){
if( event.MSPOINTER_TYPE_TOUCH === event.pointerType ){
callback( 0, event );
}
// else not a touch interaction
return;
}
// regular multi-touch support
var i = -1,
touched = event.originalEvent || event,
changed = touched.changedTouches||[];
while( ++i < changed.length ){
callback( i, changed[i] );
}
}
element.addEventListener( navigator.msPointerEnabled ? 'MSPointerDown' : 'touchstart', onTouchStart, false );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment