珠宝小程序前端代码
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.

143 lines
3.1 KiB

3 months ago
  1. var MIN_DISTANCE = 10;
  2. function changeData(newValue, oldValue, ownerInstance, instance) {
  3. var state = instance.getState();
  4. if (newValue.left != undefined) {
  5. state.left = newValue.left
  6. }
  7. if (newValue.bounce != undefined) {
  8. state.bounce = newValue.bounce
  9. }
  10. // console.log('changeData', JSON.stringify(newValue))
  11. }
  12. /**
  13. * 开始触摸操作
  14. * @param {Object} e
  15. * @param {Object} ins
  16. */
  17. function touchstart(e, ins) {
  18. var instance = e.instance;
  19. // 计算滑动开始位置
  20. stopTouchStart(e, ins)
  21. }
  22. /**
  23. * 开始滑动操作
  24. * @param {Object} e
  25. * @param {Object} ownerInstance
  26. */
  27. function touchmove(e, ownerInstance) {
  28. var instance = e.instance;
  29. // 是否可以滑动页面
  30. stopTouchMove(e);
  31. if (e.preventDefault) {
  32. // 阻止页面滚动
  33. e.preventDefault()
  34. }
  35. // var state = instance.getState();
  36. // && state.bounce
  37. move(instance, ownerInstance)
  38. return false
  39. }
  40. /**
  41. * 结束触摸操作
  42. * @param {Object} e
  43. * @param {Object} ownerInstance
  44. */
  45. function touchend(e, ownerInstance) {
  46. var instance = e.instance;
  47. var state = instance.getState()
  48. ownerInstance.callMethod('moveEnd', {
  49. velocity: Math.abs(state.deltaX / state.timing),
  50. direction: state.direction,
  51. deltaX: state.deltaX,
  52. deltaY: state.deltaY
  53. })
  54. }
  55. /**
  56. * 设置移动距离
  57. * @param {Object} instance
  58. * @param {Object} ownerInstance
  59. */
  60. function move(instance, ownerInstance) {
  61. var state = instance.getState()
  62. var value = state.deltaX || 0
  63. var state = instance.getState()
  64. if (state.direction == 'horizontal') {
  65. // instance.requestAnimationFrame(function() {
  66. // instance.setStyle({
  67. // transform: 'translateX(' + value + 'px)',
  68. // '-webkit-transform': 'translateX(' + value + 'px)'
  69. // })
  70. // })
  71. ownerInstance.callMethod('moveTo', {
  72. deltaX: value,
  73. deltaY: state.deltaY || 0,
  74. left: state.left + value
  75. })
  76. }
  77. }
  78. /**
  79. * 滑动中,是否禁止打开
  80. * @param {Object} event
  81. */
  82. function stopTouchMove(event) {
  83. var instance = event.instance;
  84. var state = instance.getState();
  85. var touch = event.touches[0];
  86. state.timing = getDate().getTime() - state.startTime;
  87. state.deltaX = touch.clientX - state.startX;
  88. state.deltaY = touch.clientY - state.startY;
  89. state.offsetX = Math.abs(state.deltaX);
  90. state.offsetY = Math.abs(state.deltaY);
  91. state.direction = state.direction || getDirection(state.offsetX, state.offsetY);
  92. }
  93. /**
  94. * 设置滑动开始位置
  95. * @param {Object} event
  96. */
  97. function stopTouchStart(event) {
  98. var instance = event.instance;
  99. var state = instance.getState();
  100. resetTouchStatus(instance);
  101. var touch = event.touches[0];
  102. state.startTime = getDate().getTime();
  103. state.startX = touch.clientX;
  104. state.startY = touch.clientY;
  105. }
  106. function getDirection(x, y) {
  107. if (x > y && x > MIN_DISTANCE) {
  108. return 'horizontal';
  109. }
  110. if (y > x && y > MIN_DISTANCE) {
  111. return 'vertical';
  112. }
  113. return '';
  114. }
  115. /**
  116. * 重置滑动状态
  117. * @param {Object} event
  118. */
  119. function resetTouchStatus(instance) {
  120. var state = instance.getState();
  121. state.direction = '';
  122. state.deltaX = 0;
  123. state.deltaY = 0;
  124. state.offsetX = 0;
  125. state.offsetY = 0;
  126. }
  127. module.exports = {
  128. changeData: changeData,
  129. touchstart: touchstart,
  130. touchmove: touchmove,
  131. touchend: touchend
  132. }