Last active
November 26, 2020 23:04
-
-
Save pxwise/cf35323237db7995103e to your computer and use it in GitHub Desktop.
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
/** | |
* 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