NOTE: Greatly inspired by EscapeNetscape's answer, I've made an edit of his script using modern javascript in a comment. I made an answer of this due to user interest and a massive 4h jsfiddle.net downtime. I chose not to edit the original answer since it would change everything...
Here is a detectSwipe
function, working pretty well (used on one of my websites). I'd suggest you read it before you use it. Feel free to review it/edit the answer.
// usage example_x000D_
detectSwipe('swipeme', (el, dir) => alert(`you swiped on element with id ${el.id} to ${dir} direction`))_x000D_
_x000D_
// source code_x000D_
_x000D_
// Tune deltaMin according to your needs. Near 0 it will almost_x000D_
// always trigger, with a big value it can never trigger._x000D_
function detectSwipe(id, func, deltaMin = 90) {_x000D_
const swipe_det = {_x000D_
sX: 0,_x000D_
sY: 0,_x000D_
eX: 0,_x000D_
eY: 0_x000D_
}_x000D_
// Directions enumeration_x000D_
const directions = Object.freeze({_x000D_
UP: 'up',_x000D_
DOWN: 'down',_x000D_
RIGHT: 'right',_x000D_
LEFT: 'left'_x000D_
})_x000D_
let direction = null_x000D_
const el = document.getElementById(id)_x000D_
el.addEventListener('touchstart', function(e) {_x000D_
const t = e.touches[0]_x000D_
swipe_det.sX = t.screenX_x000D_
swipe_det.sY = t.screenY_x000D_
}, false)_x000D_
el.addEventListener('touchmove', function(e) {_x000D_
// Prevent default will stop user from scrolling, use with care_x000D_
// e.preventDefault();_x000D_
const t = e.touches[0]_x000D_
swipe_det.eX = t.screenX_x000D_
swipe_det.eY = t.screenY_x000D_
}, false)_x000D_
el.addEventListener('touchend', function(e) {_x000D_
const deltaX = swipe_det.eX - swipe_det.sX_x000D_
const deltaY = swipe_det.eY - swipe_det.sY_x000D_
// Min swipe distance, you could use absolute value rather_x000D_
// than square. It just felt better for personnal use_x000D_
if (deltaX ** 2 + deltaY ** 2 < deltaMin ** 2) return_x000D_
// horizontal_x000D_
if (deltaY === 0 || Math.abs(deltaX / deltaY) > 1)_x000D_
direction = deltaX > 0 ? directions.RIGHT : directions.LEFT_x000D_
else // vertical_x000D_
direction = deltaY > 0 ? directions.UP : directions.DOWN_x000D_
_x000D_
if (direction && typeof func === 'function') func(el, direction)_x000D_
_x000D_
direction = null_x000D_
}, false)_x000D_
}
_x000D_
#swipeme {_x000D_
width: 100%;_x000D_
height: 100%;_x000D_
background-color: orange;_x000D_
color: black;_x000D_
text-align: center;_x000D_
padding-top: 20%;_x000D_
padding-bottom: 20%;_x000D_
}
_x000D_
<div id='swipeme'>_x000D_
swipe me_x000D_
</div>
_x000D_