touch operation

This commit is contained in:
Gabe Yuan
2023-11-11 16:59:38 +08:00
parent 46428b7c7f
commit 4b23ee733f
11 changed files with 58 additions and 168 deletions

View File

@@ -1,31 +1,12 @@
export function touchTapListener(fn, setting) {
const [touchLength, touchCount, touchTime] = setting;
let lastTouch = 0;
let curCount = 0;
export function touchTapListener(fn, touchsLength) {
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;
if (e.touches.length === touchsLength) {
fn();
}
lastTouch = now;
};
document.addEventListener("touchend", handleTouchend);
document.addEventListener("touchstart", handleTouchend);
return () => {
document.removeEventListener("touchend", handleTouchend);
document.removeEventListener("touchstart", handleTouchend);
};
}