jquery.finger
Version:
jQuery Finger unifies click and touch events by removing the 300ms delay on touch devices. It also provide a common set of events to handle basic gestures such as drag and pinch. Small (< 1kb gzipped), it is focused on performance, is well tested and ...
4 lines • 2.25 kB
JavaScript
/*! jquery.finger - v0.1.6 - 2016-10-05
* https://github.com/ngryman/jquery.finger
* Copyright (c) 2016 Nicolas Gryman; Licensed MIT */
!function(a){"function"==typeof define&&define.amd?define(["jquery"],a):a("object"==typeof exports?require("jquery"):jQuery)}(function(a){function b(c){c.preventDefault(),a.event.remove(u,"click",b)}function c(a,b){return(p?b.originalEvent.touches[0]:b)["page"+a.toUpperCase()]}function d(c,d,e){var h=a.Event(d,w);a.event.trigger(h,{originalEvent:c},c.target),h.isDefaultPrevented()&&(~d.indexOf("tap")&&!p?a.event.add(u,"click",b):c.preventDefault()),e&&(a.event.remove(u,s+"."+t,f),a.event.remove(u,r+"."+t,g))}function e(e){if(!(e.which>1)){var k=e.timeStamp||+new Date;i!=k&&(i=k,v.x=w.x=c("x",e),v.y=w.y=c("y",e),v.time=k,v.target=e.target,w.orientation=null,w.end=!1,h=!1,j=setTimeout(function(){d(e,"press",!0)},x.pressDuration),a.event.add(u,s+"."+t,f),a.event.add(u,r+"."+t,g),x.preventDefault&&(e.preventDefault(),a.event.add(u,"click",b)))}}function f(b){if(w.x=c("x",b),w.y=c("y",b),w.dx=w.x-v.x,w.dy=w.y-v.y,w.adx=Math.abs(w.dx),w.ady=Math.abs(w.dy),h=w.adx>x.motionThreshold||w.ady>x.motionThreshold){for(clearTimeout(j),w.orientation||(w.adx>w.ady?(w.orientation="horizontal",w.direction=w.dx>0?1:-1):(w.orientation="vertical",w.direction=w.dy>0?1:-1));b.target&&b.target!==v.target;)b.target=b.target.parentNode;return b.target!==v.target?(b.target=v.target,void g.call(this,a.Event(r+"."+t,b))):void d(b,"drag")}}function g(a){var b,c=a.timeStamp||+new Date,e=c-v.time;if(clearTimeout(j),h)a.target=v.target,e<x.flickDuration&&d(a,"flick"),w.end=!0,b="drag";else if(a.target===v.target){var f=k===a.target&&c-l<x.doubleTapInterval;b=f?"doubletap":"tap",k=f?null:v.target,l=c}b&&d(a,b,!0)}var h,i,j,k,l,m=navigator.userAgent,n=/chrome/i.exec(m),o=/android/i.exec(m),p="ontouchstart"in window&&!(n&&!o),q=p?"touchstart":"mousedown",r=p?"touchend touchcancel":"mouseup mouseleave",s=p?"touchmove":"mousemove",t="finger",u=a("html")[0],v={},w={},x=a.Finger={pressDuration:300,doubleTapInterval:300,flickDuration:150,motionThreshold:5};return a.event.add(u,q+"."+t,e),a.each("tap doubletap press drag flick".split(" "),function(b,c){a.fn[c]=function(a){return a?this.on(c,a):this.trigger(c)}}),x});