Skip to content

Instantly share code, notes, and snippets.

@cacheleocode
Created July 25, 2013 20:28
Show Gist options
  • Select an option

  • Save cacheleocode/6083439 to your computer and use it in GitHub Desktop.

Select an option

Save cacheleocode/6083439 to your computer and use it in GitHub Desktop.
//this code converts touch events to mouse events
function touchHandler(event) {
var touches = event.changedTouches,
first = touches[0],
type = "";
switch (event.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/*left*/, null);
first.target.dispatchEvent(simulatedEvent);
event.preventDefault();
}
//this code stops the I-beam appearing when dragging elements - it also prevents text selection
window.addEventListener("dragstart", function(fEventObject){ CancelEvent(fEventObject); } );
window.addEventListener("selectstart", function(fEventObject){ CancelEvent(fEventObject); } );
function CancelEvent(fEventObject)
{
if (fEventObject.preventDefault) fEventObject.preventDefault();
if (fEventObject.cancel != null) fEventObject.cancel = true;
}/**/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment