Skip to content

Instantly share code, notes, and snippets.

@Pamblam
Created September 27, 2017 19:42
Show Gist options
  • Select an option

  • Save Pamblam/7d0203e0c011c9bb272c41eac7aec5df to your computer and use it in GitHub Desktop.

Select an option

Save Pamblam/7d0203e0c011c9bb272c41eac7aec5df to your computer and use it in GitHub Desktop.
listen for tap and swipe on the same element, simultaneously...
/**
* listen for tap and swipe on the same element, simultaneously...
*/
let tapSwipe = (()=>{
var lastSwipe = 0;
return (ele, cb) => {
let isAction = false;
let downAt = {x:0, y:0};
let downHandler = (e) => {
downAt = {x: e.touches[0].pageX, y: e.touches[0].pageY};
isAction = true;
};
let upHandler = (e) => {
if(!isAction) return;
if(new Date().getTime() - lastSwipe < 100) return;
isAction = false;
lastSwipe = new Date().getTime()
let diffx = Math.abs(downAt.x - e.changedTouches[0].pageX);
let diffy = Math.abs(downAt.y - e.changedTouches[0].pageY);
let type = diffx > 3 || diffy > 3 ? "swipe" : "tap";
cb.call(ele, e, type);
};
ele.addEventListener('touchstart', downHandler, false);
document.addEventListener('touchend', upHandler, false);
};
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment