/ Published in: iPhone
turns screen touches into mouse clicks - useful for porting drag n drop to iphone / ipad
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
<script type="text/javascript"> function touchHandler(event) { var touches = event.changedTouches, first = touches[0], type = ""; switch(event.type) { case "touchstart": type = "mousedown"; break; case "touchmove": type="mousemove"; event.preventDefault(); break; case "touchend": type="mouseup"; break; default: return; } var simulatedEvent = document.createEvent("MouseEvent"); //initMouseEvent(type, canBubble, cancelable, view, clickCount, screenX, screenY, clientX, clientY, // ctrlKey, altKey, shiftKey, metaKey, button, relatedTarget); simulatedEvent.initMouseEvent(type, true, true, window, 1, first.screenX, first.screenY, first.clientX, first.clientY, false, false, false, false, 0/*left*/, null); first.target.dispatchEvent(simulatedEvent); //event.preventDefault(); } function init() { document.addEventListener("touchstart", touchHandler, false); document.addEventListener("touchmove", touchHandler, false); document.addEventListener("touchend", touchHandler, false); document.addEventListener("touchcancel", touchHandler, false); } //and what i've done here is create a load_function yield statement that attaches init() to the body onload= tag if you are using rails - otherwise just do <body onload="init();"> <% load_function "init();"%> </script>
URL: http://www.gorankem.com/bestof2010