-
-
Save nasal/8967231 to your computer and use it in GitHub Desktop.
JS: handle touch events on multiple devices
This file contains hidden or 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 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