swipescreen.wxml 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <view class="ss-showcase" bindtouchstart="{{swipe.onStartDrag}}" bindtouchmove="{{swipe.onDrag}}" bindtouchend="{{swipe.onEndDrag}}" displayWidth="{{displayWidth}}" change:displayWidth="{{swipe.initWidth}}" style="height: {{displayHeight}};width: {{displayWidth}}px;">
  2. <view id="wrapper" class="ss-showcase__wrap">
  3. <slot />
  4. </view>
  5. <view class="ss-showcase__absolute">
  6. <view class="dotsbar" wx:if="{{displayDots}}">
  7. <view wx:for="{{lens}}" wx:key="index" class="dot{{current === item ? ' dot-active': ''}}" />
  8. </view>
  9. <slot name="pointer" />
  10. </view>
  11. </view>
  12. <wxs module="swipe">
  13. var THRESHOLD = 0.3
  14. var MIN_DISTANCE = 10
  15. var owner
  16. var state
  17. var getState = function (ownerInstance) {
  18. owner = ownerInstance
  19. state = owner.getState()
  20. }
  21. var initWidth = function (newVal, oldVal, ownerInstance) {
  22. getState(ownerInstance)
  23. state.screen = newVal
  24. }
  25. var _restTouchStatus = function () {
  26. state.direction = undefined
  27. state.deltaX = 0
  28. state.deltaY = 0
  29. state.offsetX = 0
  30. state.offsetY = 0
  31. }
  32. var _getDirection = function (x, y) {
  33. if (x > y && x > MIN_DISTANCE) {
  34. return 'horizontal'
  35. }
  36. if (y > x && y > MIN_DISTANCE) {
  37. return 'vertical'
  38. }
  39. return undefined
  40. }
  41. var _range = function (num, min, max) {
  42. return Math.min(Math.max(num, min), max)
  43. }
  44. var _throttle = function (fn, gapTime) {
  45. if (gapTime == null || gapTime == undefined) {
  46. gapTime = 1500
  47. }
  48. var _lastTime = null
  49. // 返回新的函数
  50. return function () {
  51. var _nowTime = getDate()
  52. if (_nowTime - _lastTime > gapTime || !_lastTime) {
  53. fn(arguments[0]) //将this和参数传给原函数
  54. _lastTime = _nowTime
  55. }
  56. }
  57. }
  58. var _swipeMove = function (_offset = 0, sec = 0.6) {
  59. state.offset = _range(
  60. _offset,
  61. state.screen - state.wrapx,
  62. 0,
  63. )
  64. var transform = 'translate3d(' + state.offset + 'px, 0, 0)'
  65. var transition = state.dragging
  66. ? 'none'
  67. : ('transform ' + sec + 's cubic-bezier(0.18, 0.89, 0.32, 1)')
  68. owner.selectComponent('#wrapper').setStyle({
  69. '-webkit-transform': transform,
  70. '-webkit-transition': transition,
  71. 'transform': transform,
  72. 'transition': transition
  73. })
  74. }
  75. var onStartDrag = function (event, ownerInstance) {
  76. getState(ownerInstance)
  77. _restTouchStatus()
  78. var wrapper = owner.selectComponent('#wrapper')
  79. var rect = wrapper.getBoundingClientRect()
  80. // state.rect = rect
  81. state.wrapx = rect.width
  82. state.startOffset = state.offset || 0
  83. var touchPoint = event.touches[0]
  84. state.startX = touchPoint.clientX
  85. state.startY = touchPoint.clientY
  86. }
  87. var onDrag = function (event, ownerInstance) {
  88. getState(ownerInstance)
  89. var touchPoint = event.touches[0]
  90. state.deltaX = touchPoint.clientX - state.startX
  91. state.deltaY = touchPoint.clientY - state.startY
  92. state.offsetX = Math.abs(state.deltaX)
  93. state.offsetY = Math.abs(state.deltaY)
  94. state.direction = state.direction || _getDirection(state.offsetX, state.offsetY)
  95. if (state.direction !== 'horizontal') {
  96. return
  97. }
  98. state.isDragging = true
  99. _swipeMove(state.startOffset + state.deltaX, 0.2)
  100. }
  101. var onEndDrag = function (event, ownerInstance) {
  102. getState(ownerInstance)
  103. state.isDragging = false
  104. var SCREEN = state.screen
  105. var therwidth = SCREEN * THRESHOLD
  106. var idx = 0
  107. if (Math.abs(state.deltaX) >= therwidth) {
  108. if (state.deltaX >= 0) {
  109. idx = Math.floor(state.offset / SCREEN) + 1
  110. } else if (state.deltaX < 0) {
  111. idx = Math.floor(state.offset / SCREEN)
  112. }
  113. } else {
  114. if (state.deltaX >= 0) {
  115. idx = Math.floor(state.offset / SCREEN)
  116. } else if (state.deltaX < 0) {
  117. idx = Math.floor(state.offset / SCREEN) + 1
  118. }
  119. }
  120. _swipeMove(idx * SCREEN)
  121. ownerInstance.callMethod("changedIndex", { idx: Math.abs(idx) })
  122. //console.log(JSON.stringify(ownerInstance), ownerInstance.triggerEvent)
  123. //ownerInstance("changedIndex", { idx })
  124. }
  125. module.exports = {
  126. onDrag: onDrag,
  127. onStartDrag: onStartDrag,
  128. onEndDrag: onEndDrag,
  129. initWidth: initWidth
  130. }
  131. </wxs>