'a'
mh-two-thousand-and-two
2024-04-12 44d2c92345cd156a59fc327b3060292a282d2893
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
export default {
  data () {
    return {
      hovering: false
    }
  },
 
  props: {
    hoverClass: {
      type: String,
      default: 'none'
    },
    hoverStopPropagation: {
      type: Boolean,
      default: false
    },
    hoverStartTime: {
      type: [Number, String],
      default: 50
    },
    hoverStayTime: {
      type: [Number, String],
      default: 400
    }
  },
 
  methods: {
    _hoverTouchStart (evt) {
      if (evt.touches.length > 1) {
        return
      }
      this._handleHoverStart(evt)
    },
 
    _hoverMousedown (evt) {
      if (this._hoverTouch) {
        return
      }
 
      this._handleHoverStart(evt)
      window.addEventListener('mouseup', this._hoverMouseup)
    },
 
    _handleHoverStart (evt) {
      // TODO detect scrolling
      if (evt._hoverPropagationStopped) {
        return
      }
      if (!this.hoverClass || this.hoverClass === 'none' || this.disabled) {
        return
      }
      if (this.hoverStopPropagation) {
        evt._hoverPropagationStopped = true
      }
      this._hoverTouch = true
      this._hoverStartTimer = setTimeout(() => {
        this.hovering = true
        if (!this._hoverTouch) { // 防止在hoverStartTime时间内触发了 touchend 或 touchcancel
          this._hoverReset()
        }
      }, this.hoverStartTime)
    },
 
    _hoverMouseup () {
      if (!this._hoverTouch) {
        return
      }
 
      this._handleHoverEnd()
      window.removeEventListener('mouseup', this._hoverMouseup)
    },
 
    _hoverTouchEnd () {
      this._handleHoverEnd()
    },
 
    _handleHoverEnd () {
      this._hoverTouch = false
      if (this.hovering) {
        this._hoverReset()
      }
    },
 
    _hoverReset () {
      requestAnimationFrame(() => {
        clearTimeout(this._hoverStayTimer)
        this._hoverStayTimer = setTimeout(() => {
          this.hovering = false
        }, this.hoverStayTime)
      })
    },
 
    _hoverTouchCancel () {
      this._hoverTouch = false
      this.hovering = false
      clearTimeout(this._hoverStartTimer)
    }
  }
}