touch.js 918 B

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