range-slider.vue 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. <template>
  2. <view class="range-slider" :style="'width:' + width + 'rpx;height:' + height + 'rpx'">
  3. <view class="range-bar" :style="'width:100%;height:' + barHeight + 'rpx'">
  4. <view class="range-bar-bg" :style="'background-color:' + backgroundColor + ''"></view>
  5. <view class="range-bar-progress" :style="'margin-left:' + progressBarLeft + 'rpx;width:' + progressBarWidth + 'rpx;background-color:' + activeColor + ''"></view>
  6. </view>
  7. <view
  8. class="block"
  9. :class="{ active: isMinActive }"
  10. :style="'width:' + blockSize + 'rpx;height:' + blockSize + 'rpx;margin-left:' + minBlockLeft + 'rpx;'"
  11. @touchstart="_onBlockTouchStart"
  12. @touchmove.stop="_onBlockTouchMove"
  13. @touchend="_onBlockTouchEnd"
  14. :data-left="minBlockLeft"
  15. data-tag="minBlock"
  16. >
  17. <slot name="minBlock"></slot>
  18. </view>
  19. <view
  20. class="block"
  21. :class="{ active: isMaxActive }"
  22. :style="'width:' + blockSize + 'rpx;height:' + blockSize + 'rpx;margin-left:' + maxBlockLeft + 'rpx;'"
  23. @touchstart="_onBlockTouchStart"
  24. @touchmove.stop="_onBlockTouchMove"
  25. @touchend="_onBlockTouchEnd"
  26. :data-left="maxBlockLeft"
  27. data-tag="maxBlock"
  28. >
  29. <slot name="maxBlock"></slot>
  30. </view>
  31. </view>
  32. </template>
  33. <script>
  34. /**
  35. * range-slider v1.0.6
  36. */
  37. const _windowWidth = uni.getSystemInfoSync().windowWidth;
  38. export default {
  39. data() {
  40. return {
  41. isMinActive: false,
  42. isMaxActive: false,
  43. //#ifdef H5
  44. MAX_LENGTH: 294,
  45. maxBlockLeft: 300,
  46. //#endif
  47. // #ifndef H5
  48. MAX_LENGTH: 700,
  49. maxBlockLeft: 350,
  50. // #endif
  51. minBlockLeft: 0,
  52. progressBarLeft: 0,
  53. progressBarWidth: 350,
  54. originalMinValue: 0,
  55. originalMaxValue: 0
  56. };
  57. },
  58. components: {},
  59. props: {
  60. //组件宽度
  61. width: {
  62. type: Number,
  63. default: 750
  64. },
  65. //组件高度
  66. height: {
  67. type: Number,
  68. default: 100
  69. },
  70. //滑块大小
  71. blockSize: {
  72. type: Number,
  73. default: 50
  74. },
  75. //区间进度条高度
  76. barHeight: {
  77. type: Number,
  78. default: 5
  79. },
  80. //背景条颜色
  81. backgroundColor: {
  82. type: String,
  83. default: '#e9e9e9'
  84. },
  85. //已选择的颜色
  86. activeColor: {
  87. type: String,
  88. default: '#1aad19'
  89. },
  90. //最小值
  91. min: {
  92. type: Number,
  93. default: 0
  94. },
  95. //最大值
  96. max: {
  97. type: Number,
  98. default: 100
  99. },
  100. //设置初始值
  101. values: {
  102. type: Array,
  103. default: function() {
  104. return [0, 100];
  105. }
  106. },
  107. //步长值
  108. step: {
  109. type: Number,
  110. default: 1
  111. },
  112. //live模式,是否动态更新
  113. liveMode: {
  114. type: Boolean,
  115. default: true
  116. }
  117. },
  118. created: function() {
  119. //使用自定义组件编译模式时,支持生命周期为:created
  120. this._refresh();
  121. },
  122. onLoad: function(option) {
  123. //不使用自定义组件编译模式时,支持生命周期为:onload
  124. this._refresh();
  125. },
  126. onUnload: function() {},
  127. watch: {
  128. //组件宽度
  129. width: function(newVal, oldVal, changedPath) {
  130. var that = this;
  131. if (newVal != that.width) {
  132. this._refresh();
  133. }
  134. },
  135. //滑块大小
  136. blockSize: function(newVal, oldVal, changedPath) {
  137. var that = this;
  138. if (newVal != that.blockSize) {
  139. this._refresh();
  140. }
  141. },
  142. //最小值
  143. min: function(newVal, oldVal, changedPath) {
  144. var that = this;
  145. if (newVal != that.min) {
  146. that._refresh();
  147. }
  148. },
  149. //最大值
  150. max: function(newVal, oldVal, changedPath) {
  151. var that = this;
  152. if (newVal != that.max) {
  153. that._refresh();
  154. }
  155. },
  156. //设置初始值
  157. values: function(newVal, oldVal, changedPath) {
  158. var that = this;
  159. var values = that.values;
  160. console.log('refresh', newVal, oldVal);
  161. if (that._isValuesValid(newVal) && that._isValuesValid(values)) {
  162. if (values[0] != oldVal[0] || values[1] != oldVal[1]) that._refresh();
  163. }
  164. }
  165. },
  166. methods: {
  167. //补0
  168. _pad: function(num, n) {
  169. return Array(n - ('' + num).length + 1).join(0) + num;
  170. },
  171. _pxToRpx: function(px) {
  172. return (750 * px) / _windowWidth;
  173. },
  174. _onBlockTouchStart: function(e) {
  175. let tag = e.target.dataset.tag;
  176. if (tag == 'minBlock' || tag == 'maxBlock') {
  177. this.isMinActive = tag == 'minBlock';
  178. this.isMaxActive = tag == 'maxBlock';
  179. //兼容h5平台及某版本微信
  180. if (e.hasOwnProperty('changedTouches')) {
  181. this._blockDownX = e.changedTouches[0].pageX;
  182. } else {
  183. this._blockDownX = e.pageX;
  184. }
  185. //#ifdef H5
  186. this._blockLeft = parseFloat(e.target.dataset.left);
  187. //#endif
  188. // #ifndef H5
  189. this._blockLeft = e.target.dataset.left;
  190. // #endif
  191. this._curBlock = e.target.dataset.tag;
  192. }
  193. },
  194. _onBlockTouchMove: function(e) {
  195. let tag = e.target.dataset.tag;
  196. if (tag == 'minBlock' || tag == 'maxBlock') {
  197. var that = this;
  198. var values = that._calculateValues(e);
  199. that._refreshProgressBar(values[2], values[3]);
  200. that._refreshBlock(values[2], values[3]);
  201. //拖动时也触发事件
  202. var eventDetail = {
  203. minValue: this.formatNumber(values[0], this.step),
  204. maxValue: this.formatNumber(values[1], this.step),
  205. fromUser: true,
  206. originalValue: {
  207. minValue: values[0],
  208. maxValue: values[1]
  209. }
  210. };
  211. this.originalMinValue = values[0];
  212. this.originalMaxValue = values[1];
  213. var eventOption = {};
  214. //
  215. if (this.liveMode) that.$emit('rangechange', eventDetail, eventOption);
  216. }
  217. },
  218. _onBlockTouchEnd: function(e) {
  219. let tag = e.target.dataset.tag;
  220. this.isMinActive = false;
  221. this.isMaxActive = false;
  222. if (tag == 'minBlock' || tag == 'maxBlock') {
  223. var that = this;
  224. var values = that._calculateValues(e.mp.changedTouches[0]);
  225. that._refreshProgressBar(values[2], values[3]);
  226. that._refreshBlock(values[2], values[3]);
  227. var eventDetail = {
  228. minValue: this.formatNumber(values[0], this.step),
  229. maxValue: this.formatNumber(values[1], this.step),
  230. fromUser: true,
  231. originalValue: {
  232. minValue: values[0],
  233. maxValue: values[1]
  234. }
  235. };
  236. this.originalMinValue = values[0];
  237. this.originalMaxValue = values[1];
  238. var eventOption = {};
  239. that.$emit('rangechange', eventDetail, eventOption);
  240. }
  241. },
  242. _isValuesValid: function(values) {
  243. return values != null && values != undefined && values.length == 2;
  244. },
  245. /**
  246. * 根据手势计算相关数据
  247. */
  248. _calculateValues: function(e) {
  249. var pageX = e.pageX;
  250. //兼容h5平台
  251. if (e.hasOwnProperty('changedTouches')) {
  252. pageX = e.changedTouches[0].pageX;
  253. }
  254. var that = this;
  255. var moveLength = pageX - that._blockDownX;
  256. var left = that._blockLeft + that._pxToRpx(moveLength);
  257. left = Math.max(0, left);
  258. left = Math.min(left, that.MAX_LENGTH);
  259. var minBlockLeft = that.minBlockLeft;
  260. var maxBlockLeft = that.maxBlockLeft;
  261. if (that._curBlock == 'minBlock') {
  262. minBlockLeft = left;
  263. } else {
  264. maxBlockLeft = left;
  265. }
  266. var range = that.max - that.min;
  267. var minLeft = Math.min(minBlockLeft, maxBlockLeft);
  268. var maxLeft = Math.max(minBlockLeft, maxBlockLeft);
  269. var minValue = (minLeft / that.MAX_LENGTH) * range + that.min;
  270. var maxValue = (maxLeft / that.MAX_LENGTH) * range + that.min;
  271. return [minValue, maxValue, minLeft, maxLeft];
  272. },
  273. /**
  274. * 计算滑块坐标
  275. */
  276. _calculateBlockLeft: function(minValue, maxValue) {
  277. var that = this;
  278. var blockSize = that.blockSize;
  279. var range = that.max - that.min;
  280. var minLeft = ((minValue - that.min) / range) * that.MAX_LENGTH;
  281. var maxLeft = ((maxValue - that.min) / range) * that.MAX_LENGTH;
  282. return [minLeft, maxLeft];
  283. },
  284. /**
  285. * 刷新进度条视图
  286. */
  287. _refreshProgressBar: function(minBlockLeft, maxBlockLeft) {
  288. var that = this;
  289. var blockSize = that.blockSize;
  290. that.progressBarLeft = minBlockLeft + blockSize / 2;
  291. that.progressBarWidth = Math.abs(maxBlockLeft - minBlockLeft);
  292. },
  293. /**
  294. * 刷新滑块视图
  295. */
  296. _refreshBlock: function(minBlockLeft, maxBlockLeft) {
  297. var that = this;
  298. that.minBlockLeft = minBlockLeft;
  299. that.maxBlockLeft = maxBlockLeft;
  300. },
  301. /**
  302. * 刷新整个视图
  303. */
  304. _refresh: function() {
  305. var that = this;
  306. var MAX_LENGTH = that.width - that.blockSize;
  307. that.MAX_LENGTH = MAX_LENGTH;
  308. that.maxBlockLeft = MAX_LENGTH;
  309. that.progressBarWidth = MAX_LENGTH;
  310. var values = that.values;
  311. if (this.originalMinValue && this.originalMinValue) {
  312. values = [this.originalMinValue || values[0], this.originalMaxValue || values[1]];
  313. }
  314. if (that._isValuesValid(values)) {
  315. values[0] = Math.max(that.min, values[0]);
  316. values[0] = Math.min(values[0], that.max);
  317. values[1] = Math.max(that.min, values[1]);
  318. values[1] = Math.min(values[1], that.max);
  319. var leftValues = that._calculateBlockLeft(values[0], values[1]);
  320. that._refreshProgressBar(leftValues[0], leftValues[1]);
  321. that._refreshBlock(leftValues[0], leftValues[1]);
  322. }
  323. },
  324. formatNumber(num, step) {
  325. //格式化数字,保留几位小数
  326. let stepStr = '' + step;
  327. let index = stepStr.indexOf('.');
  328. let len = index > -1 ? stepStr.length - index - 1 : 0;
  329. let offestNum = parseInt(1 + Array(('' + len).length + 1).join(0)) * 0.1;
  330. let tmpNum = num * offestNum;
  331. return ((parseInt(tmpNum / step + (step > 1 ? 1 : step) * 0.5) * step) / offestNum).toFixed(len);
  332. }
  333. }
  334. };
  335. </script>
  336. <style>
  337. .range-slider {
  338. position: relative;
  339. }
  340. .range-bar {
  341. position: absolute;
  342. }
  343. .range-bar {
  344. position: absolute;
  345. top: 50%;
  346. transform: translate(0, -50%);
  347. border-radius: 10000rpx;
  348. }
  349. .range-bar-bg {
  350. position: absolute;
  351. width: 100%;
  352. height: 100%;
  353. border-radius: 10000rpx;
  354. }
  355. .range-bar-progress {
  356. position: absolute;
  357. width: 100%;
  358. height: 100%;
  359. background-color: blueviolet;
  360. }
  361. .block {
  362. position: absolute;
  363. top: 50%;
  364. transform: translate(0, -50%);
  365. background: #fff;
  366. border-radius: 50%;
  367. box-shadow: 0rpx 0rpx 6rpx #ccc;
  368. }
  369. .block.active {
  370. transform: translate(0, -50%) scale(1.5);
  371. }
  372. </style>