Skip to content

Commit

Permalink
enables locking an element to no-scroll (preventdefault on touchmove)…
Browse files Browse the repository at this point in the history
… or allowing scroll after touch (eg for buttons), scrolling after touch cancels all tap events.
  • Loading branch information
hunterloftis committed Apr 29, 2012
1 parent a642571 commit ae60cc3
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 11 deletions.
26 changes: 16 additions & 10 deletions src/touch.js
Expand Up @@ -3,7 +3,8 @@
// Zepto.js may be freely distributed under the MIT license.

;(function($){
var touch = {}, touchTimeout
var touch = {}, touchTimeout, tapTimeout
var longTapDelay = 750, longTapTimeout

function parentIfText(node){
return 'tagName' in node ? node : node.parentNode
Expand All @@ -14,8 +15,6 @@
return xDelta >= yDelta ? (x1 - x2 > 0 ? 'Left' : 'Right') : (y1 - y2 > 0 ? 'Up' : 'Down')
}

var longTapDelay = 750, longTapTimeout

function longTap(){
longTapTimeout = null
if (touch.last) {
Expand All @@ -29,6 +28,14 @@
longTapTimeout = null
}

function cancelAll() {
if (touchTimeout) clearTimeout(touchTimeout)
if (tapTimeout) clearTimeout(tapTimeout)
if (longTapTimeout) clearTimeout(longTapTimeout)
longTapTimeout = touchTimeout = null
touch = {}
}

$(document).ready(function(){
var now, delta

Expand Down Expand Up @@ -63,20 +70,19 @@

// normal tap
} else if ('last' in touch) {
touch.el.trigger('tap')
tapTimeout = setTimeout(function() {
touch.el.trigger('tap')
}, 0);

touchTimeout = setTimeout(function(){
touchTimeout = null
touch.el.trigger('singleTap')
touch = {}
}, 250)
}
}).bind('touchcancel', function(){
if (touchTimeout) clearTimeout(touchTimeout)
if (longTapTimeout) clearTimeout(longTapTimeout)
longTapTimeout = touchTimeout = null
touch = {}
})
}).bind('touchcancel', cancelAll)

$(window).bind('scroll', cancelAll)
})

;['swipe', 'swipeLeft', 'swipeRight', 'swipeUp', 'swipeDown', 'doubleTap', 'tap', 'singleTap', 'longTap'].forEach(function(m){
Expand Down
35 changes: 34 additions & 1 deletion test/touch_functional.html
Expand Up @@ -12,10 +12,14 @@
<body>
<h1>Zepto touch functional test</h1>

<div id="touch_test" style="width: 200px; height: 200px; background: #ddd; -webkit-user-select: none">
<div id="touch_test" style="width: 200px; height: 200px; background: #cce; -webkit-user-select: none">
touch events test
</div>

<div id='touch_test_scrollable' style="width: 200px; height: 200px; background: #cec; -webkit-user-select: none">
touch events test (scrollable cancel)
</div>

<div id="browser"> </div>

<script>
Expand Down Expand Up @@ -50,6 +54,35 @@ <h1>Zepto touch functional test</h1>
.singleTap(function(){
$(this).append(' | single tap!')
})

$('#touch_test_scrollable')
.tap(function(){
$(this).append(' | tap!')
})
.doubleTap(function(){
$(this).append(' | double tap!')
})
.swipe(function(){
$(this).append(' | swipe!')
})
.swipeLeft(function(){
$(this).append(' | swipe left!')
})
.swipeRight(function(){
$(this).append(' | swipe right!')
})
.swipeUp(function(){
$(this).append(' | swipe up!')
})
.swipeDown(function(){
$(this).append(' | swipe down!')
})
.longTap(function(){
$(this).append(' | long tap!')
})
.singleTap(function(){
$(this).append(' | single tap!')
})
</script>
</body>
</html>

0 comments on commit ae60cc3

Please sign in to comment.