Create a gist now

Instantly share code, notes, and snippets.

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