-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathonSwipeVertical.js
50 lines (39 loc) · 1.23 KB
/
onSwipeVertical.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import { onTouchStart } from "./onTouchStart.js";
import { onTouchMove } from "./onTouchMove.js";
import { onTouchEnd } from "./onTouchEnd.js";
export function onSwipeVertical(element, fn) {
let startY = -1;
let enabled = true;
function handleStart(event) {
startY = event.touches[0].clientY;
}
function handleEnd(event) {
enabled = true;
}
function handleMove(event) {
if (startY < 0) return;
let moveY = event.touches[0].clientY;
let deltaY = moveY - startY;
if (enabled) {
if (deltaY > 30) {
fn({ directionX: 0, directionY: -1 }); // top
enabled = false;
} else if (deltaY < -30) {
fn({ directionX: 0, directionY: 1 }); // bottom
enabled = false;
}
}
}
let offTouchMove = onTouchMove(element, handleMove, { passive: false });
let offTouchStart = onTouchStart(element, handleStart);
let offTouchEnd = onTouchEnd(element, handleEnd);
return () => {
offTouchStart();
offTouchStart = null;
offTouchMove();
offTouchMove = null;
offTouchEnd();
offTouchEnd = null;
startY = null;
};
}