Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
/*
* jQuery Double Tap
* Developer: Sergey Margaritov (github.com/attenzione)
* License: MIT
* Date: 22.10.2013
* Based on jquery documentation http://learn.jquery.com/events/event-extensions/
*/
(function($){
$.event.special.doubletap = {
bindType: 'touchend',
delegateType: 'touchend',
handle: function(event) {
var handleObj = event.handleObj,
targetData = jQuery.data(event.target),
now = new Date().getTime(),
delta = targetData.lastTouch ? now - targetData.lastTouch : 0,
delay = delay == null ? 300 : delay;
if (delta < delay && delta > 30) {
targetData.lastTouch = null;
event.type = handleObj.origType;
['clientX', 'clientY', 'pageX', 'pageY'].forEach(function(property) {
event[property] = event.originalEvent.changedTouches[0][property];
})
// let jQuery handle the triggering of "doubletap" event handlers
handleObj.handler.apply(this, arguments);
} else {
targetData.lastTouch = now;
}
}
};
})(jQuery);
@daniel-990
Copy link

daniel-990 commented Dec 11, 2015

thanks!!! :) 👍

@thdoan
Copy link

thdoan commented Dec 28, 2015

There doesn't appear to be a way to pass a delay value, but it's a great plugin nonetheless 👍

@yairEO
Copy link

yairEO commented Feb 13, 2016

I've made a tiny custom event which is superior - https://github.com/yairEO/touchy

@eldyvoon
Copy link

eldyvoon commented Nov 5, 2016

@yairEO, how does that different than this one?

@zanderwar
Copy link

zanderwar commented Sep 3, 2019

@yairEO You have used tabs AND spaces, and have left debugging code in there...

@VigibotDev
Copy link

VigibotDev commented Sep 25, 2019

Hello, It need a filter for two finger multitouch usage !
The filter must discard two different location taps !

@attenzione
Copy link
Author

attenzione commented Oct 1, 2019

Hello, It need a filter for two finger multitouch usage !
The filter must discard two different location taps !

You need to parse event params:

if (e.touches && e.touches.length === 2) {
  // two finger multitouch event
}

@VigibotDev
Copy link

VigibotDev commented Oct 1, 2019

This is a part of the work yes, now the filter must discard two different location taps

@attenzione
Copy link
Author

attenzione commented Oct 1, 2019

This is a part of the work yes, now the filter must discard two different location taps

Nice, I can't give you an answer with one row of code, you need to implement some logic with event touches coordinates

@VigibotDev
Copy link

VigibotDev commented Oct 1, 2019

Sorry for my english I'm French:) Yes it's mandatory because when we slide with two fingers there is a lot bad taps received and this frequently make a false double tap.
just store the last tap coordinate and make a difference with the new one to get the delta. If delta > tolerance; return.
There is no need for touches.length check.

@Senior-Hayato-Suzuki
Copy link

Senior-Hayato-Suzuki commented Apr 4, 2020

Awesome !!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment