cc704f6f1eb51c75948c2982d258ca16cf802ad4fd88cd7b5d0e22b1b17d54ffe07d0c492e7ce105e19818300e18125e54637ec0e33cab5efe09f76df37bf2 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <template>
  2. <div class="el-color-hue-slider" :class="{ 'is-vertical': vertical }">
  3. <div class="el-color-hue-slider__bar" @click="handleClick" ref="bar"></div>
  4. <div class="el-color-hue-slider__thumb"
  5. :style="{
  6. left: thumbLeft + 'px',
  7. top: thumbTop + 'px'
  8. }"
  9. ref="thumb">
  10. </div>
  11. </div>
  12. </template>
  13. <script>
  14. import draggable from '../draggable';
  15. export default {
  16. name: 'el-color-hue-slider',
  17. props: {
  18. color: {
  19. required: true
  20. },
  21. vertical: Boolean
  22. },
  23. data() {
  24. return {
  25. thumbLeft: 0,
  26. thumbTop: 0
  27. };
  28. },
  29. computed: {
  30. hueValue() {
  31. const hue = this.color.get('hue');
  32. return hue;
  33. }
  34. },
  35. watch: {
  36. hueValue() {
  37. this.update();
  38. }
  39. },
  40. methods: {
  41. handleClick(event) {
  42. const thumb = this.$refs.thumb;
  43. const target = event.target;
  44. if (target !== thumb) {
  45. this.handleDrag(event);
  46. }
  47. },
  48. handleDrag(event) {
  49. const rect = this.$el.getBoundingClientRect();
  50. const { thumb } = this.$refs;
  51. let hue;
  52. if (!this.vertical) {
  53. let left = event.clientX - rect.left;
  54. left = Math.min(left, rect.width - thumb.offsetWidth / 2);
  55. left = Math.max(thumb.offsetWidth / 2, left);
  56. hue = Math.round((left - thumb.offsetWidth / 2) / (rect.width - thumb.offsetWidth) * 360);
  57. } else {
  58. let top = event.clientY - rect.top;
  59. top = Math.min(top, rect.height - thumb.offsetHeight / 2);
  60. top = Math.max(thumb.offsetHeight / 2, top);
  61. hue = Math.round((top - thumb.offsetHeight / 2) / (rect.height - thumb.offsetHeight) * 360);
  62. }
  63. this.color.set('hue', hue);
  64. },
  65. getThumbLeft() {
  66. if (this.vertical) return 0;
  67. const el = this.$el;
  68. const hue = this.color.get('hue');
  69. if (!el) return 0;
  70. const thumb = this.$refs.thumb;
  71. return Math.round(hue * (el.offsetWidth - thumb.offsetWidth / 2) / 360);
  72. },
  73. getThumbTop() {
  74. if (!this.vertical) return 0;
  75. const el = this.$el;
  76. const hue = this.color.get('hue');
  77. if (!el) return 0;
  78. const thumb = this.$refs.thumb;
  79. return Math.round(hue * (el.offsetHeight - thumb.offsetHeight / 2) / 360);
  80. },
  81. update() {
  82. this.thumbLeft = this.getThumbLeft();
  83. this.thumbTop = this.getThumbTop();
  84. }
  85. },
  86. mounted() {
  87. const { bar, thumb } = this.$refs;
  88. const dragConfig = {
  89. drag: (event) => {
  90. this.handleDrag(event);
  91. },
  92. end: (event) => {
  93. this.handleDrag(event);
  94. }
  95. };
  96. draggable(bar, dragConfig);
  97. draggable(thumb, dragConfig);
  98. this.update();
  99. }
  100. };
  101. </script>