touch.js 1.1KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. // @ts-nocheck
  2. const MIN_DISTANCE = 10;
  3. function getDirection(x, y) {
  4. if (x > y && x > MIN_DISTANCE) {
  5. return 'horizontal';
  6. }
  7. if (y > x && y > MIN_DISTANCE) {
  8. return 'vertical';
  9. }
  10. return '';
  11. }
  12. export const touch = Behavior({
  13. methods: {
  14. resetTouchStatus() {
  15. this.direction = '';
  16. this.deltaX = 0;
  17. this.deltaY = 0;
  18. this.offsetX = 0;
  19. this.offsetY = 0;
  20. },
  21. touchStart(event) {
  22. this.resetTouchStatus();
  23. const touch = event.touches[0];
  24. this.startX = touch.clientX;
  25. this.startY = touch.clientY;
  26. },
  27. touchMove(event) {
  28. const touch = event.touches[0];
  29. this.deltaX = touch.clientX - this.startX;
  30. this.deltaY = touch.clientY - this.startY;
  31. this.offsetX = Math.abs(this.deltaX);
  32. this.offsetY = Math.abs(this.deltaY);
  33. this.direction =
  34. this.direction || getDirection(this.offsetX, this.offsetY);
  35. },
  36. },
  37. });