Posted By

ringingEars on 12/28/10


Tagged

drop n drag simulation touch


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

shalomfriss


drag n drop javascript to iphone support


 / Published in: iPhone
 

URL: http://www.gorankem.com/bestof2010

turns screen touches into mouse clicks - useful for porting drag n drop to iphone / ipad

  1. <script type="text/javascript">
  2.  
  3. function touchHandler(event)
  4. {
  5. var touches = event.changedTouches,
  6. first = touches[0],
  7. type = "";
  8.  
  9.  
  10. switch(event.type)
  11. {
  12. case "touchstart":
  13. type = "mousedown";
  14. break;
  15.  
  16. case "touchmove":
  17. type="mousemove";
  18. event.preventDefault();
  19. break;
  20.  
  21. case "touchend":
  22. type="mouseup";
  23. break;
  24.  
  25. default:
  26. return;
  27. }
  28.  
  29. var simulatedEvent = document.createEvent("MouseEvent");
  30.  
  31. //initMouseEvent(type, canBubble, cancelable, view, clickCount, screenX, screenY, clientX, clientY,
  32. // ctrlKey, altKey, shiftKey, metaKey, button, relatedTarget);
  33.  
  34. simulatedEvent.initMouseEvent(type, true, true, window, 1, first.screenX, first.screenY, first.clientX, first.clientY,
  35. false, false, false, false, 0/*left*/, null);
  36.  
  37. first.target.dispatchEvent(simulatedEvent);
  38.  
  39. //event.preventDefault();
  40. }
  41.  
  42. function init()
  43. {
  44. document.addEventListener("touchstart", touchHandler, false);
  45. document.addEventListener("touchmove", touchHandler, false);
  46. document.addEventListener("touchend", touchHandler, false);
  47. document.addEventListener("touchcancel", touchHandler, false);
  48. }
  49.  
  50. //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();">
  51.  
  52. <% load_function "init();"%>
  53.  
  54. </script>

Report this snippet  

You need to login to post a comment.