Greasy Fork镜像 还支持 简体中文。

jQueryTouchActionEx

簡単なタッチ操作のイベントを追加します

此腳本不應該直接安裝,它是一個供其他腳本使用的函式庫。欲使用本函式庫,請在腳本 metadata 寫上: // @require https://update.gf.qytechs.cn/scripts/419065/894972/jQueryTouchActionEx.js

  1. // ==UserScript==
  2. // @name jQuery touch action Ex
  3. // @version 0.3.1
  4. // @description 簡単なタッチ操作のイベントを追加します
  5. // @author y_kahou
  6. // ==/UserScript==
  7. if (window.jQuery) (function($) {
  8. 'use strict';
  9. // 通常のタップ
  10. $.fn.tap = function(interval = 200) {
  11. // すでにtapまたはdoubletapを設定していたら何もしない
  12. if ($(this).prop('tap') || $(this).prop('doubletap'))
  13. return $(this)
  14. $(this).attr('tap','')
  15. return $(this).on({
  16. touchstart: function(e) {
  17. if (e.targetTouches.length > 1) {
  18. $(this).data('tap', false)
  19. return
  20. }
  21. $(this).data('tap', true)
  22. var to = setTimeout(() => {
  23. $(this).data('tap', false)
  24. $(this).trigger('hold')
  25. }, interval)
  26. $(this).data('tap_to', to)
  27. },
  28. touchmove: function(e) {
  29. if (!$(this).data('tap'))
  30. return
  31. $(this).trigger('subfocus')
  32. $(this).data('tap', false)
  33. clearTimeout($(this).data('tap_to'))
  34. },
  35. touchend: function(e) {
  36. if (!$(this).data('tap'))
  37. return
  38. $(this).data('tap', false)
  39. clearTimeout($(this).data('tap_to'))
  40. e.type = 'tap'
  41. e.touches = e.changedTouches
  42. e.changedTouches = void(0)
  43. $(this).trigger(e)
  44. e.preventDefault()
  45. }
  46. })
  47. }
  48. // ダブルタップを考慮したタップとダブルタップ
  49. $.fn.doubletap = function(interval = 200) {
  50. // すでにtapまたはdoubletapを設定していたら何もしない
  51. if ($(this).prop('tap') || $(this).prop('doubletap'))
  52. return $(this)
  53. $(this).attr('doubletap','')
  54. return $(this).on({
  55. touchstart: function(e) {
  56. if (e.targetTouches.length > 1) {
  57. $(this).data('tap', false)
  58. return
  59. }
  60. $(this).data('tap', true)
  61. var to = setTimeout(() => {
  62. $(this).data('tap', false)
  63. $(this).trigger('hold')
  64. }, interval)
  65. $(this).data('tap_to', to)
  66. },
  67. touchmove: function(e) {
  68. if (!$(this).data('tap'))
  69. return
  70. $(this).trigger('subfocus')
  71. $(this).data('tap', false)
  72. clearTimeout($(this).data('tap_to'))
  73. },
  74. touchend: function(e) {
  75. if (!$(this).data('tap'))
  76. return
  77. $(this).data('tap', false)
  78. clearTimeout($(this).data('tap_to'))
  79. e.type = 'temp'
  80. $(this).trigger(e)
  81. e.preventDefault()
  82. },
  83. temp: function(e) {
  84. e.touches = e.changedTouches
  85. e.changedTouches = void(0)
  86. if ($(this).data('dbltap')) {
  87. $(this).data('dbltap', false)
  88. clearTimeout($(this).data('dbltap_to'))
  89. e.type = 'doubletap'
  90. $(this).trigger(e)
  91. } else {
  92. $(this).data('dbltap', true)
  93. var to = setTimeout(() => {
  94. $(this).data('dbltap', false)
  95. e.type = 'tap'
  96. $(this).trigger(e)
  97. }, interval)
  98. $(this).data('dbltap_to', to)
  99. }
  100. }
  101. })
  102. }
  103. $.fn.swipe = function(min_dist = 50) {
  104. // すでにswipeを設定していたら何もしない
  105. if ($(this).prop('swipe'))
  106. return $(this)
  107. $(this).attr('swipe','')
  108. return $(this).on({
  109. touchstart: function(e) {
  110. if (e.touches.length > 1) {
  111. $(this).data('start_point', '')
  112. return
  113. }
  114. var x = Math.floor(e.touches[0].screenX)
  115. var y = Math.floor(e.touches[0].screenY)
  116. $(this).data('start_point', `${x},${y}`)
  117. },
  118. touchmove: function(e) {
  119. if (e.touches.length > 1) {
  120. $(this).data('start_point', '')
  121. return
  122. }
  123. },
  124. touchend: function(e) {
  125. if (e.changedTouches.length > 1)
  126. return
  127. var p = $(this).data('start_point')
  128. if (!p)
  129. return
  130. var x1 = Number(p.split(',')[0])
  131. var y1 = Number(p.split(',')[1])
  132. var x2 = Math.floor(e.changedTouches[0].screenX)
  133. var y2 = Math.floor(e.changedTouches[0].screenY)
  134. var ang = Math.atan2(y2 - y1, x2 - x1) * 180 / Math.PI
  135. if (ang < 0) ang += 360
  136. var dist = Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2))
  137. if (min_dist < dist) {
  138. let from = { x: x1, y: y1 }
  139. let dest = { x: x2, y: y2 }
  140. $(this).trigger('swipe', [ang, dist, from, dest])
  141. }
  142. }
  143. })
  144. }
  145. // 4方向のスワイプ判定
  146. $.fn.swipe_way = function({min_dist = 50, range = 90} = {}) {
  147. // すでにswipe_wayを設定していたら何もしない
  148. if ($(this).prop('swipe_way'))
  149. return $(this)
  150. // swipe未設定なら設定
  151. if (!$(this).prop('swipe')) {
  152. $(this).swipe(min_dist)
  153. }
  154. $(this).removeAttr('swipe')
  155. $(this).attr('swipe_way','')
  156. return $(this).on('swipe', function(e, ang, dist, from, dest) {
  157. const ways = ['right', 'down', 'left', 'up']
  158. for (var i in ways) {
  159. var ang_ = (i==0 && 270<ang ? ang-360 : ang)
  160. if (Math.abs(90 * i - ang_) <= range / 2) {
  161. $(this).trigger('swipe' + ways[i], [ang, dist, from, dest])
  162. break
  163. }
  164. }
  165. })
  166. }
  167. })(window.jQuery);
  168.  

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址