珠宝小程序前端代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

91 lines
2.5 KiB

3 months ago
  1. const BindingX = uni.requireNativePlugin('bindingx');
  2. const animation = uni.requireNativePlugin('animation');
  3. export default {
  4. mounted() {
  5. this.container = this.getEl(this.$refs['container'])
  6. },
  7. methods: {
  8. _notifyTouchChangeForItems(isTouchEnd) {
  9. this.swiperViews && this.swiperViews.forEach(it => {
  10. it.canClick = isTouchEnd
  11. })
  12. },
  13. touchstart(e) {
  14. if (this.isAnimated) return;
  15. if (this.stop) return
  16. this.isScrolling = true
  17. this._notifyTouchChangeForItems(false)
  18. this.stop = true
  19. this.startTime = new Date().getTime()
  20. const props = [{
  21. element: this.container,
  22. property: 'transform.translateX',
  23. expression: uni.upx2px(this.left) + '+x'
  24. }]
  25. if (this.is3D) {
  26. const deltaScale = `${this.scale}/${this.width}*x`
  27. const currentScale = `${this.scale}-abs(x)/${this.width}*1`
  28. const realScale = `min(${currentScale},${this.scale})`
  29. const currentCardExp = `${realScale} < 1 ? 1 : ${realScale}`;
  30. const leftCardExp = `${deltaScale} > 0 ? min(${this.scale},(1 + ${deltaScale})) : min(1,(1 + ${deltaScale}))`;
  31. const rightCardExp = `${deltaScale} > 0 ? min(1,(1 - ${deltaScale})) : min(${this.scale},(1 - ${deltaScale}))`;
  32. props.push({
  33. element: this.getSwipteItemEl(this.position),
  34. property: 'transform.scale',
  35. expression: currentCardExp
  36. })
  37. if (this.position - 1 > -1) {
  38. props.push({
  39. element: this.getSwipteItemEl(this.position - 1),
  40. property: 'transform.scale',
  41. expression: leftCardExp
  42. })
  43. }
  44. if (this.position + 1 > this._size) {
  45. props.push({
  46. element: this.getSwipteItemEl(this.position + 1),
  47. property: 'transform.scale',
  48. expression: rightCardExp
  49. })
  50. }
  51. }
  52. this.eventpan = BindingX.bind({
  53. anchor: this.container,
  54. eventType: 'pan',
  55. props
  56. }, (e) => {
  57. if (e.state === 'end' ||
  58. e.state === 'exit') {
  59. setTimeout(() => {
  60. this._notifyTouchChangeForItems(true)
  61. }, 10)
  62. const timing = new Date().getTime() - this.startTime
  63. const velocity = Math.abs(e.deltaX / timing)
  64. this.stop = false
  65. this.isScrolling = false
  66. if (this.eventpan && this.eventpan.token) {
  67. BindingX.unbind({
  68. token: this.eventpan.token,
  69. eventType: 'pan'
  70. })
  71. }
  72. this.moveEnd({
  73. velocity,
  74. deltaX: e.deltaX,
  75. deltaY: e.deltaY
  76. })
  77. }
  78. })
  79. },
  80. getSwipteItemEl(idx) {
  81. return this.swiperViews[idx].$el.ref
  82. },
  83. /**
  84. * 获取ref
  85. * @param {Object} el
  86. */
  87. getEl(el) {
  88. return el.ref
  89. }
  90. }
  91. }