feat: more touch operations

This commit is contained in:
Gabe
2025-10-22 01:50:49 +08:00
parent 53e32d3031
commit 60b9653fd3
15 changed files with 730 additions and 550 deletions

View File

@@ -1,12 +1,47 @@
export function touchTapListener(fn, touchsLength) {
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);
};
const handleTouchend = (e) => {
if (e.touches.length === touchsLength) {
fn();
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;
}
};
document.addEventListener("touchstart", handleTouchend);
document.addEventListener("touchstart", handleTouchStart, { passive: true });
document.addEventListener("touchend", handleTouchend, { passive: true });
return () => {
document.removeEventListener("touchstart", handleTouchend);
clearTimeout(tapTimer);
document.removeEventListener("touchstart", handleTouchStart);
document.removeEventListener("touchend", handleTouchend);
};
}