Skip to content

Commit

Permalink
Merge pull request #346 from davidkaneda/master
Browse files Browse the repository at this point in the history
Modification to tap behavior
  • Loading branch information
madrobby committed Dec 1, 2011
2 parents 14c31af + 664aa8f commit 92c8c6d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
16 changes: 9 additions & 7 deletions src/touch.js
Expand Up @@ -21,15 +21,15 @@
var longTapDelay = 750;
function longTap(){
if (touch.last && (Date.now() - touch.last >= longTapDelay)) {
$(touch.target).trigger('longTap');
touch.el.trigger('longTap');
touch = {};
}
}

$(document).ready(function(){
$(document.body).bind('touchstart', function(e){
var now = Date.now(), delta = now - (touch.last || now);
touch.target = parentIfText(e.touches[0].target);
touch.el = $(parentIfText(e.touches[0].target));
touchTimeout && clearTimeout(touchTimeout);
touch.x1 = e.touches[0].pageX;
touch.y1 = e.touches[0].pageY;
Expand All @@ -41,24 +41,26 @@
touch.y2 = e.touches[0].pageY;
}).bind('touchend', function(e){
if (touch.isDoubleTap) {
$(touch.target).trigger('doubleTap');
touch.el.trigger('doubleTap');
touch = {};
} else if (touch.x2 > 0 || touch.y2 > 0) {
(Math.abs(touch.x1 - touch.x2) > 30 || Math.abs(touch.y1 - touch.y2) > 30) &&
$(touch.target).trigger('swipe') &&
$(touch.target).trigger('swipe' + (swipeDirection(touch.x1, touch.x2, touch.y1, touch.y2)));
touch.el.trigger('swipe') &&
touch.el.trigger('swipe' + (swipeDirection(touch.x1, touch.x2, touch.y1, touch.y2)));
touch.x1 = touch.x2 = touch.y1 = touch.y2 = touch.last = 0;
} else if ('last' in touch) {
touch.el.trigger('tap');

touchTimeout = setTimeout(function(){
touchTimeout = null;
$(touch.target).trigger('tap')
touch.el.trigger('singleTap');
touch = {};
}, 250);
}
}).bind('touchcancel', function(){ touch = {} });
});

['swipe', 'swipeLeft', 'swipeRight', 'swipeUp', 'swipeDown', 'doubleTap', 'tap', 'longTap'].forEach(function(m){
['swipe', 'swipeLeft', 'swipeRight', 'swipeUp', 'swipeDown', 'doubleTap', 'tap', 'singleTap', 'longTap'].forEach(function(m){
$.fn[m] = function(callback){ return this.bind(m, callback) }
});
})(Zepto);
3 changes: 3 additions & 0 deletions test/touch_functional.html
Expand Up @@ -46,6 +46,9 @@ <h1>Zepto touch functional test</h1>
})
.longTap(function(){
$(this).append(' | long tap!');
})
.singleTap(function(){
$(this).append(' | single tap!');
});
</script>
</body>
Expand Down

0 comments on commit 92c8c6d

Please sign in to comment.