Skip to content

Instantly share code, notes, and snippets.

@h43z
Last active April 15, 2026 22:03
Show Gist options
  • Select an option

  • Save h43z/c3811d31de115a84741d2cbd86073298 to your computer and use it in GitHub Desktop.

Select an option

Save h43z/c3811d31de115a84741d2cbd86073298 to your computer and use it in GitHub Desktop.
(() => {
let swipeStartX, swipeStartY, fingers
addEventListener('touchstart', event => {
if(isInput(event.target))
return
fingers = event.touches.length
swipeStartX = event.changedTouches[0].clientX
swipeStartY = event.changedTouches[0].clientY
})
addEventListener('touchend', event => {
if(isInput(event.target))
return
if(isScrollable(event.target))
return
// skip if multiple fingers are touching the screen
if(fingers > 1) return
const movedX = event.changedTouches[0].clientX - swipeStartX
const movedY = event.changedTouches[0].clientY - swipeStartY
// skip if swipe was not very horizontal
if(Math.abs(movedY) > 55) return
// swipe at least 50px but under 400px
if(movedX < -50 && movedX > -400){
// swipe from right to leftc
history.back()
}else if(movedX > 50 && movedX < 400){
// swipe from left to right
history.forward()
}
})
const isInput = e => {
return e.nodeName === 'SELECT' ||
e.nodeName === 'INPUT' ||
e.nodeName === 'TEXTAREA' ||
e.getAttribute("contenteditable") === "true"
}
const isScrollable = e => {
const hasHorizontalScroll = e.scrollWidth > e.clientWidth
return hasHorizontalScroll;
}
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment