1. /**
    2. * @name:触屏事件
    3. * @param {string} element dom元素
    4. * @param {function} fn 事件触发函数
    5. ** */
    6. export const touchEvent = {
    7. /* 向上滑动事件 */
    8. swipeUp(element: any, fn: Function) {
    9. let isTouchMove: boolean;
    10. let startTx: number;
    11. let startTy: number;
    12. const touchstartFun = (e: any) => {
    13. const touches = e.touches[0];
    14. startTx = touches.clientX;
    15. startTy = touches.clientY;
    16. isTouchMove = false;
    17. };
    18. const touchmoveFun = () => {
    19. isTouchMove = true;
    20. // e.preventDefault();
    21. };
    22. const touchendFun = (e: any) => {
    23. if (!isTouchMove) {
    24. return;
    25. }
    26. const touches = e.changedTouches[0];
    27. const endTx = touches.clientX;
    28. const endTy = touches.clientY;
    29. const distanceX = startTx - endTx;
    30. const distanceY = startTy - endTy;
    31. // eslint-disable-next-line @typescript-eslint/no-unused-vars
    32. let isSwipe = false;
    33. if (Math.abs(distanceX) < Math.abs(distanceY)) {
    34. if (distanceY > 20) {
    35. fn();
    36. isSwipe = true;
    37. // element.removeEventListener("touchstart", touchstartFun, false);
    38. // element.removeEventListener("touchmove", touchmoveFun, false);
    39. // element.removeEventListener("touchend", touchendFun, false);
    40. }
    41. }
    42. };
    43. element.addEventListener("touchstart", touchstartFun, false);
    44. element.addEventListener("touchmove", touchmoveFun, false);
    45. element.addEventListener("touchend", touchendFun, false);
    46. },
    47. /* 向下滑动事件 */
    48. swipeDown(element: any, fn: Function) {
    49. let isTouchMove: boolean;
    50. let startTx: number;
    51. let startTy: number;
    52. const touchstartFun = (e: any) => {
    53. const touches = e.touches[0];
    54. startTx = touches.clientX;
    55. startTy = touches.clientY;
    56. isTouchMove = false;
    57. };
    58. const touchmoveFun = () => {
    59. isTouchMove = true;
    60. // e.preventDefault();
    61. };
    62. const touchendFun = (e: any) => {
    63. if (!isTouchMove) {
    64. return;
    65. }
    66. const touches = e.changedTouches[0];
    67. const endTx = touches.clientX;
    68. const endTy = touches.clientY;
    69. const distanceX = startTx - endTx;
    70. const distanceY = startTy - endTy;
    71. // eslint-disable-next-line @typescript-eslint/no-unused-vars
    72. let isSwipe = false;
    73. if (Math.abs(distanceX) < Math.abs(distanceY)) {
    74. if (distanceY < -20) {
    75. fn();
    76. isSwipe = true;
    77. // element.removeEventListener("touchstart", touchstartFun, false);
    78. // element.removeEventListener("touchmove", touchmoveFun, false);
    79. // element.removeEventListener("touchend", touchendFun, false);
    80. }
    81. }
    82. };
    83. element.addEventListener("touchstart", touchstartFun, false);
    84. element.addEventListener("touchmove", touchmoveFun, false);
    85. element.addEventListener("touchend", touchendFun, false);
    86. },
    87. };