;(function() { /** * 模拟PC端 mouse 事件 */ function touchEventToMouseEvent(event, eventType) { var te = event.targetTouches[0] || event.changedTouches[0]; var clientX = te.clientX, clientY = te.clientY, screenX = te.screenX, screenY = te.screenY; var simEvent = new MouseEvent(eventType, { clientX: clientX, clientY: clientY, screenX: screenX, screenY: screenY, button: 0, buttons: 0, bubbles: true }); return simEvent; } /** * only trigger touches when the left mousebutton has been pressed * @param touchType * @returns {Function} */ function onMouse(touchType) { return function(ev) { if(touchType == "mousemove") { // ev.preventDefault() } var simEvent = touchEventToMouseEvent(ev, touchType); if (simEvent != null) { ev.target.dispatchEvent(simEvent); } }; } /** * MouseEmulator initializer */ function MouseEmulator() { window.addEventListener('touchstart', onMouse('mousedown'), true); window.addEventListener('touchmove', onMouse('mousemove'), true); window.addEventListener('touchend', onMouse('mouseup'), true); } MouseEmulator(); })();