/**
* @name:触屏事件
* @param {string} element dom元素
* @param {function} fn 事件触发函数
** */
export const touchEvent = {
/* 向上滑动事件 */
swipeUp(element: any, fn: Function) {
let isTouchMove: boolean;
let startTx: number;
let startTy: number;
const touchstartFun = (e: any) => {
const touches = e.touches[0];
startTx = touches.clientX;
startTy = touches.clientY;
isTouchMove = false;
};
const touchmoveFun = () => {
isTouchMove = true;
// e.preventDefault();
};
const touchendFun = (e: any) => {
if (!isTouchMove) {
return;
}
const touches = e.changedTouches[0];
const endTx = touches.clientX;
const endTy = touches.clientY;
const distanceX = startTx - endTx;
const distanceY = startTy - endTy;
// eslint-disable-next-line @typescript-eslint/no-unused-vars
let isSwipe = false;
if (Math.abs(distanceX) < Math.abs(distanceY)) {
if (distanceY > 20) {
fn();
isSwipe = true;
// element.removeEventListener("touchstart", touchstartFun, false);
// element.removeEventListener("touchmove", touchmoveFun, false);
// element.removeEventListener("touchend", touchendFun, false);
}
}
};
element.addEventListener("touchstart", touchstartFun, false);
element.addEventListener("touchmove", touchmoveFun, false);
element.addEventListener("touchend", touchendFun, false);
},
/* 向下滑动事件 */
swipeDown(element: any, fn: Function) {
let isTouchMove: boolean;
let startTx: number;
let startTy: number;
const touchstartFun = (e: any) => {
const touches = e.touches[0];
startTx = touches.clientX;
startTy = touches.clientY;
isTouchMove = false;
};
const touchmoveFun = () => {
isTouchMove = true;
// e.preventDefault();
};
const touchendFun = (e: any) => {
if (!isTouchMove) {
return;
}
const touches = e.changedTouches[0];
const endTx = touches.clientX;
const endTy = touches.clientY;
const distanceX = startTx - endTx;
const distanceY = startTy - endTy;
// eslint-disable-next-line @typescript-eslint/no-unused-vars
let isSwipe = false;
if (Math.abs(distanceX) < Math.abs(distanceY)) {
if (distanceY < -20) {
fn();
isSwipe = true;
// element.removeEventListener("touchstart", touchstartFun, false);
// element.removeEventListener("touchmove", touchmoveFun, false);
// element.removeEventListener("touchend", touchendFun, false);
}
}
};
element.addEventListener("touchstart", touchstartFun, false);
element.addEventListener("touchmove", touchmoveFun, false);
element.addEventListener("touchend", touchendFun, false);
},
};