index.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import Vue from 'vue'
  2. function vueTouch(el, binding, type) {
  3. var _this = this
  4. this.obj = el
  5. this.binding = binding
  6. this.touchType = type
  7. this.vueTouches = { x: 0, y: 0 }
  8. this.vueMoves = true
  9. this.vueLeave = true
  10. this.longTouch = true
  11. this.vueCallBack = typeof (binding.value) == 'object' ? binding.value.fn : binding.value
  12. this.obj.addEventListener('touchstart', function (e) {
  13. _this.start(e)
  14. }, false)
  15. this.obj.addEventListener('touchend', function (e) {
  16. _this.end(e)
  17. }, false)
  18. this.obj.addEventListener('touchmove', function (e) {
  19. _this.move(e)
  20. }, false)
  21. }
  22. vueTouch.prototype = {
  23. start: function (e) {
  24. this.vueMoves = true
  25. this.vueLeave = true
  26. this.longTouch = true
  27. this.vueTouches = { x: e.changedTouches[0].pageX, y: e.changedTouches[0].pageY }
  28. this.time = setTimeout(function () {
  29. if (this.vueLeave && this.vueMoves) {
  30. this.touchType == 'longtap' && this.vueCallBack(this.binding.value, e)
  31. this.longTouch = false
  32. }
  33. }.bind(this), 1000)
  34. },
  35. end: function (e) {
  36. var disX = e.changedTouches[0].pageX - this.vueTouches.x
  37. var disY = e.changedTouches[0].pageY - this.vueTouches.y
  38. clearTimeout(this.time)
  39. if (Math.abs(disX) > 10 || Math.abs(disY) > 100) {
  40. this.touchType == 'swipe' && this.vueCallBack(this.binding.value, e)
  41. if (Math.abs(disX) > Math.abs(disY)) {
  42. if (disX > 10) {
  43. this.touchType == 'swiperight' && this.vueCallBack(this.binding.value, e)
  44. }
  45. if (disX < -10) {
  46. this.touchType == 'swipeleft' && this.vueCallBack(this.binding.value, e)
  47. }
  48. } else {
  49. if (disY > 10) {
  50. this.touchType == 'swipedown' && this.vueCallBack(this.binding.value, e)
  51. }
  52. if (disY < -10) {
  53. this.touchType == 'swipeup' && this.vueCallBack(this.binding.value, e)
  54. }
  55. }
  56. } else {
  57. if (this.longTouch && this.vueMoves) {
  58. this.touchType == 'tap' && this.vueCallBack(this.binding.value, e)
  59. this.vueLeave = false
  60. }
  61. }
  62. },
  63. move: function (e) {
  64. this.vueMoves = false
  65. }
  66. }
  67. const tap = Vue.directive('tap', {
  68. bind: function (el, binding) {
  69. // eslint-disable-next-line no-new,new-cap
  70. new vueTouch(el, binding, 'tap')
  71. }
  72. })
  73. const swipe = Vue.directive('swipe', {
  74. bind: function (el, binding) {
  75. // eslint-disable-next-line no-new,new-cap
  76. new vueTouch(el, binding, 'swipe')
  77. }
  78. })
  79. const swipeleft = Vue.directive('swipeleft', {
  80. bind: function (el, binding) {
  81. // eslint-disable-next-line no-new,new-cap
  82. new vueTouch(el, binding, 'swipeleft')
  83. }
  84. })
  85. const swiperight = Vue.directive('swiperight', {
  86. bind: function (el, binding) {
  87. // eslint-disable-next-line no-new,new-cap
  88. new vueTouch(el, binding, 'swiperight')
  89. }
  90. })
  91. const swipedown = Vue.directive('swipedown', {
  92. bind: function (el, binding) {
  93. // eslint-disable-next-line no-new,new-cap
  94. new vueTouch(el, binding, 'swipedown')
  95. }
  96. })
  97. const swipeup = Vue.directive('swipeup', {
  98. bind: function (el, binding) {
  99. // eslint-disable-next-line no-new,new-cap
  100. new vueTouch(el, binding, 'swipeup')
  101. }
  102. })
  103. const longtap = Vue.directive('longtap', {
  104. bind: function (el, binding) {
  105. // eslint-disable-next-line no-new,new-cap
  106. new vueTouch(el, binding, 'longtap')
  107. }
  108. })
  109. export { longtap, swipeup, swipedown, swiperight, swipeleft, swipe, tap }