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 } 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. }
  59. else {
  60. this.startValue = this.format(this.newValue);
  61. }
  62. this.dragStatus = DRAG_STATUS.START;
  63. },
  64. onTouchMove(event) {
  65. if (this.data.disabled)
  66. return;
  67. if (this.dragStatus === DRAG_STATUS.START) {
  68. this.$emit('drag-start');
  69. }
  70. this.touchMove(event);
  71. this.dragStatus = DRAG_STATUS.MOVING;
  72. getRect(this, '.van-slider').then((rect) => {
  73. const { vertical } = this.data;
  74. const delta = vertical ? this.deltaY : this.deltaX;
  75. const total = vertical ? rect.height : rect.width;
  76. const diff = (delta / total) * this.getRange();
  77. if (this.isRange(this.startValue)) {
  78. this.newValue[this.buttonIndex] =
  79. this.startValue[this.buttonIndex] + diff;
  80. }
  81. else {
  82. this.newValue = this.startValue + diff;
  83. }
  84. this.updateValue(this.newValue, false, true);
  85. });
  86. },
  87. onTouchEnd() {
  88. if (this.data.disabled)
  89. return;
  90. if (this.dragStatus === DRAG_STATUS.MOVING) {
  91. this.dragStatus = DRAG_STATUS.END;
  92. nextTick(() => {
  93. this.updateValue(this.newValue, true);
  94. this.$emit('drag-end');
  95. });
  96. }
  97. },
  98. onClick(event) {
  99. if (this.data.disabled)
  100. return;
  101. const { min } = this.data;
  102. getRect(this, '.van-slider').then((rect) => {
  103. const { vertical } = this.data;
  104. const touch = event.touches[0];
  105. const delta = vertical
  106. ? touch.clientY - rect.top
  107. : touch.clientX - rect.left;
  108. const total = vertical ? rect.height : rect.width;
  109. const value = Number(min) + (delta / total) * this.getRange();
  110. if (this.isRange(this.value)) {
  111. const [left, right] = this.value;
  112. const middle = (left + right) / 2;
  113. if (value <= middle) {
  114. this.updateValue([value, right], true);
  115. }
  116. else {
  117. this.updateValue([left, value], true);
  118. }
  119. }
  120. else {
  121. this.updateValue(value, true);
  122. }
  123. });
  124. },
  125. isRange(val) {
  126. const { range } = this.data;
  127. return range && Array.isArray(val);
  128. },
  129. handleOverlap(value) {
  130. if (value[0] > value[1]) {
  131. return value.slice(0).reverse();
  132. }
  133. return value;
  134. },
  135. updateValue(value, end, drag) {
  136. if (this.isRange(value)) {
  137. value = this.handleOverlap(value).map((val) => this.format(val));
  138. }
  139. else {
  140. value = this.format(value);
  141. }
  142. this.value = value;
  143. const { vertical } = this.data;
  144. const mainAxis = vertical ? 'height' : 'width';
  145. this.setData({
  146. wrapperStyle: `
  147. background: ${this.data.inactiveColor || ''};
  148. ${vertical ? 'width' : 'height'}: ${addUnit(this.data.barHeight) || ''};
  149. `,
  150. barStyle: `
  151. ${mainAxis}: ${this.calcMainAxis()};
  152. left: ${vertical ? 0 : this.calcOffset()};
  153. top: ${vertical ? this.calcOffset() : 0};
  154. ${drag ? 'transition: none;' : ''}
  155. `,
  156. });
  157. if (drag) {
  158. this.$emit('drag', { value });
  159. }
  160. if (end) {
  161. this.$emit('change', value);
  162. }
  163. if ((drag || end) && canIUseModel()) {
  164. this.setData({ value });
  165. }
  166. },
  167. getScope() {
  168. return Number(this.data.max) - Number(this.data.min);
  169. },
  170. getRange() {
  171. const { max, min } = this.data;
  172. return max - min;
  173. },
  174. getOffsetWidth(current, min) {
  175. const scope = this.getScope();
  176. // 避免最小值小于最小step时出现负数情况
  177. return `${Math.max(((current - min) * 100) / scope, 0)}%`;
  178. },
  179. // 计算选中条的长度百分比
  180. calcMainAxis() {
  181. const { value } = this;
  182. const { min } = this.data;
  183. if (this.isRange(value)) {
  184. return this.getOffsetWidth(value[1], value[0]);
  185. }
  186. return this.getOffsetWidth(value, Number(min));
  187. },
  188. // 计算选中条的开始位置的偏移量
  189. calcOffset() {
  190. const { value } = this;
  191. const { min } = this.data;
  192. const scope = this.getScope();
  193. if (this.isRange(value)) {
  194. return `${((value[0] - Number(min)) * 100) / scope}%`;
  195. }
  196. return '0%';
  197. },
  198. format(value) {
  199. const { max, min, step } = this.data;
  200. return Math.round(Math.max(min, Math.min(value, max)) / step) * step;
  201. },
  202. },
  203. });