rangeSlider.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. const _windowWidth = wx.getSystemInfoSync().windowWidth;
  2. Component({
  3. properties: {
  4. //组件宽度
  5. width: {
  6. type: Number,
  7. value: 750,
  8. observer: function(newVal, oldVal, changedPath) {
  9. var that = this;
  10. if (newVal != that.data.width) {
  11. this._refresh();
  12. }
  13. }
  14. },
  15. //组件高度
  16. height: {
  17. type: Number,
  18. value: 100
  19. },
  20. //滑块大小
  21. blockSize: {
  22. type: Number,
  23. value: 50,
  24. observer: function(newVal, oldVal, changedPath) {
  25. var that = this;
  26. if (newVal != that.data.blockSize) {
  27. this._refresh();
  28. }
  29. }
  30. },
  31. //区间进度条高度
  32. barHeight: {
  33. type: Number,
  34. value: 10
  35. },
  36. //背景条颜色
  37. backgroundColor: {
  38. type: String,
  39. value: "#e9e9e9"
  40. },
  41. //已选择的颜色
  42. activeColor: {
  43. type: String,
  44. value: "#1aad19"
  45. },
  46. //最小值
  47. min: {
  48. type: Number,
  49. value: 0,
  50. observer: function(newVal, oldVal, changedPath) {
  51. var that = this;
  52. if (newVal != that.data.min) {
  53. that._refresh();
  54. }
  55. }
  56. },
  57. //最大值
  58. max: {
  59. type: Number,
  60. value: 100,
  61. observer: function(newVal, oldVal, changedPath) {
  62. var that = this;
  63. if (newVal != that.data.max) {
  64. that._refresh();
  65. }
  66. }
  67. },
  68. //设置初始值
  69. values: {
  70. type: Object,
  71. value: [0, 100],
  72. observer: function(newVal, oldVal, changedPath) {
  73. var that = this;
  74. var values = that.data.values;
  75. if (that._isValuesValid(newVal) && that._isValuesValid(values)) {
  76. if (newVal[0] != values[0] || newVal[1] != values[1])
  77. that._refresh();
  78. }
  79. }
  80. }
  81. },
  82. /**
  83. * 组件生命周期函数,在组件布局完成后执行
  84. */
  85. ready() {
  86. this._refresh();
  87. },
  88. options: {
  89. multipleSlots: true
  90. },
  91. data: {
  92. MAX_LENGTH: 700,
  93. minBlockLeft: 0,
  94. maxBlockLeft: 700,
  95. progressBarLeft: 0,
  96. progressBarWidth: 700
  97. },
  98. methods: {
  99. _pxToRpx: function(px) {
  100. return 750 * px / _windowWidth;
  101. },
  102. _onBlockTouchStart: function(e) {
  103. this._blockDownX = e.changedTouches[0].pageX
  104. this._blockLeft = e.target.dataset.left;
  105. this._curBlock = e.target.dataset.tag;
  106. },
  107. _onBlockTouchMove: function(e) {
  108. var that = this;
  109. var values = that._calculateValues(e);
  110. that._refreshProgressBar(values[2], values[3]);
  111. that._refreshBlock(values[2], values[3]);
  112. },
  113. _onBlockTouchEnd: function(e) {
  114. var that = this;
  115. var values = that._calculateValues(e);
  116. that._refreshProgressBar(values[2], values[3]);
  117. that._refreshBlock(values[2], values[3]);
  118. var eventDetail = {
  119. minValue: values[0],
  120. maxValue: values[1],
  121. fromUser: true
  122. };
  123. var eventOption = {};
  124. that.triggerEvent("rangechange", eventDetail, eventOption);
  125. },
  126. _isValuesValid: function(values) {
  127. return values != null && values != undefined && values.length == 2;
  128. },
  129. /**
  130. * 根据手势计算相关数据
  131. */
  132. _calculateValues: function(e) {
  133. var that = this;
  134. var moveLength = e.changedTouches[0].pageX - that._blockDownX;
  135. var left = that._blockLeft + that._pxToRpx(moveLength)
  136. left = Math.max(0, left);
  137. left = Math.min(left, that.data.MAX_LENGTH);
  138. var minBlockLeft = that.data.minBlockLeft;
  139. var maxBlockLeft = that.data.maxBlockLeft;
  140. if (that._curBlock == "minBlock") {
  141. minBlockLeft = left;
  142. } else {
  143. maxBlockLeft = left;
  144. }
  145. var range = that.data.max - that.data.min;
  146. var minLeft = Math.min(minBlockLeft, maxBlockLeft);
  147. var maxLeft = Math.max(minBlockLeft, maxBlockLeft);
  148. var minValue = minLeft / that.data.MAX_LENGTH * range + that.data.min;
  149. var maxValue = maxLeft / that.data.MAX_LENGTH * range + that.data.min;
  150. return [minValue, maxValue, minLeft, maxLeft];
  151. },
  152. /**
  153. * 计算滑块坐标
  154. */
  155. _calculateBlockLeft: function(minValue, maxValue) {
  156. var that = this;
  157. var blockSize = that.data.blockSize;
  158. var range = that.data.max - that.data.min;
  159. var minLeft = (minValue - that.data.min) / range * that.data.MAX_LENGTH;
  160. var maxLeft = (maxValue - that.data.min) / range * that.data.MAX_LENGTH;
  161. return [minLeft, maxLeft];
  162. },
  163. /**
  164. * 刷新进度条视图
  165. */
  166. _refreshProgressBar: function(minBlockLeft, maxBlockLeft) {
  167. var that = this;
  168. var blockSize = that.data.blockSize;
  169. that.setData({
  170. progressBarLeft: minBlockLeft + blockSize / 2,
  171. progressBarWidth: Math.abs(maxBlockLeft - minBlockLeft)
  172. });
  173. },
  174. /**
  175. * 刷新滑块视图
  176. */
  177. _refreshBlock: function(minBlockLeft, maxBlockLeft) {
  178. var that = this;
  179. that.setData({
  180. minBlockLeft: minBlockLeft,
  181. maxBlockLeft: maxBlockLeft
  182. });
  183. },
  184. /**
  185. * 刷新整个视图
  186. */
  187. _refresh: function() {
  188. var that = this;
  189. var MAX_LENGTH = that.data.width - that.data.blockSize;
  190. that.setData({
  191. MAX_LENGTH: MAX_LENGTH,
  192. maxBlockLeft: MAX_LENGTH,
  193. progressBarWidth: MAX_LENGTH
  194. });
  195. var values = that.data.values;
  196. if (that._isValuesValid(values)) {
  197. values[0] = Math.max(that.data.min, values[0]);
  198. values[0] = Math.min(values[0], that.data.max);
  199. values[1] = Math.max(that.data.min, values[1]);
  200. values[1] = Math.min(values[1], that.data.max);
  201. var leftValues = that._calculateBlockLeft(values[0], values[1]);
  202. that._refreshProgressBar(leftValues[0], leftValues[1]);
  203. that._refreshBlock(leftValues[0], leftValues[1]);
  204. }
  205. }
  206. }
  207. })