Skip to content

Instantly share code, notes, and snippets.

@yasirtaher
Created June 25, 2016 07:51
Show Gist options
  • Save yasirtaher/05d7cc5ddf01bfc9dc0b1a406661c9fe to your computer and use it in GitHub Desktop.
Save yasirtaher/05d7cc5ddf01bfc9dc0b1a406661c9fe to your computer and use it in GitHub Desktop.
JS Drag and Drop for touch devices
//source: http://stackoverflow.com/questions/5186441/javascript-drag-and-drop-for-touch-devices
function touchHandler(event) {
var touch = event.changedTouches[0];
var simulatedEvent = document.createEvent("MouseEvent");
simulatedEvent.initMouseEvent({
touchstart: "mousedown",
touchmove: "mousemove",
touchend: "mouseup"
}[event.type], true, true, window, 1,
touch.screenX, touch.screenY,
touch.clientX, touch.clientY, false,
false, false, false, 0, null);
touch.target.dispatchEvent(simulatedEvent);
event.preventDefault();
}
function init() {
document.addEventListener("touchstart", touchHandler, true);
document.addEventListener("touchmove", touchHandler, true);
document.addEventListener("touchend", touchHandler, true);
document.addEventListener("touchcancel", touchHandler, true);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment