xyc
2025-02-21 664db98c9e8595ce4dd636a27f480e3a08b81ff5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// http://ross.posterous.com/2008/08/19/iphone-touch-events-in-javascript/
function touchHandler (ev) {
  const {changedTouches} = ev,
    first = changedTouches[0];
 
  let type = '';
  switch (ev.type) {
  case 'touchstart': type = 'mousedown'; break;
  case 'touchmove': type = 'mousemove'; break;
  case 'touchend': type = 'mouseup'; break;
  default: return;
  }
 
  const {screenX, screenY, clientX, clientY} = first;
  const simulatedEvent = new MouseEvent(type, {
    // Event interface
    bubbles: true,
    cancelable: true,
    // UIEvent interface
    view: window,
    detail: 1, // click count
    // MouseEvent interface (customized)
    screenX, screenY, clientX, clientY,
    // MouseEvent interface (defaults) - these could be removed
    ctrlKey: false,
    altKey: false,
    shiftKey: false,
    metaKey: false,
    button: 0, // main button (usually left)
    relatedTarget: null
  });
  if (changedTouches.length < 2) {
    first.target.dispatchEvent(simulatedEvent);
    ev.preventDefault();
  }
}
 
document.addEventListener('touchstart', touchHandler, true);
document.addEventListener('touchmove', touchHandler, true);
document.addEventListener('touchend', touchHandler, true);
document.addEventListener('touchcancel', touchHandler, true);