This repository has been archived by the owner on Jan 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 32
/
pointer.min.js
1 lines (1 loc) · 1.78 KB
/
pointer.min.js
1
(function(){var body=document.body;var isScrolling=false;var timeout=false;var sDistX=0;var sDistY=0;window.addEventListener("scroll",function(){if(!isScrolling){sDistX=window.pageXOffset;sDistY=window.pageYOffset}isScrolling=true;clearTimeout(timeout);timeout=setTimeout(function(){isScrolling=false;sDistX=0;sDistY=0},100)});body.addEventListener("mousedown",pointerDown);body.addEventListener("touchstart",pointerDown);body.addEventListener("mouseup",pointerUp);body.addEventListener("touchend",pointerUp);body.addEventListener("mousemove",pointerMove);body.addEventListener("touchmove",pointerMove);body.addEventListener("mouseout",pointerLeave);body.addEventListener("touchleave",pointerLeave);function pointerDown(e){var evt=makePointerEvent("down",e);var singleFinger=evt.mouse||evt.touch&&e.touches.length===1;if(!isScrolling&&singleFinger){e.target.maybeClick=true;e.target.maybeClickX=evt.x;e.target.maybeClickY=evt.y}}function pointerLeave(e){e.target.maybeClick=false;makePointerEvent("leave",e)}function pointerMove(e){var evt=makePointerEvent("move",e)}function pointerUp(e){var evt=makePointerEvent("up",e);if(e.target.maybeClick){if(Math.abs(e.target.maybeClickX-evt.x)<5&&Math.abs(e.target.maybeClickY-evt.y)<3){if(!isScrolling||Math.abs(sDistX-window.pageXOffset)<5&&Math.abs(sDistY-window.pageYOffset)<5){makePointerEvent("click",e)}}}e.target.maybeClick=false}function makePointerEvent(type,e){var tgt=e.target;var evt=document.createEvent("CustomEvent");evt.initCustomEvent("pointer"+type,true,true,{});evt.touch=e.type.indexOf("touch")===0;evt.mouse=e.type.indexOf("mouse")===0;if(evt.touch){evt.x=e.changedTouches[0].pageX;evt.y=e.changedTouches[0].pageY}if(evt.mouse){evt.x=e.clientX+window.pageXOffset;evt.y=e.clientY+window.pageYOffset}evt.maskedEvent=e;tgt.dispatchEvent(evt);return evt}})();