Skip to content

Instantly share code, notes, and snippets.

@allenhwkim
Created July 20, 2021 22:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save allenhwkim/677b25df5d5265b623cbdef7491290a1 to your computer and use it in GitHub Desktop.
Save allenhwkim/677b25df5d5265b623cbdef7491290a1 to your computer and use it in GitHub Desktop.
Mobile TouchSwipe Class
export class XTouchSwipe {
constructor(el) {
this.touchStaX;
this.toucnendX; // touch position
this.touchSta;
this.touchEnd; // touch time
el = el || document.body;
if (el.getAttribute('x-swipe') === null) {
el.setAttribute('x-swipe', '');
el.addEventListener('touchstart', this.setTouchSta.bind(this));
el.addEventListener('mousedown', this.setTouchSta.bind(this));
el.addEventListener('touchend', this.setTouchEnd.bind(this));
el.addEventListener('mouseup', this.setTouchEnd.bind(this));
}
}
handleGesture() {
if (Math.abs(this.touchEndX - this.touchStaX) < 80) return; // min 80px distance
if ((this.touchEnd - this.touchSta) > 500) return; // max 500ms dragging
const direction = this.touchEndX < this.touchStaX ? 'left' : 'right';
const event = new CustomEvent('x-swipe', { bubbles: true, detail: direction });
document.body.dispatchEvent(event);
}
setTouchSta(e) {
this.touchStaX = e.type === 'touchstart' ? e.changedTouches[0].screenX : e.screenX;
this.touchSta = new Date().getTime();
}
setTouchEnd(e) {
this.touchEndX = e.type === 'touchend' ? e.changedTouches[0].screenX : e.screenX;
this.touchEnd = new Date().getTime();
this.handleGesture();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment