touch.js 1.0 KB

1234567891011121314151617181920212223242526272829303132333435
  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 = {
  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 = this.direction || getDirection(this.offsetX, this.offsetY);
  33. }
  34. }
  35. };