index.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. Component({
  2. /**
  3. * 组件的属性列表
  4. */
  5. properties: {
  6. pid: {
  7. type: String,
  8. value: '',
  9. observer(newVal) {
  10. console.log("on pid:", newVal, typeof newVal);
  11. if (newVal) {
  12. this.setData(
  13. {
  14. animate: true,
  15. },
  16. () => {
  17. this.setData({
  18. translateX: 0,
  19. });
  20. }
  21. );
  22. }
  23. },
  24. },
  25. pinned: {
  26. type: Boolean,
  27. value: false,
  28. observer(newVal) {
  29. console.log("onTop changed:", newVal, typeof newVal);
  30. },
  31. },
  32. },
  33. /**
  34. * 组件的初始数据
  35. */
  36. data: {
  37. translateX: 0,
  38. animate: false,
  39. },
  40. /**
  41. * 组件的方法列表
  42. */
  43. methods: {
  44. /**
  45. * 处理touchstart事件
  46. */
  47. handleTouchStart(e) {
  48. // touch事件初始时,组件禁掉transition动画
  49. this.setData(
  50. {
  51. animate: false,
  52. },
  53. () => {
  54. this.touchStartX = e.touches[0].pageX;
  55. this.touchStartY = e.touches[0].pageY;
  56. this.startX = this.data.translateX; // 组件初始位置
  57. this.direction = null; // 记录手指滑动方向 X:左右滑动; Y:上下滑动
  58. }
  59. );
  60. },
  61. /**
  62. * 处理touchmove事件
  63. */
  64. handleTouchMove: function (e) {
  65. this.touchMoveX = e.touches[0].pageX;
  66. this.touchMoveY = e.touches[0].pageY;
  67. this.moveX = this.touchMoveX - this.touchStartX;
  68. // 竖直移动距离超过了左右移动距离
  69. if (Math.abs(this.touchMoveY - this.touchStartY) > Math.abs(this.moveX)) {
  70. this.direction = "Y";
  71. return;
  72. }
  73. this.direction = "X";
  74. // 以下两种情况不进行移动:1. 在最右边时向右滑动; 2. 在最左边时向左滑动
  75. if (
  76. (this.startX === 0 && this.moveX > 0) ||
  77. (this.startX === -this.actionWidth && this.moveX < 0)
  78. ) {
  79. return;
  80. } else if (Math.abs(this.moveX) >= this.actionWidth) {
  81. // 移动超出删除按钮的宽度时取按钮宽度作为移动距离
  82. this.moveX = this.moveX < 0 ? -this.actionWidth : this.actionWidth;
  83. this.setData({
  84. translateX: this.moveX,
  85. });
  86. } else {
  87. // 其他情况:手指滑动多少就位移多少
  88. this.setData({
  89. translateX: this.touchMoveX - this.touchStartX + this.startX,
  90. });
  91. }
  92. },
  93. /**
  94. * 处理touchend事件
  95. */
  96. handleTouchEnd: function (e) {
  97. // 非左右滑动时不进行任何操作
  98. if (this.direction !== "X") {
  99. return;
  100. }
  101. let translateX = 0;
  102. // 移动超出右滑最大位移
  103. if (this.moveX + this.startX >= 0) {
  104. translateX = 0;
  105. } else if (this.moveX + this.startX <= -this.actionWidth) {
  106. // 移动超出左滑最大位移
  107. translateX = -this.actionWidth;
  108. } else if (
  109. (this.startX === 0 && Math.abs(this.moveX) < this.actionWidth / 2) ||
  110. (this.startX === -this.actionWidth &&
  111. Math.abs(this.moveX) > this.actionWidth / 2)
  112. ) {
  113. // 以下两种情况都滑动到右边起点(即删除按钮隐藏的状态):
  114. // 1. 从右边起点左滑但未超过最大位移的一半,回退到右边起点
  115. // 2. 从左边起点右滑且超过最大位移的一半,继续滑到到右边起点
  116. translateX = 0;
  117. } else {
  118. translateX = -this.actionWidth;
  119. }
  120. this.setData(
  121. {
  122. animate: true,
  123. },
  124. () => {
  125. if (translateX === -this.actionWidth) {
  126. this.triggerEvent("action", {
  127. type: e.currentTarget.dataset.type,
  128. id: this.data.pid,
  129. });
  130. }
  131. this.setData({
  132. translateX,
  133. });
  134. }
  135. );
  136. },
  137. /**
  138. * 组件操作事件(此示例只有删除事件,可根据需要增加其他事件)
  139. */
  140. handleAction({ currentTarget: { dataset: data } }) {
  141. this.triggerEvent("action", {
  142. type: data.type,
  143. id: this.data.pid,
  144. });
  145. },
  146. },
  147. ready() {
  148. this.actionWidth = 120;
  149. console.log('i am ready')
  150. },
  151. });