jQueryTouch.js 5.8 KB

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