Javascript touch events from click


/ Published in: jQuery
Save to your folder(s)

Add the init() function to your document.ready call. This allows touch events to be handled like click events.


Copy this code and paste it in your HTML
  1. function touchHandler(event)
  2. {
  3. var touches = event.changedTouches,
  4. first = touches[0],
  5. type = "";
  6.  
  7. switch(event.type)
  8. {
  9. case "touchstart": type = "mousedown"; break;
  10. case "touchmove": type="mousemove"; break;
  11. case "touchend": type="mouseup"; break;
  12. default: return;
  13. }
  14. var simulatedEvent = document.createEvent("MouseEvent");
  15. simulatedEvent.initMouseEvent(type, true, true, window, 1,
  16. first.screenX, first.screenY,
  17. first.clientX, first.clientY, false,
  18. false, false, false, 0/*left*/, null);
  19.  
  20. first.target.dispatchEvent(simulatedEvent);
  21. event.preventDefault();
  22. }
  23.  
  24. function init()
  25. {
  26. document.addEventListener("touchstart", touchHandler, true);
  27. document.addEventListener("touchmove", touchHandler, true);
  28. document.addEventListener("touchend", touchHandler, true);
  29. document.addEventListener("touchcancel", touchHandler, true);
  30. }

URL: http://stackoverflow.com/questions/5186441/javascript-drag-and-drop-for-touch-devices

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.