Blame view

uni_modules/uview-plus/libs/mixin/touch.js 1.68 KB
a2702f6d   刘淇   巡查计划
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
  import { defineMixin } from '../vue'
  
  const MIN_DISTANCE = 10
  
  function getDirection(x, y) {
      if (x > y && x > MIN_DISTANCE) {
          return 'horizontal'
      }
      if (y > x && y > MIN_DISTANCE) {
          return 'vertical'
      }
      return ''
  }
  
  export const touchMixin = defineMixin({
      methods: {
          getTouchPoint(e) {
              if (!e) {
                  return {
                      x: 0,
                      y: 0
                  }
              } if (e.touches && e.touches[0]) {
                  return {
                      x: e.touches[0].pageX,
                      y: e.touches[0].pageY
                  }
              } if (e.changedTouches && e.changedTouches[0]) {
                  return {
                      x: e.changedTouches[0].pageX,
                      y: e.changedTouches[0].pageY
                  }
              }
              return {
                  x: e.clientX || 0,
                  y: e.clientY || 0
              }
          },
          resetTouchStatus() {
              this.direction = ''
              this.deltaX = 0
              this.deltaY = 0
              this.offsetX = 0
              this.offsetY = 0
          },
          touchStart(event) {
              this.resetTouchStatus()
              const touch = this.getTouchPoint(event)
              this.startX = touch.x
              this.startY = touch.y
          },
          touchMove(event) {
              const touch = this.getTouchPoint(event)
              this.deltaX = touch.x - this.startX
              this.deltaY = touch.y - this.startY
              this.offsetX = Math.abs(this.deltaX)
              this.offsetY = Math.abs(this.deltaY)
              this.direction =				this.direction || getDirection(this.offsetX, this.offsetY)
          }
      }
  })