Skip to content

Commit ab6afad

Browse files
committed
feat: Swipe Detector
1 parent 7989a94 commit ab6afad

File tree

4 files changed

+94
-1
lines changed

4 files changed

+94
-1
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,4 @@ typings/
5757
# dotenv environment variables file
5858
.env
5959

60+
*.iml

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
# swipe-detector
2-
Javascript Swipe gesture detector.
2+
Javascript Swipe gesture detector. By Daniel & Lucas.

package.json

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"name": "swipe-detector",
3+
"version": "0.1.0",
4+
"description": "avascript Swipe gesture detector.",
5+
"main": "src/swipe-detector.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"repository": {
10+
"type": "git",
11+
"url": "git+https://github.com/wnr/swipe-detector.git"
12+
},
13+
"keywords": [
14+
"swipe",
15+
"event",
16+
"javascript"
17+
],
18+
"author": "Daniel Kihlgren & Lucas Wiener",
19+
"license": "MIT",
20+
"bugs": {
21+
"url": "https://github.com/wnr/swipe-detector/issues"
22+
},
23+
"homepage": "https://github.com/wnr/swipe-detector#readme"
24+
}

src/swipe-detector.js

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
export default function SwipeDetector({
2+
element,
3+
minVelocity = 0.3,
4+
maxDistanceY = 20,
5+
minDistanceX = 10
6+
}) {
7+
var startX;
8+
var startY;
9+
var startTime;
10+
var swipeLeftListeners = [];
11+
var swipeRightListeners = [];
12+
13+
function triggerSwipeRight() {
14+
swipeRightListeners.forEach(function (listener) {
15+
listener();
16+
});
17+
}
18+
19+
function triggerSwipeLeft() {
20+
swipeLeftListeners.forEach(function (listener) {
21+
listener();
22+
});
23+
}
24+
25+
function handleTouchStart (e) {
26+
startX = e.changedTouches[0].pageX;
27+
startY = e.changedTouches[0].pageY;
28+
startTime = new Date().getTime();
29+
}
30+
31+
function handleTouchEnd(e) {
32+
const endX = e.changedTouches[0].pageX;
33+
const endY = e.changedTouches[0].pageY;
34+
const endTime = new Date().getTime();
35+
36+
const elapsedDistanceX = endX - startX;
37+
const elapsedDistanceY = endY - startY;
38+
const elapsedTime = endTime - startTime;
39+
40+
const velocityX = Math.abs(elapsedDistanceX) / elapsedTime; // px/ms
41+
42+
if (Math.abs(elapsedDistanceX) >= minDistanceX && velocityX >= minVelocity && Math.abs(elapsedDistanceY) <= maxDistanceY) {
43+
if (elapsedDistanceX > 0) {
44+
triggerSwipeRight();
45+
} else {
46+
triggerSwipeLeft();
47+
}
48+
}
49+
}
50+
51+
element.addEventListener('touchend', handleTouchEnd);
52+
element.addEventListener('touchstart', handleTouchStart);
53+
54+
return {
55+
onLeftSwipe: function (callback) {
56+
swipeLeftListeners.push(callback);
57+
},
58+
onRightSwipe: function (callback) {
59+
swipeRightListeners.push(callback);
60+
},
61+
remove: function () {
62+
element.removeEventListener('touchstart', handleTouchStart);
63+
element.removeEventListener('touchend', handleTouchEnd);
64+
swipeLeftListeners = [];
65+
swipeRightListeners = [];
66+
}
67+
};
68+
}

0 commit comments

Comments
 (0)