2025-10-22 01:50:49 +08:00
|
|
|
export function touchTapListener(fn, options = {}) {
|
|
|
|
|
const config = {
|
|
|
|
|
taps: 2,
|
|
|
|
|
fingers: 1,
|
|
|
|
|
delay: 300,
|
|
|
|
|
...options,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let maxTouches = 0;
|
|
|
|
|
let tapCount = 0;
|
|
|
|
|
let tapTimer = null;
|
|
|
|
|
|
|
|
|
|
const handleTouchStart = (e) => {
|
|
|
|
|
maxTouches = Math.max(maxTouches, e.touches.length);
|
|
|
|
|
};
|
|
|
|
|
|
2023-11-10 18:00:34 +08:00
|
|
|
const handleTouchend = (e) => {
|
2025-10-22 01:50:49 +08:00
|
|
|
if (e.touches.length === 0) {
|
|
|
|
|
if (maxTouches === config.fingers) {
|
|
|
|
|
tapCount++;
|
|
|
|
|
clearTimeout(tapTimer);
|
|
|
|
|
|
|
|
|
|
if (tapCount === config.taps) {
|
|
|
|
|
fn(e);
|
|
|
|
|
tapCount = 0;
|
|
|
|
|
} else {
|
|
|
|
|
tapTimer = setTimeout(() => {
|
|
|
|
|
tapCount = 0;
|
|
|
|
|
}, config.delay);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
tapCount = 0;
|
|
|
|
|
clearTimeout(tapTimer);
|
|
|
|
|
}
|
|
|
|
|
maxTouches = 0;
|
2023-11-10 18:00:34 +08:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2025-10-22 01:50:49 +08:00
|
|
|
document.addEventListener("touchstart", handleTouchStart, { passive: true });
|
|
|
|
|
document.addEventListener("touchend", handleTouchend, { passive: true });
|
|
|
|
|
|
2023-11-10 18:00:34 +08:00
|
|
|
return () => {
|
2025-10-22 01:50:49 +08:00
|
|
|
clearTimeout(tapTimer);
|
|
|
|
|
document.removeEventListener("touchstart", handleTouchStart);
|
|
|
|
|
document.removeEventListener("touchend", handleTouchend);
|
2023-11-10 18:00:34 +08:00
|
|
|
};
|
|
|
|
|
}
|