button.vue 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. <template>
  2. <div
  3. class="el-slider__button-wrapper"
  4. @mouseenter="handleMouseEnter"
  5. @mouseleave="handleMouseLeave"
  6. @mousedown="onButtonDown"
  7. @touchstart="onButtonDown"
  8. :class="{ 'hover': hovering, 'dragging': dragging }"
  9. :style="wrapperStyle"
  10. ref="button"
  11. tabindex="0"
  12. @focus="handleMouseEnter"
  13. @blur="handleMouseLeave"
  14. @keydown.left="onLeftKeyDown"
  15. @keydown.right="onRightKeyDown"
  16. @keydown.down.prevent="onLeftKeyDown"
  17. @keydown.up.prevent="onRightKeyDown"
  18. >
  19. <el-tooltip
  20. placement="top"
  21. ref="tooltip"
  22. :popper-class="tooltipClass"
  23. :disabled="!showTooltip">
  24. <span slot="content">{{ formatValue }}</span>
  25. <div class="el-slider__button" :class="{ 'hover': hovering, 'dragging': dragging }"></div>
  26. </el-tooltip>
  27. </div>
  28. </template>
  29. <script>
  30. import ElTooltip from 'element-ui/packages/tooltip';
  31. export default {
  32. name: 'ElSliderButton',
  33. components: {
  34. ElTooltip
  35. },
  36. props: {
  37. value: {
  38. type: Number,
  39. default: 0
  40. },
  41. vertical: {
  42. type: Boolean,
  43. default: false
  44. },
  45. tooltipClass: String
  46. },
  47. data() {
  48. return {
  49. hovering: false,
  50. dragging: false,
  51. isClick: false,
  52. startX: 0,
  53. currentX: 0,
  54. startY: 0,
  55. currentY: 0,
  56. startPosition: 0,
  57. newPosition: null,
  58. oldValue: this.value
  59. };
  60. },
  61. computed: {
  62. disabled() {
  63. return this.$parent.sliderDisabled;
  64. },
  65. max() {
  66. return this.$parent.max;
  67. },
  68. min() {
  69. return this.$parent.min;
  70. },
  71. step() {
  72. return this.$parent.step;
  73. },
  74. showTooltip() {
  75. return this.$parent.showTooltip;
  76. },
  77. precision() {
  78. return this.$parent.precision;
  79. },
  80. currentPosition() {
  81. return `${ (this.value - this.min) / (this.max - this.min) * 100 }%`;
  82. },
  83. enableFormat() {
  84. return this.$parent.formatTooltip instanceof Function;
  85. },
  86. formatValue() {
  87. return this.enableFormat && this.$parent.formatTooltip(this.value) || this.value;
  88. },
  89. wrapperStyle() {
  90. return this.vertical ? { bottom: this.currentPosition } : { left: this.currentPosition };
  91. }
  92. },
  93. watch: {
  94. dragging(val) {
  95. this.$parent.dragging = val;
  96. }
  97. },
  98. methods: {
  99. displayTooltip() {
  100. this.$refs.tooltip && (this.$refs.tooltip.showPopper = true);
  101. },
  102. hideTooltip() {
  103. this.$refs.tooltip && (this.$refs.tooltip.showPopper = false);
  104. },
  105. handleMouseEnter() {
  106. this.hovering = true;
  107. this.displayTooltip();
  108. },
  109. handleMouseLeave() {
  110. this.hovering = false;
  111. this.hideTooltip();
  112. },
  113. onButtonDown(event) {
  114. if (this.disabled) return;
  115. event.preventDefault();
  116. this.onDragStart(event);
  117. window.addEventListener('mousemove', this.onDragging);
  118. window.addEventListener('touchmove', this.onDragging);
  119. window.addEventListener('mouseup', this.onDragEnd);
  120. window.addEventListener('touchend', this.onDragEnd);
  121. window.addEventListener('contextmenu', this.onDragEnd);
  122. },
  123. onLeftKeyDown() {
  124. if (this.disabled) return;
  125. this.newPosition = parseFloat(this.currentPosition) - this.step / (this.max - this.min) * 100;
  126. this.setPosition(this.newPosition);
  127. this.$parent.emitChange();
  128. },
  129. onRightKeyDown() {
  130. if (this.disabled) return;
  131. this.newPosition = parseFloat(this.currentPosition) + this.step / (this.max - this.min) * 100;
  132. this.setPosition(this.newPosition);
  133. this.$parent.emitChange();
  134. },
  135. onDragStart(event) {
  136. this.dragging = true;
  137. this.isClick = true;
  138. if (event.type === 'touchstart') {
  139. event.clientY = event.touches[0].clientY;
  140. event.clientX = event.touches[0].clientX;
  141. }
  142. if (this.vertical) {
  143. this.startY = event.clientY;
  144. } else {
  145. this.startX = event.clientX;
  146. }
  147. this.startPosition = parseFloat(this.currentPosition);
  148. this.newPosition = this.startPosition;
  149. },
  150. onDragging(event) {
  151. if (this.dragging) {
  152. this.isClick = false;
  153. this.displayTooltip();
  154. this.$parent.resetSize();
  155. let diff = 0;
  156. if (event.type === 'touchmove') {
  157. event.clientY = event.touches[0].clientY;
  158. event.clientX = event.touches[0].clientX;
  159. }
  160. if (this.vertical) {
  161. this.currentY = event.clientY;
  162. diff = (this.startY - this.currentY) / this.$parent.sliderSize * 100;
  163. } else {
  164. this.currentX = event.clientX;
  165. diff = (this.currentX - this.startX) / this.$parent.sliderSize * 100;
  166. }
  167. this.newPosition = this.startPosition + diff;
  168. this.setPosition(this.newPosition);
  169. }
  170. },
  171. onDragEnd() {
  172. if (this.dragging) {
  173. /*
  174. * 防止在 mouseup 后立即触发 click,导致滑块有几率产生一小段位移
  175. * 不使用 preventDefault 是因为 mouseup 和 click 没有注册在同一个 DOM 上
  176. */
  177. setTimeout(() => {
  178. this.dragging = false;
  179. this.hideTooltip();
  180. if (!this.isClick) {
  181. this.setPosition(this.newPosition);
  182. this.$parent.emitChange();
  183. }
  184. }, 0);
  185. window.removeEventListener('mousemove', this.onDragging);
  186. window.removeEventListener('touchmove', this.onDragging);
  187. window.removeEventListener('mouseup', this.onDragEnd);
  188. window.removeEventListener('touchend', this.onDragEnd);
  189. window.removeEventListener('contextmenu', this.onDragEnd);
  190. }
  191. },
  192. setPosition(newPosition) {
  193. if (newPosition === null || isNaN(newPosition)) return;
  194. if (newPosition < 0) {
  195. newPosition = 0;
  196. } else if (newPosition > 100) {
  197. newPosition = 100;
  198. }
  199. const lengthPerStep = 100 / ((this.max - this.min) / this.step);
  200. const steps = Math.round(newPosition / lengthPerStep);
  201. let value = steps * lengthPerStep * (this.max - this.min) * 0.01 + this.min;
  202. value = parseFloat(value.toFixed(this.precision));
  203. this.$emit('input', value);
  204. this.$nextTick(() => {
  205. this.displayTooltip();
  206. this.$refs.tooltip && this.$refs.tooltip.updatePopper();
  207. });
  208. if (!this.dragging && this.value !== this.oldValue) {
  209. this.oldValue = this.value;
  210. }
  211. }
  212. }
  213. };
  214. </script>