index.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. import {VantComponent} from '../common/component';
  2. import {touch} from '../mixins/touch';
  3. import {canIUseModel} from '../common/version';
  4. import {getRect, addUnit, nextTick, addNumber, clamp} from '../common/utils';
  5. const DRAG_STATUS = {
  6. START: 'start',
  7. MOVING: 'moving',
  8. END: 'end',
  9. };
  10. VantComponent({
  11. mixins: [touch],
  12. props: {
  13. range: Boolean,
  14. disabled: Boolean,
  15. useButtonSlot: Boolean,
  16. activeColor: String,
  17. inactiveColor: String,
  18. max: {
  19. type: Number,
  20. value: 100,
  21. },
  22. min: {
  23. type: Number,
  24. value: 0,
  25. },
  26. step: {
  27. type: Number,
  28. value: 1,
  29. },
  30. value: {
  31. type: null,
  32. value: 0,
  33. observer(val) {
  34. if (val !== this.value) {
  35. this.updateValue(val);
  36. }
  37. },
  38. },
  39. vertical: Boolean,
  40. barHeight: null,
  41. },
  42. created() {
  43. this.updateValue(this.data.value);
  44. },
  45. methods: {
  46. onTouchStart(event) {
  47. if (this.data.disabled)
  48. return;
  49. const {index} = event.currentTarget.dataset;
  50. if (typeof index === 'number') {
  51. this.buttonIndex = index;
  52. }
  53. this.touchStart(event);
  54. this.startValue = this.format(this.value);
  55. this.newValue = this.value;
  56. if (this.isRange(this.newValue)) {
  57. this.startValue = this.newValue.map((val) => this.format(val));
  58. } else {
  59. this.startValue = this.format(this.newValue);
  60. }
  61. this.dragStatus = DRAG_STATUS.START;
  62. },
  63. onTouchMove(event) {
  64. if (this.data.disabled)
  65. return;
  66. if (this.dragStatus === DRAG_STATUS.START) {
  67. this.$emit('drag-start');
  68. }
  69. this.touchMove(event);
  70. this.dragStatus = DRAG_STATUS.MOVING;
  71. getRect(this, '.van-slider').then((rect) => {
  72. const {vertical} = this.data;
  73. const delta = vertical ? this.deltaY : this.deltaX;
  74. const total = vertical ? rect.height : rect.width;
  75. const diff = (delta / total) * this.getRange();
  76. if (this.isRange(this.startValue)) {
  77. this.newValue[this.buttonIndex] =
  78. this.startValue[this.buttonIndex] + diff;
  79. } else {
  80. this.newValue = this.startValue + diff;
  81. }
  82. this.updateValue(this.newValue, false, true);
  83. });
  84. },
  85. onTouchEnd() {
  86. if (this.data.disabled)
  87. return;
  88. if (this.dragStatus === DRAG_STATUS.MOVING) {
  89. this.dragStatus = DRAG_STATUS.END;
  90. nextTick(() => {
  91. this.updateValue(this.newValue, true);
  92. this.$emit('drag-end');
  93. });
  94. }
  95. },
  96. onClick(event) {
  97. if (this.data.disabled)
  98. return;
  99. const {min} = this.data;
  100. getRect(this, '.van-slider').then((rect) => {
  101. const {vertical} = this.data;
  102. const touch = event.touches[0];
  103. const delta = vertical
  104. ? touch.clientY - rect.top
  105. : touch.clientX - rect.left;
  106. const total = vertical ? rect.height : rect.width;
  107. const value = Number(min) + (delta / total) * this.getRange();
  108. if (this.isRange(this.value)) {
  109. const [left, right] = this.value;
  110. const middle = (left + right) / 2;
  111. if (value <= middle) {
  112. this.updateValue([value, right], true);
  113. } else {
  114. this.updateValue([left, value], true);
  115. }
  116. } else {
  117. this.updateValue(value, true);
  118. }
  119. });
  120. },
  121. isRange(val) {
  122. const {range} = this.data;
  123. return range && Array.isArray(val);
  124. },
  125. handleOverlap(value) {
  126. if (value[0] > value[1]) {
  127. return value.slice(0).reverse();
  128. }
  129. return value;
  130. },
  131. updateValue(value, end, drag) {
  132. if (this.isRange(value)) {
  133. value = this.handleOverlap(value).map((val) => this.format(val));
  134. } else {
  135. value = this.format(value);
  136. }
  137. this.value = value;
  138. const {vertical} = this.data;
  139. const mainAxis = vertical ? 'height' : 'width';
  140. this.setData({
  141. wrapperStyle: `
  142. background: ${this.data.inactiveColor || ''};
  143. ${vertical ? 'width' : 'height'}: ${addUnit(this.data.barHeight) || ''};
  144. `,
  145. barStyle: `
  146. ${mainAxis}: ${this.calcMainAxis()};
  147. left: ${vertical ? 0 : this.calcOffset()};
  148. top: ${vertical ? this.calcOffset() : 0};
  149. ${drag ? 'transition: none;' : ''}
  150. `,
  151. });
  152. if (drag) {
  153. this.$emit('drag', {value});
  154. }
  155. if (end) {
  156. this.$emit('change', value);
  157. }
  158. if ((drag || end) && canIUseModel()) {
  159. this.setData({value});
  160. }
  161. },
  162. getScope() {
  163. return Number(this.data.max) - Number(this.data.min);
  164. },
  165. getRange() {
  166. const {max, min} = this.data;
  167. return max - min;
  168. },
  169. getOffsetWidth(current, min) {
  170. const scope = this.getScope();
  171. // 避免最小值小于最小step时出现负数情况
  172. return `${Math.max(((current - min) * 100) / scope, 0)}%`;
  173. },
  174. // 计算选中条的长度百分比
  175. calcMainAxis() {
  176. const {value} = this;
  177. const {min} = this.data;
  178. if (this.isRange(value)) {
  179. return this.getOffsetWidth(value[1], value[0]);
  180. }
  181. return this.getOffsetWidth(value, Number(min));
  182. },
  183. // 计算选中条的开始位置的偏移量
  184. calcOffset() {
  185. const {value} = this;
  186. const {min} = this.data;
  187. const scope = this.getScope();
  188. if (this.isRange(value)) {
  189. return `${((value[0] - Number(min)) * 100) / scope}%`;
  190. }
  191. return '0%';
  192. },
  193. format(value) {
  194. const min = +this.data.min;
  195. const max = +this.data.max;
  196. const step = +this.data.step;
  197. value = clamp(value, min, max);
  198. const diff = Math.round((value - min) / step) * step;
  199. return addNumber(min, diff);
  200. },
  201. },
  202. });