27 lines
682 B
JavaScript
27 lines
682 B
JavaScript
// 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();
|
|
}
|
|
} |