1. <!--
    2. * @Descripttion:
    3. * @version:
    4. * @Author: WangPeng
    5. * @Date: 2021-09-17 18:12:55
    6. * @LastEditors: WangPeng
    7. * @LastEditTime: 2021-09-18 18:25:56
    8. -->
    9. <!doctype html>
    10. <html>
    11. <head>
    12. <meta charset="utf-8">
    13. <meta name="viewport"
    14. content="width=device-width, initial-scale=1, minimum-scale=1, maximum-scale=1, user-scalable=no">
    15. <title>无标题文档</title>
    16. <style>
    17. div {
    18. width: 100px;
    19. height: 100px;
    20. line-height: 100px;
    21. margin-bottom: 10px;
    22. background: red;
    23. text-align: center;
    24. color: #fff;
    25. }
    26. </style>
    27. <script>
    28. /***
    29. @name:触屏事件
    30. @param {string} element dom元素
    31. {function} fn 事件触发函数
    32. ***/
    33. var touchEvent = {
    34. /*单次触摸事件*/
    35. tap: function (element, fn) {
    36. var startTx, startTy;
    37. element.addEventListener('touchstart', function (e) {
    38. var touches = e.touches[0];
    39. startTx = touches.clientX;
    40. startTy = touches.clientY;
    41. }, false);
    42. element.addEventListener('touchend', function (e) {
    43. var touches = e.changedTouches[0],
    44. endTx = touches.clientX,
    45. endTy = touches.clientY;
    46. // 在部分设备上 touch 事件比较灵敏,导致按下和松开手指时的事件坐标会出现一点点变化
    47. if (Math.abs(startTx - endTx) < 6 && Math.abs(
    48. startTy - endTy) < 6) {
    49. fn();
    50. }
    51. }, false);
    52. },
    53. /*两次触摸事件*/
    54. doubleTap: function (element, fn) {
    55. var isTouchEnd = false,
    56. lastTime = 0,
    57. lastTx = null,
    58. lastTy = null,
    59. firstTouchEnd = true,
    60. body = document.body,
    61. dTapTimer, startTx, startTy, startTime;
    62. element.addEventListener('touchstart', function (e) {
    63. if (dTapTimer) {
    64. clearTimeout(dTapTimer);
    65. dTapTimer = null;
    66. }
    67. var touches = e.touches[0];
    68. startTx = touches.clientX;
    69. startTy = touches.clientY;
    70. }, false);
    71. element.addEventListener('touchend', function (e) {
    72. var touches = e.changedTouches[0],
    73. endTx = touches.clientX,
    74. endTy = touches.clientY,
    75. now = Date.now(),
    76. duration = now - lastTime;
    77. // 首先要确保能触发单次的 tap 事件
    78. if (Math.abs(startTx - endTx) < 6 && Math.abs(
    79. startTx - endTx) < 6) {
    80. // 两次 tap 的间隔确保在 500 毫秒以内
    81. if (duration < 301) {
    82. // 本次的 tap 位置和上一次的 tap 的位置允许一定范围内的误差
    83. if (lastTx !== null &&
    84. Math.abs(lastTx -
    85. endTx) < 45 &&
    86. Math.abs(lastTy -
    87. endTy) < 45) {
    88. firstTouchEnd = true;
    89. lastTx = lastTy = null;
    90. fn();
    91. }
    92. } else {
    93. lastTx = endTx;
    94. lastTy = endTy;
    95. }
    96. } else {
    97. firstTouchEnd = true;
    98. lastTx = lastTy = null;
    99. }
    100. lastTime = now;
    101. }, false);
    102. // 在 iOS 的 safari 上手指敲击屏幕的速度过快,
    103. // 有一定的几率会导致第二次不会响应 touchstart 和 touchend 事件
    104. // 同时手指长时间的touch不会触发click
    105. if (~navigator.userAgent.toLowerCase().indexOf('iphone os')) {
    106. body.addEventListener('touchstart', function (e) {
    107. startTime = Date.now();
    108. }, true);
    109. body.addEventListener('touchend', function (e) {
    110. var noLongTap = Date.now() - startTime <
    111. 501;
    112. if (firstTouchEnd) {
    113. firstTouchEnd = false;
    114. if (noLongTap && e.target ===
    115. element) {
    116. dTapTimer = setTimeout(
    117. function () {
    118. firstTouchEnd
    119. =
    120. true;
    121. lastTx = lastTy =
    122. null;
    123. fn
    124. ();
    125. }, 400);
    126. }
    127. } else {
    128. firstTouchEnd = true;
    129. }
    130. }, true);
    131. // iOS 上手指多次敲击屏幕时的速度过快不会触发 click 事件
    132. element.addEventListener('click', function (e) {
    133. if (dTapTimer) {
    134. clearTimeout(dTapTimer);
    135. dTapTimer = null;
    136. firstTouchEnd = true;
    137. }
    138. }, false);
    139. }
    140. },
    141. /*长按事件*/
    142. longTap: function (element, fn) {
    143. var startTx, startTy, lTapTimer;
    144. element.addEventListener('touchstart', function (e) {
    145. if (lTapTimer) {
    146. clearTimeout(lTapTimer);
    147. lTapTimer = null;
    148. }
    149. var touches = e.touches[0];
    150. startTx = touches.clientX;
    151. startTy = touches.clientY;
    152. lTapTimer = setTimeout(function () {
    153. fn();
    154. }, 1000);
    155. e.preventDefault();
    156. }, false);
    157. element.addEventListener('touchmove', function (e) {
    158. var touches = e.touches[0],
    159. endTx = touches.clientX,
    160. endTy = touches.clientY;
    161. if (lTapTimer && (Math.abs(endTx - startTx) >
    162. 5 || Math.abs(endTy - startTy) >
    163. 5)) {
    164. clearTimeout(lTapTimer);
    165. lTapTimer = null;
    166. }
    167. }, false);
    168. element.addEventListener('touchend', function (e) {
    169. if (lTapTimer) {
    170. clearTimeout(lTapTimer);
    171. lTapTimer = null;
    172. }
    173. }, false);
    174. },
    175. /*滑屏事件*/
    176. swipe: function (element, fn) {
    177. var isTouchMove, startTx, startTy;
    178. element.addEventListener('touchstart', function (e) {
    179. var touches = e.touches[0];
    180. startTx = touches.clientX;
    181. startTy = touches.clientY;
    182. isTouchMove = false;
    183. }, false);
    184. element.addEventListener('touchmove', function (e) {
    185. isTouchMove = true;
    186. e.preventDefault();
    187. }, false);
    188. element.addEventListener('touchend', function (e) {
    189. if (!isTouchMove) {
    190. return;
    191. }
    192. var touches = e.changedTouches[0],
    193. endTx = touches.clientX,
    194. endTy = touches.clientY,
    195. distanceX = startTx - endTx
    196. distanceY = startTy - endTy,
    197. isSwipe = false;
    198. if (Math.abs(distanceX) > 20 || Math.abs(
    199. distanceY) > 20) {
    200. fn();
    201. }
    202. }, false);
    203. },
    204. /*向上滑动事件*/
    205. swipeUp: function (element, fn) {
    206. var isTouchMove, startTx, startTy;
    207. element.addEventListener('touchstart', function (e) {
    208. var touches = e.touches[0];
    209. startTx = touches.clientX;
    210. startTy = touches.clientY;
    211. isTouchMove = false;
    212. }, false);
    213. element.addEventListener('touchmove', function (e) {
    214. isTouchMove = true;
    215. e.preventDefault();
    216. }, false);
    217. element.addEventListener('touchend', function (e) {
    218. if (!isTouchMove) {
    219. return;
    220. }
    221. var touches = e.changedTouches[0],
    222. endTx = touches.clientX,
    223. endTy = touches.clientY,
    224. distanceX = startTx - endTx
    225. distanceY = startTy - endTy,
    226. isSwipe = false;
    227. if (Math.abs(distanceX) < Math.abs(distanceY)) {
    228. if (distanceY > 20) {
    229. fn();
    230. isSwipe = true;
    231. }
    232. }
    233. }, false);
    234. },
    235. /*向下滑动事件*/
    236. swipeDown: function (element, fn) {
    237. var isTouchMove, startTx, startTy;
    238. element.addEventListener('touchstart', function (e) {
    239. var touches = e.touches[0];
    240. startTx = touches.clientX;
    241. startTy = touches.clientY;
    242. isTouchMove = false;
    243. }, false);
    244. element.addEventListener('touchmove', function (e) {
    245. isTouchMove = true;
    246. e.preventDefault();
    247. }, false);
    248. element.addEventListener('touchend', function (e) {
    249. if (!isTouchMove) {
    250. return;
    251. }
    252. var touches = e.changedTouches[0],
    253. endTx = touches.clientX,
    254. endTy = touches.clientY,
    255. distanceX = startTx - endTx
    256. distanceY = startTy - endTy,
    257. isSwipe = false;
    258. if (Math.abs(distanceX) < Math.abs(distanceY)) {
    259. if (distanceY < -20) {
    260. fn();
    261. isSwipe = true;
    262. }
    263. }
    264. }, false);
    265. },
    266. /*向左滑动事件*/
    267. swipeLeft: function (element, fn) {
    268. var isTouchMove, startTx, startTy;
    269. element.addEventListener('touchstart', function (e) {
    270. var touches = e.touches[0];
    271. startTx = touches.clientX;
    272. startTy = touches.clientY;
    273. isTouchMove = false;
    274. }, false);
    275. element.addEventListener('touchmove', function (e) {
    276. isTouchMove = true;
    277. e.preventDefault();
    278. }, false);
    279. element.addEventListener('touchend', function (e) {
    280. if (!isTouchMove) {
    281. return;
    282. }
    283. var touches = e.changedTouches[0],
    284. endTx = touches.clientX,
    285. endTy = touches.clientY,
    286. distanceX = startTx - endTx
    287. distanceY = startTy - endTy,
    288. isSwipe = false;
    289. if (Math.abs(distanceX) >= Math.abs(
    290. distanceY)) {
    291. if (distanceX > 20) {
    292. fn();
    293. isSwipe = true;
    294. }
    295. }
    296. }, false);
    297. },
    298. /*向右滑动事件*/
    299. swipeRight: function (element, fn) {
    300. var isTouchMove, startTx, startTy;
    301. var touchstart, touchmove, touchend;
    302. touchstart = function (e) {
    303. var touches = e.touches[0];
    304. startTx = touches.clientX;
    305. startTy = touches.clientY;
    306. isTouchMove = false;
    307. };
    308. touchmove = function (e) {
    309. isTouchMove = true;
    310. e.preventDefault();
    311. };
    312. touchend = function (e) {
    313. if (!isTouchMove) {
    314. return;
    315. }
    316. var touches = e.changedTouches[0],
    317. endTx = touches.clientX,
    318. endTy = touches.clientY,
    319. distanceX = startTx - endTx
    320. distanceY = startTy - endTy,
    321. isSwipe = false;
    322. if (Math.abs(distanceX) >= Math.abs(distanceY)) {
    323. if (distanceX < -20) {
    324. fn();
    325. isSwipe = true;
    326. element.removeEventListener('touchstart',
    327. touchstart, false);
    328. element.removeEventListener('touchmove',
    329. touchmove, false);
    330. element.removeEventListener('touchend',
    331. touchend, false);
    332. }
    333. }
    334. };
    335. element.addEventListener('touchstart', touchstart, false);
    336. element.addEventListener('touchmove', touchmove, false);
    337. element.addEventListener('touchend', touchend, false);
    338. }
    339. }
    340. </script>
    341. <script>
    342. window.onload = function () {
    343. var aDiv = document.getElementsByTagName("div");
    344. touchEvent.tap(aDiv[0], function () {
    345. alert("单次触摸");
    346. })
    347. touchEvent.doubleTap(aDiv[1], function () {
    348. alert("两次触摸");
    349. })
    350. touchEvent.longTap(aDiv[2], function () {
    351. alert("长按");
    352. })
    353. touchEvent.swipe(document, function () {
    354. alert("滑屏了");
    355. })
    356. touchEvent.swipeUp(document, function () {
    357. alert("向上滑屏了");
    358. })
    359. touchEvent.swipeDown(document, function () {
    360. alert("向下滑屏了");
    361. })
    362. touchEvent.swipeLeft(document, function () {
    363. alert("向左滑屏了");
    364. })
    365. touchEvent.swipeRight(document, function () {
    366. alert("向右滑屏了");
    367. })
    368. }
    369. </script>
    370. </head>
    371. <body>
    372. <div class="box1">单次触摸我</div>
    373. <div class="box2">两次触摸</div>
    374. <div class="box3">长按我</div>
    375. <span>试一试在屏幕上任意区域滑动</span>
    376. </body>
    377. </html>