video-swiper.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. Component({
  2. behaviors: [],
  3. options: {
  4. addGlobalClass: true,
  5. pureDataPattern: /^_/
  6. },
  7. properties: {
  8. duration: {
  9. type: Number,
  10. value: 500
  11. },
  12. easingFunction: {
  13. type: String,
  14. value: 'default'
  15. },
  16. loop: {
  17. type: Boolean,
  18. value: false,
  19. },
  20. videoList: {
  21. type: Array,
  22. value: [],
  23. observer: function observer(...args) {
  24. const newVal = args.length > 0 && args[0] !== undefined ? args[0] : []
  25. console.log(newVal)
  26. this._videoListChanged(newVal)
  27. }
  28. }
  29. },
  30. data: {
  31. nextQueue: [],
  32. prevQueue: [],
  33. curQueue: [],
  34. circular: false,
  35. _last: 1,
  36. _change: -1,
  37. _invalidUp: 0,
  38. _invalidDown: 0,
  39. _videoContexts: []
  40. },
  41. lifetimes: {
  42. attached() {
  43. this.data._videoContexts = [
  44. wx.createVideoContext('video_0', this), wx.createVideoContext('video_1', this), wx.createVideoContext('video_2', this)]
  45. },
  46. },
  47. methods: {
  48. _videoListChanged(newVal) {
  49. const _this = this
  50. const data = this.data
  51. newVal.forEach(function (item) {
  52. data.nextQueue.push(item)
  53. })
  54. if (data.curQueue.length === 0) {
  55. this.setData({
  56. curQueue: data.nextQueue.splice(0, 3)
  57. }, function () {
  58. _this.playCurrent(0)
  59. })
  60. }
  61. },
  62. animationfinish(e) {
  63. const _data = this.data
  64. const _last = _data._last
  65. const _change = _data._change
  66. const curQueue = _data.curQueue
  67. const prevQueue = _data.prevQueue
  68. const nextQueue = _data.nextQueue
  69. const current = e.detail.current
  70. const diff = current - _last
  71. if (diff === 0) return
  72. this.data._last = current
  73. this.playCurrent(current)
  74. this.triggerEvent('change', {activeId: curQueue[current].id})
  75. const direction = diff === 1 || diff === -2 ? 'up' : 'down'
  76. if (direction === 'up') {
  77. console.log(this.data)
  78. if (this.data._invalidDown === 0) {
  79. const change = (_change + 1) % 3
  80. const add = nextQueue.shift()
  81. const remove = curQueue[change]
  82. if (add) {
  83. prevQueue.push(remove)
  84. curQueue[change] = add
  85. this.data._change = change
  86. } else {
  87. this.data._invalidUp += 1
  88. }
  89. } else {
  90. this.data._invalidDown -= 1
  91. }
  92. }
  93. if (direction === 'down') {
  94. if (this.data._invalidUp === 0) {
  95. const _change2 = _change
  96. const _remove = curQueue[_change2]
  97. const _add = prevQueue.pop()
  98. if (_add) {
  99. curQueue[_change2] = _add
  100. nextQueue.unshift(_remove)
  101. this.data._change = (_change2 - 1 + 3) % 3
  102. } else {
  103. this.data._invalidDown += 1
  104. }
  105. } else {
  106. this.data._invalidUp -= 1
  107. }
  108. }
  109. let circular = true
  110. if (nextQueue.length === 0 && current !== 0) {
  111. circular = false
  112. }
  113. if (prevQueue.length === 0 && current !== 2) {
  114. circular = false
  115. }
  116. this.setData({
  117. curQueue,
  118. circular
  119. })
  120. },
  121. playCurrent(current) {
  122. this.data._videoContexts.forEach(function (ctx, index) {
  123. if (index !== current) {
  124. ctx.pause()
  125. } else {
  126. ctx.play()
  127. }
  128. })
  129. },
  130. onPlay: function onPlay(e) {
  131. this.trigger(e, 'play')
  132. },
  133. onPause: function onPause(e) {
  134. this.trigger(e, 'pause')
  135. },
  136. onEnded: function onEnded(e) {
  137. this.trigger(e, 'ended')
  138. },
  139. onError: function onError(e) {
  140. this.trigger(e, 'error')
  141. },
  142. onTimeUpdate: function onTimeUpdate(e) {
  143. this.trigger(e, 'timeupdate')
  144. },
  145. onWaiting: function onWaiting(e) {
  146. this.trigger(e, 'wait')
  147. },
  148. onProgress: function onProgress(e) {
  149. this.trigger(e, 'progress')
  150. },
  151. onLoadedMetaData: function onLoadedMetaData(e) {
  152. this.trigger(e, 'loadedmetadata')
  153. },
  154. trigger: function trigger(e, type, ...args) {
  155. const ext = args.length > 2 && args[2] !== undefined ? args[2] : {}
  156. const detail = e.detail
  157. const activeId = e.target.dataset.id
  158. this.triggerEvent(
  159. type, Object.assign(Object.assign(Object.assign({}, detail), {activeId}), ext)
  160. )
  161. }
  162. },
  163. })