index.js 3.4 KB
Newer Older
Q
qiang 已提交
1
import { Scroller } from './Scroller'
fxy060608's avatar
fxy060608 已提交
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

export default {
  methods: {
    initScroller: function (element, options) {
      this._touchInfo = {
        trackingID: -1,
        maxDy: 0,
        maxDx: 0
      }
      this._scroller = new Scroller(element, options)
      this.__handleTouchStart = this._handleTouchStart.bind(this)
      this.__handleTouchMove = this._handleTouchMove.bind(this)
      this.__handleTouchEnd = this._handleTouchEnd.bind(this)
      this._initedScroller = true
    },
    _findDelta: function (event) {
      var touchInfo = this._touchInfo
      return event.detail.state === 'move' || event.detail.state === 'end' ? {
        x: event.detail.dx,
        y: event.detail.dy
      } : {
        x: event.screenX - touchInfo.x,
        y: event.screenY - touchInfo.y
      }
    },
    _handleTouchStart: function (e) {
      var t = this._touchInfo
      var n = this._scroller
      if (n) {
        if (e.detail.state === 'start') {
          t.trackingID = 'touch'
          t.x = e.detail.x
          t.y = e.detail.y
        } else {
          t.trackingID = 'mouse'
          t.x = e.screenX
          t.y = e.screenY
        }
        t.maxDx = 0
        t.maxDy = 0
        t.historyX = [0]
        t.historyY = [0]
        t.historyTime = [e.detail.timeStamp]
        t.listener = n
        if (n.onTouchStart) {
          n.onTouchStart()
        }
49
        event.preventDefault()
fxy060608's avatar
fxy060608 已提交
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 100 101 102 103 104 105
      }
    },
    _handleTouchMove: function (event) {
      var touchInfo = this._touchInfo
      if (touchInfo.trackingID !== -1) {
        event.preventDefault()
        var delta = this._findDelta(event)
        if (delta) {
          for (touchInfo.maxDy = Math.max(touchInfo.maxDy, Math.abs(delta.y)), touchInfo.maxDx = Math.max(touchInfo.maxDx, Math.abs(delta.x)), touchInfo.historyX.push(delta.x), touchInfo.historyY.push(delta.y), touchInfo.historyTime.push(event.detail.timeStamp); touchInfo.historyTime.length > 10;) {
            touchInfo.historyTime.shift()
            touchInfo.historyX.shift()
            touchInfo.historyY.shift()
          }
          if (touchInfo.listener && touchInfo.listener.onTouchMove) {
            touchInfo.listener.onTouchMove(delta.x, delta.y, event.detail.timeStamp)
          }
        }
      }
    },
    _handleTouchEnd: function (event) {
      var touchInfo = this._touchInfo
      if (touchInfo.trackingID !== -1) {
        event.preventDefault()
        var delta = this._findDelta(event)
        if (delta) {
          var listener = touchInfo.listener
          touchInfo.trackingID = -1
          touchInfo.listener = null
          var r = touchInfo.historyTime.length
          var o = {
            x: 0,
            y: 0
          }
          if (r > 2) {
            for (var a = touchInfo.historyTime.length - 1, s = touchInfo.historyTime[a], l = touchInfo.historyX[a], c = touchInfo.historyY[a]; a > 0;) {
              a--
              var u = touchInfo.historyTime[a]
              var d = s - u
              if (d > 30 && d < 50) {
                o.x = (l - touchInfo.historyX[a]) / (d / 1e3)
                o.y = (c - touchInfo.historyY[a]) / (d / 1e3)
                break
              }
            }
          }
          touchInfo.historyTime = []
          touchInfo.historyX = []
          touchInfo.historyY = []
          if (listener && listener.onTouchEnd) {
            listener.onTouchEnd(delta.x, delta.y, o)
          }
        }
      }
    }
  }
}