Skip to content

Instantly share code, notes, and snippets.

@rogeruiz
Created January 27, 2014 15:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save rogeruiz/8650211 to your computer and use it in GitHub Desktop.
Save rogeruiz/8650211 to your computer and use it in GitHub Desktop.
Simple Touch and Pointer Events Handler
function handleTouch (evt) {
var touchX, touchY, xOffset, yOffset;
if (window.navigator.msPointerEnabled) {
var bodyEl = document.getElementByTagName('body')[0];
bodyEl.style.msTouchAction = 'none';
bodyEl.style.touchAction = 'none';
}
// Normalize your touch events
touchX = evt.originalEvent.pageX || evt.originalEvent.changedTouches[0].screenX;
touchY = evt.originalEvent.pageY || evt.originalEvent.changedTouches[0].screenY;
switch (evt.type) {
case 'touchstart':
case 'MSPointerDown':
case 'pointerdown':
// Do some touchStart stuff.
break;
case 'touchmove':
case 'MSPointerMove':
case 'pointermove':
// Do some touchMove stuff
break;
case 'touchend':
case 'MSPointerUp':
case 'pointerup':
if (window.navigator.msPointerEnabled) {
var bodyEl = document.getElementByTagName('body')[0];
bodyEl.style.msTouchAction = '';
bodyEl.style.touchAction = '';
}
// Handle the touchEnd stuff.
break;
}
}
$('.js-touch-trigger').on('touchstart touchmove touchend MSPointerDown pointerdown MSPointerMove pointermove MSPointerUp pointerup click', function(evt) {
evt.preventDefault();
if (evt.type !== 'click') {
handleTouch(evt);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment