From 96358bf61ab8ba3aae9a2f945ffa40cabff94d41 Mon Sep 17 00:00:00 2001 From: Florian Knoch Date: Tue, 20 Sep 2022 16:40:16 +0200 Subject: [PATCH] Add swipe support --- touch-support.js | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 touch-support.js diff --git a/touch-support.js b/touch-support.js new file mode 100644 index 0000000..ec6ead4 --- /dev/null +++ b/touch-support.js @@ -0,0 +1,27 @@ +// Reference: https://www.delftstack.com/howto/javascript/detect-finger-swipe-events-in-javascript/ + +window.addEventListener('load', () => { + document.body.addEventListener('touchstart', onTouchStart); + document.body.addEventListener('touchmove', onTouchMove); + document.body.addEventListener('touchend', onTouchEnd); +}) + +var startX, startY, moveX, moveY; + +function onTouchStart(e){ + startX = e.touches[0].clientX; + startY = e.touches[0].clientY; +} + +function onTouchMove(e){ + moveX = e.touches[0].clientX; + moveY = e.touches[0].clientY; +} + +function onTouchEnd(){ + if (startY+100 < moveY){ + goToPreviousSlide(); + } else if (startY-100 > moveY){ + goToNextSlide(); + } +} \ No newline at end of file