index.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. // slide-view/slide-view.js
  2. const _windowWidth = wx.getSystemInfoSync().windowWidth;
  3. Component({
  4. /**
  5. * 组件的属性列表
  6. */
  7. options: {
  8. multipleSlots: true,
  9. },
  10. properties: {
  11. // 组件显示区域的宽度
  12. width: {
  13. type: Number,
  14. value: _windowWidth
  15. },
  16. // 组件显示区域的高度
  17. height: {
  18. type: Number,
  19. value: 100,
  20. },
  21. // 组件滑动显示区域的宽度
  22. slideWidth: {
  23. type: Number,
  24. value: 0
  25. },
  26. // 阈值
  27. threshold: {
  28. type: Number,
  29. value: 0
  30. },
  31. // 禁用
  32. disabled: {
  33. type: Boolean,
  34. value: false
  35. },
  36. // 自动关闭
  37. autoClose: {
  38. type: Boolean,
  39. value: false
  40. },
  41. // 主动关闭
  42. close: {
  43. type: Boolean,
  44. value: false,
  45. observer: function (newVal) {
  46. if (newVal) {
  47. this.setData({
  48. popup: false,
  49. x: 0
  50. });
  51. this.onCloseTap();
  52. }
  53. }
  54. }
  55. },
  56. /**
  57. * 组件的初始数据
  58. */
  59. data: {
  60. viewWidth: _windowWidth,
  61. // movable-view偏移量
  62. x: 0,
  63. // movable-view是否可以出界
  64. out: false,
  65. },
  66. /**
  67. * 组件的方法列表
  68. */
  69. ready() {
  70. this.updateRight();
  71. },
  72. methods: {
  73. updateRight() {
  74. // 获取右侧滑动显示区域的宽度
  75. const that = this;
  76. const query = wx.createSelectorQuery().in(this);
  77. query.select('.right').boundingClientRect(function (res) {
  78. that._slideWidth = res.width;
  79. let width = res.width <=50 ? res.width : 50;
  80. that._threshold = that.properties.threshold ? that.properties.threshold : width;
  81. that._viewWidth = that.data.width + res.width * (750 / _windowWidth);
  82. that.setData({
  83. viewWidth: that._viewWidth
  84. });
  85. }).exec();
  86. },
  87. onTouchStart(e) {
  88. this._startX = e.changedTouches[0].pageX;
  89. },
  90. // 当滑动范围超过阈值自动完成剩余滑动
  91. onTouchEnd(e) {
  92. if (this.properties.disabled) return;
  93. this._endX = e.changedTouches[0].pageX;
  94. this._length = this._endX - this._startX;
  95. const {
  96. _endX,
  97. _startX,
  98. _threshold
  99. } = this;
  100. if (this._length > _threshold) {
  101. this.setData({
  102. popup: false,
  103. x: 0,
  104. });
  105. this.onCloseTap();
  106. }
  107. if (_endX > _startX && this.data.out === false) return;
  108. if (_startX - _endX >= _threshold) {
  109. this.setData({
  110. x: -this._slideWidth,
  111. popup: true,
  112. close: false
  113. });
  114. this.onOpenTap();
  115. } else if (_startX - _endX < _threshold && _startX - _endX > 0 && this.data.popup != true) {
  116. this.setData({
  117. x: 0
  118. });
  119. this.onCloseTap();
  120. } else if (_endX - _startX >= _threshold) {
  121. this.setData({
  122. x: 0
  123. });
  124. this.onCloseTap();
  125. } else if (_endX - _startX < _threshold && _endX - _startX > 0) {
  126. this.setData({
  127. x: -this._slideWidth,
  128. close: false
  129. });
  130. this.onOpenTap();
  131. }
  132. },
  133. // 根据滑动的范围设定是否允许movable-view出界
  134. onChange(e) {
  135. if (!this.data.out && e.detail.x < -this._threshold) {
  136. this.setData({
  137. out: true
  138. });
  139. } else if (this.data.out && e.detail.x >= -this._threshold) {
  140. this.setData({
  141. out: false
  142. });
  143. }
  144. },
  145. // 点击 右边区域
  146. onRightTap() {
  147. let detail = 'click right';
  148. let option = { bubbles: true, composed: true };
  149. if (this.properties.autoClose) {
  150. this.setData({
  151. popup: false,
  152. x: 0
  153. });
  154. this.onCloseTap();
  155. }
  156. this.triggerEvent('lintap', detail, option);
  157. },
  158. // 打开后触发
  159. onOpenTap() {
  160. let detail = true;
  161. let option = { bubbles: true, composed: true };
  162. this.triggerEvent('slideopen', detail, option);
  163. },
  164. // 关闭后触发
  165. onCloseTap() {
  166. let detail = false;
  167. let option = { bubbles: true, composed: true };
  168. this.triggerEvent('slideclose', detail, option);
  169. }
  170. }
  171. });