side-wxs.wxs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /**
  2. * 监听页面内值的变化,主要用于动态开关swipe-action
  3. * @param {Object} newValue
  4. * @param {Object} oldValue
  5. * @param {Object} ownerInstance
  6. * @param {Object} instance
  7. */
  8. function sizeReady(newValue, oldValue, ownerInstance, instance) {
  9. var state = instance.getState()
  10. state.position = JSON.parse(newValue)
  11. if (!state.position || state.position.length === 0) return
  12. var show = state.position[0].show
  13. var onceChange = state.position[0].once_change;
  14. state.left = state.left || state.position[0].left;
  15. //初始化 bMove
  16. state.bMove = false;
  17. // 通过用户变量,开启或关闭
  18. // console.log("sizeReady",onceChange,show,state);
  19. if(state.position[0].bInit){
  20. var x = -state.position[0].width;
  21. var test = ownerInstance.selectComponent('#my-side-dialog');
  22. test.setStyle({
  23. transform: 'translateX(' + x + 'px)',
  24. '-webkit-transform': 'translateX(' + x + 'px)'
  25. })
  26. state.left = x;
  27. // #my-side-dialog 设置 -500px ,这里要重置为0
  28. state.position[0].left = 0;
  29. state.position[0].bInit = false;
  30. }
  31. if(!onceChange)return;
  32. if (show) {
  33. openState(false, instance, ownerInstance)
  34. } else {
  35. openState(true, instance, ownerInstance)
  36. }
  37. }
  38. /**
  39. * 开始触摸操作
  40. * @param {Object} e
  41. * @param {Object} ins
  42. */
  43. function touchstart(e, ins) {
  44. var instance = e.instance;
  45. var state = instance.getState();
  46. var pageX = e.touches[0].pageX;
  47. //触发一次
  48. if(state.bMove)return;
  49. state.bMove = true;
  50. state.left = state.left || state.position[0].left;
  51. // console.log(state);
  52. // 获取最终按钮组的宽度
  53. state.width = pageX - state.left;
  54. // console.log("width:",pageX,state.left,state.position[0].left);
  55. ins.callMethod('openSwipe')
  56. var test = instance.selectComponent('#my-side-dialog');
  57. test.removeClass('ani');
  58. }
  59. /**
  60. * 开始滑动操作
  61. * @param {Object} e
  62. * @param {Object} ownerInstance
  63. */
  64. function touchmove(e, ownerInstance) {
  65. var instance = e.instance;
  66. var state = instance.getState()
  67. var pageX = e.touches[0].pageX;
  68. if(!state.bMove)return;
  69. move(pageX - state.width, instance, ownerInstance)
  70. }
  71. /**
  72. * 结束触摸操作
  73. * @param {Object} e
  74. * @param {Object} ownerInstance
  75. */
  76. function touchend(e, ownerInstance) {
  77. var instance = e.instance;
  78. var state = instance.getState();
  79. if(!state.bMove)return;
  80. state.bMove = false;
  81. // 滑动过程中触摸结束,通过阙值判断是开启还是关闭
  82. moveDirection(state.left, -200, instance, ownerInstance);
  83. }
  84. /**-
  85. * 设置移动距离
  86. * @param {Object} value
  87. * @param {Object} instance
  88. * @param {Object} ownerInstance
  89. */
  90. function move(value, instance, ownerInstance) {
  91. var state = instance.getState()
  92. // 获取可滑动范围
  93. var x = Math.max(-state.position[0].width, Math.min((value), 0));
  94. // console.log(x);
  95. state.left = x;
  96. var test = ownerInstance.selectComponent('#my-side-dialog');
  97. test.setStyle({
  98. transform: 'translateX(' + x + 'px)',
  99. '-webkit-transform': 'translateX(' + x + 'px)'
  100. })
  101. }
  102. /**
  103. * 移动方向判断
  104. * @param {Object} left
  105. * @param {Object} value
  106. * @param {Object} ownerInstance
  107. * @param {Object} ins
  108. */
  109. function moveDirection(left, value, ins, ownerInstance) {
  110. var state = ins.getState()
  111. var position = state.position
  112. var isclose = state.isclose
  113. // 如果是关闭状态,进行判断是否打开,还是保留关闭状态
  114. if (isclose) {
  115. if (left >= value) {
  116. openState(false, ins, ownerInstance)
  117. } else {
  118. openState(true, ins, ownerInstance)
  119. }
  120. return
  121. }
  122. // 如果已经是打开状态,进行判断是否关闭,还是保留打开状态
  123. if (left <= value) {
  124. openState(true, ins, ownerInstance)
  125. } else {
  126. openState(false, ins, ownerInstance)
  127. }
  128. }
  129. /**
  130. * 开启状态
  131. * @param {Boolean} type
  132. * @param {Object} ins
  133. * @param {Object} ownerInstance
  134. */
  135. function openState(type, ins, ownerInstance) {
  136. var state = ins.getState()
  137. var position = state.position
  138. // 设置打开和移动状态
  139. state.isclose = type
  140. // console.log("openState",type);
  141. // 通知页面,已经打开
  142. ownerInstance.callMethod('change', {
  143. close: type
  144. })
  145. // 添加动画类
  146. var test = ownerInstance.selectComponent('#my-side-dialog');
  147. test.addClass('ani');
  148. // 设置最终移动位置
  149. move(type ? -position[0].width : 0, ins, ownerInstance)
  150. }
  151. module.exports = {
  152. sizeReady: sizeReady,
  153. touchstart: touchstart,
  154. touchmove: touchmove,
  155. touchend: touchend
  156. }