touch operation...

This commit is contained in:
Gabe Yuan
2023-11-10 18:00:34 +08:00
parent 6805340a9a
commit 46428b7c7f
7 changed files with 189 additions and 2 deletions

31
src/libs/touch.js Normal file
View File

@@ -0,0 +1,31 @@
export function touchTapListener(fn, setting) {
const [touchLength, touchCount, touchTime] = setting;
let lastTouch = 0;
let curCount = 0;
const handleTouchend = (e) => {
if (e.touches.length !== touchLength) {
return;
}
const timer = setTimeout(() => {
clearTimeout(timer);
curCount = 0;
}, touchTime);
curCount++;
const now = Date.now();
if (curCount === touchCount && now - lastTouch <= touchTime) {
timer && clearTimeout(timer);
curCount = 0;
fn();
}
lastTouch = now;
};
document.addEventListener("touchend", handleTouchend);
return () => {
document.removeEventListener("touchend", handleTouchend);
};
}