Skip to content

Instantly share code, notes, and snippets.

@YujiSODE
Created February 28, 2017 08:52
Show Gist options
  • Save YujiSODE/adb1943695d8cbe1cff29efc90b1bc53 to your computer and use it in GitHub Desktop.
Save YujiSODE/adb1943695d8cbe1cff29efc90b1bc53 to your computer and use it in GitHub Desktop.
function simulates mouse event via touch event.
//touchEvt2mouseEvt.js
//Copyright (c) 2017 Yuji SODE <yuji.sode@gmail.com>
//Handling clicks with touch event
(function(){
var slf=window,events=['touchstart','touchmove','touchend'],i=0,n=events.length,
/*this function simulates mouse event via touch event*/
touch2MouseEvt=function(e){
//e is event object
e.preventDefault();
if(!!e.changedTouches&&e.changedTouches.length>0){
var touch0=e.changedTouches[0],
/*function simulates new mouse event*/
newMEvt=function(EventName,tObj,tgt){
//tObj and tgt are touch object and target element
var E=new MouseEvent(EventName,{
'view':window,
'bubbles':true,
'cancelable':true,
'clientX':tObj.clientX,
'clientY':tObj.clientY,
'ctrlKey':tObj.ctrlKey,
'shiftKey':tObj.shiftKey,
'altKey':tObj.altKey
});
tgt.dispatchEvent(E);
};
switch(e.type){
case 'touchstart':
newMEvt('mousedown',touch0,e.target);
break;
case 'touchmove':
newMEvt('mousemove',touch0,e.target);
break;
case 'touchend':
newMEvt('mouseup',touch0,e.target);
break;
}
}else{return;}
};
while(i<n){slf.addEventListener(events[i],touch2MouseEvt,true),i+=1;}
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment