Created
November 14, 2012 16:24
-
-
Save timwhitlock/4073106 to your computer and use it in GitHub Desktop.
Abstracting touch and pointer events with an iterator function
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
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