touch.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. ;(function($){
  2. var touch = {},
  3. touchTimeout, tapTimeout, swipeTimeout, longTapTimeout,
  4. longTapDelay = 750,
  5. gesture
  6. function swipeDirection(x1, x2, y1, y2) {
  7. return Math.abs(x1 - x2) >=
  8. Math.abs(y1 - y2) ? (x1 - x2 > 0 ? 'Left' : 'Right') : (y1 - y2 > 0 ? 'Up' : 'Down')
  9. }
  10. function longTap() {
  11. longTapTimeout = null
  12. if (touch.last) {
  13. touch.el.trigger('longTap')
  14. touch = {}
  15. }
  16. }
  17. function cancelLongTap() {
  18. if (longTapTimeout) clearTimeout(longTapTimeout)
  19. longTapTimeout = null
  20. }
  21. function cancelAll() {
  22. if (touchTimeout) clearTimeout(touchTimeout)
  23. if (tapTimeout) clearTimeout(tapTimeout)
  24. if (swipeTimeout) clearTimeout(swipeTimeout)
  25. if (longTapTimeout) clearTimeout(longTapTimeout)
  26. touchTimeout = tapTimeout = swipeTimeout = longTapTimeout = null
  27. touch = {}
  28. }
  29. function isPrimaryTouch(event){
  30. return (event.pointerType == 'touch' ||
  31. event.pointerType == event.MSPOINTER_TYPE_TOUCH)
  32. && event.isPrimary
  33. }
  34. function isPointerEventType(e, type){
  35. return (e.type == 'pointer'+type ||
  36. e.type.toLowerCase() == 'mspointer'+type)
  37. }
  38. $(document).ready(function(){
  39. var now, delta, deltaX = 0, deltaY = 0, firstTouch, _isPointerType
  40. if ('MSGesture' in window) {
  41. gesture = new MSGesture()
  42. gesture.target = document.body
  43. }
  44. $(document)
  45. .bind('MSGestureEnd', function(e){
  46. var swipeDirectionFromVelocity =
  47. e.velocityX > 1 ? 'Right' : e.velocityX < -1 ? 'Left' : e.velocityY > 1 ? 'Down' : e.velocityY < -1 ? 'Up' : null;
  48. if (swipeDirectionFromVelocity) {
  49. touch.el.trigger('swipe')
  50. touch.el.trigger('swipe'+ swipeDirectionFromVelocity)
  51. }
  52. })
  53. .on('touchstart MSPointerDown pointerdown', function(e){
  54. if((_isPointerType = isPointerEventType(e, 'down')) &&
  55. !isPrimaryTouch(e)) return
  56. firstTouch = _isPointerType ? e : e.touches[0]
  57. if (e.touches && e.touches.length === 1 && touch.x2) {
  58. // Clear out touch movement data if we have it sticking around
  59. // This can occur if touchcancel doesn't fire due to preventDefault, etc.
  60. touch.x2 = undefined
  61. touch.y2 = undefined
  62. }
  63. now = Date.now()
  64. delta = now - (touch.last || now)
  65. touch.el = $('tagName' in firstTouch.target ?
  66. firstTouch.target : firstTouch.target.parentNode)
  67. touchTimeout && clearTimeout(touchTimeout)
  68. touch.x1 = firstTouch.pageX
  69. touch.y1 = firstTouch.pageY
  70. if (delta > 0 && delta <= 250) touch.isDoubleTap = true
  71. touch.last = now
  72. longTapTimeout = setTimeout(longTap, longTapDelay)
  73. // adds the current touch contact for IE gesture recognition
  74. if (gesture && _isPointerType) gesture.addPointer(e.pointerId);
  75. })
  76. .on('touchmove MSPointerMove pointermove', function(e){
  77. if((_isPointerType = isPointerEventType(e, 'move')) &&
  78. !isPrimaryTouch(e)) return
  79. firstTouch = _isPointerType ? e : e.touches[0]
  80. cancelLongTap()
  81. touch.x2 = firstTouch.pageX
  82. touch.y2 = firstTouch.pageY
  83. deltaX += Math.abs(touch.x1 - touch.x2)
  84. deltaY += Math.abs(touch.y1 - touch.y2)
  85. })
  86. .on('touchend MSPointerUp pointerup', function(e){
  87. if((_isPointerType = isPointerEventType(e, 'up')) &&
  88. !isPrimaryTouch(e)) return
  89. cancelLongTap()
  90. // swipe
  91. if ((touch.x2 && Math.abs(touch.x1 - touch.x2) > 30) ||
  92. (touch.y2 && Math.abs(touch.y1 - touch.y2) > 30))
  93. swipeTimeout = setTimeout(function() {
  94. touch.el.trigger('swipe')
  95. touch.el.trigger('swipe' + (swipeDirection(touch.x1, touch.x2, touch.y1, touch.y2)))
  96. touch = {}
  97. }, 0)
  98. // normal tap
  99. else if ('last' in touch)
  100. // don't fire tap when delta position changed by more than 30 pixels,
  101. // for instance when moving to a point and back to origin
  102. if (deltaX < 30 && deltaY < 30) {
  103. // delay by one tick so we can cancel the 'tap' event if 'scroll' fires
  104. // ('tap' fires before 'scroll')
  105. tapTimeout = setTimeout(function() {
  106. // trigger universal 'tap' with the option to cancelTouch()
  107. // (cancelTouch cancels processing of single vs double taps for faster 'tap' response)
  108. var event = $.Event('tap')
  109. event.cancelTouch = cancelAll
  110. touch.el.trigger(event)
  111. // trigger double tap immediately
  112. if (touch.isDoubleTap) {
  113. if (touch.el) touch.el.trigger('doubleTap')
  114. touch = {}
  115. }
  116. // trigger single tap after 250ms of inactivity
  117. else {
  118. touchTimeout = setTimeout(function(){
  119. touchTimeout = null
  120. if (touch.el) touch.el.trigger('singleTap')
  121. touch = {}
  122. }, 250)
  123. }
  124. }, 0)
  125. } else {
  126. touch = {}
  127. }
  128. deltaX = deltaY = 0
  129. })
  130. // when the browser window loses focus,
  131. // for example when a modal dialog is shown,
  132. // cancel all ongoing events
  133. .on('touchcancel MSPointerCancel pointercancel', cancelAll)
  134. // scrolling the window indicates intention of the user
  135. // to scroll, not tap or swipe, so cancel all ongoing events
  136. $(window).on('scroll', cancelAll)
  137. })
  138. ;['swipe', 'swipeLeft', 'swipeRight', 'swipeUp', 'swipeDown',
  139. 'doubleTap', 'tap', 'singleTap', 'longTap'].forEach(function(eventName){
  140. $.fn[eventName] = function(callback){ return this.on(eventName, callback) }
  141. })
  142. })(Zepto)