Created
September 27, 2017 19:42
-
-
Save Pamblam/7d0203e0c011c9bb272c41eac7aec5df to your computer and use it in GitHub Desktop.
listen for tap and swipe on the same element, simultaneously...
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * 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