09fb2750f49c2a73f1bf69001e956a74b4e7634b374f791daae38cc43b2759a4743ad1aefbd926a8979e5fef80ae9949c99d7ecb68af6acb11e00e823809b2 3.2 KB

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