A simple swipe detection on vanilla js

Yevgeny Petukhov
2016-10-02T13:24:36Z

swipe.js

| var touchstartX = 0;
| var touchstartY = 0;
| var touchendX = 0;
| var touchendY = 0;
|
| var gesuredZone = document.getElementById('gesuredZone');
|
| gesuredZone.addEventListener('touchstart', function(event) {
| touchstartX = event.screenX;
| touchstartY = event.screenY;
| }, false);
|
| gesuredZone.addEventListener('touchend', function(event) {
| touchendX = event.screenX;
| touchendY = event.screenY;
| handleGesure();
| }, false);
|
| function handleGesure() {
| var swiped = 'swiped: ';
| if (touchendX < touchstartX) {
| alert(swiped + 'left!');
| }
| if (touchendX > touchstartX) {
| alert(swiped + 'right!');
| }
| if (touchendY < touchstartY) {
| alert(swiped + 'down!');
| }
| if (touchendY > touchstartY) {
| alert(swiped + 'left!');
| }
| if (touchendY == touchstartY) {
| alert('tap!');
| }
| }