Skip to content

Instantly share code, notes, and snippets.

@cagerton
Created December 13, 2013 18:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cagerton/7948779 to your computer and use it in GitHub Desktop.
Save cagerton/7948779 to your computer and use it in GitHub Desktop.
Generate click events on touchstart; kill the mouse events that the browser cooks 200+ms afterwards.
(function touchJack(){
var lastTouch = 0;
document.addEventListener("touchstart", function(e){
lastTouch = Date.now();
var touch = e.touches[0],
synth_event = document.createEvent("MouseEvents");
synth_event.fab=true;
synth_event.initMouseEvent("click", true, true, window,
0, touch.screenX, touch.screenY, touch.clientX, touch.clientY,
false, false, false, false, 0, null);
e.target.dispatchEvent(synth_event);
});
["click", "mouseout", "mouseover"].forEach(function(event_type){
document.addEventListener(event_type, function(e){
if(!e.fab && Date.now() < lastTouch + 500){
e.stopPropagation();
e.stopImmediatePropagation();
e.preventDefault();
return false;
}
}, true); // capture phase.
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment