Skip to content

Instantly share code, notes, and snippets.

@itsravenous
Created May 31, 2012 15:12
Show Gist options
  • Save itsravenous/2844052 to your computer and use it in GitHub Desktop.
Save itsravenous/2844052 to your computer and use it in GitHub Desktop.
Zepto.js - Adding touches object to data passed with tap events
$(document.body).bind('touchstart', function(e){
now = Date.now()
delta = now - (touch.last || now)
touch.el = $(parentIfText(e.touches[0].target))
touchTimeout && clearTimeout(touchTimeout)
touch.touches = e.touches // Store touch list
touch.x1 = e.touches[0].pageX
touch.y1 = e.touches[0].pageY
if (delta > 0 && delta <= 250) touch.isDoubleTap = true
touch.last = now
longTapTimeout = setTimeout(longTap, longTapDelay)
fingers = e.touches.length;
}).bind('touchmove', function(e){
cancelLongTap()
touch.x2 = e.touches[0].pageX
touch.y2 = e.touches[0].pageY
}).bind('touchend', function(e){
cancelLongTap()
// double tap (tapped twice within 250ms)
if (touch.isDoubleTap) {
touch.el.trigger('doubleTap', [touch]) // Pass touch list along with event
touch = {}
// swipe
} else if ((touch.x2 && Math.abs(touch.x1 - touch.x2) > 30) ||
(touch.y2 && Math.abs(touch.y1 - touch.y2) > 30)) {
touch.el.trigger('swipe') &&
touch.el.trigger('swipe' + (swipeDirection(touch.x1, touch.x2, touch.y1, touch.y2)))
touch = {}
// normal tap
} else if ('last' in touch) {
touch.el.trigger('tap', [touch]) // Pass touch list along with event
touchTimeout = setTimeout(function(){
touchTimeout = null
touch.el.trigger('singleTap', [touch]) // Pass touch list along with event
touch = {}
}, 250)
}
}).bind('touchcancel', function(){
if (touchTimeout) clearTimeout(touchTimeout)
if (longTapTimeout) clearTimeout(longTapTimeout)
longTapTimeout = touchTimeout = null
touch = {}
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment