touch.js 1021 B

1234567891011121314151617181920212223242526272829303132333435
  1. const MinDistance = 10;
  2. const getDirection = (x, y) => {
  3. if (x > y && x > MinDistance) {
  4. return 'horizontal';
  5. }
  6. if (y > x && y > MinDistance) {
  7. return 'vertical';
  8. }
  9. return '';
  10. };
  11. export default 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;
  23. this.startX = touch.clientX;
  24. this.startY = touch.clientY;
  25. },
  26. touchMove(event) {
  27. const [touch] = event.touches;
  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 = getDirection(this.offsetX, this.offsetY);
  33. },
  34. },
  35. });