Skip to content

Instantly share code, notes, and snippets.

@pxwise
Last active November 26, 2020 23:04
Show Gist options
  • Save pxwise/cf35323237db7995103e to your computer and use it in GitHub Desktop.
Save pxwise/cf35323237db7995103e to your computer and use it in GitHub Desktop.
/**
* Lightweight script to convert touch handlers to mouse handlers
* credit: http://stackoverflow.com/a/6141093
*/
(function() {
function touchHandler(e) {
var touches = e.changedTouches;
var first = touches[0];
var type = "";
switch(e.type) {
case "touchstart":
type = "mousedown";
break;
case "touchmove":
type="mousemove";
break;
case "touchend":
type="mouseup";
break;
default:
return;
}
var simulatedEvent = document.createEvent("MouseEvent");
simulatedEvent.initMouseEvent(type, true, true, window, 1, first.screenX, first.screenY, first.clientX, first.clientY, false, false, false, false, 0, null);
first.target.dispatchEvent(simulatedEvent);
e.preventDefault();
}
function init() {
document.addEventListener("touchstart", touchHandler, true);
document.addEventListener("touchmove", touchHandler, true);
document.addEventListener("touchend", touchHandler, true);
document.addEventListener("touchcancel", touchHandler, true);
}
init();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment