touchevent.js 1.2 KB

123456789101112131415161718192021222324252627282930
  1. function touchHandler(event) {
  2. event.stopPropagation();
  3. var touches = event.changedTouches,
  4. first = touches[0],
  5. type = "";
  6. switch (event.type) {
  7. case "touchstart": type = "mousedown"; break;
  8. case "touchmove": type = "mousemove"; break;
  9. case "touchend": type = "mouseup"; break;
  10. default: return;
  11. }
  12. var simulatedEvent = new MouseEvent(type, { "altKey": false, "button": 0, "buttons": 0, "clientX": first.clientX, "clientY": first.clientY, "ctrlKey": false, "metaKey": false, "relatedTarget": first, "screenX": first.screenX, "screenY": first.screenY, "shiftKey": false });
  13. // var simulatedEvent = document.createEvent("MouseEvent");
  14. // simulatedEvent.initMouseEvent(type, true, true, window, 1,
  15. // first.screenX, first.screenY,
  16. // first.clientX, first.clientY, false,
  17. // false, false, false, 0/*left*/, null);
  18. first.target.dispatchEvent(simulatedEvent);
  19. }
  20. function init() {
  21. document.addEventListener("touchstart", touchHandler, true);
  22. document.addEventListener("touchmove", touchHandler, true);
  23. document.addEventListener("touchend", touchHandler, true);
  24. document.addEventListener("touchcancel", touchHandler, true);
  25. }