index.vue 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /** * @Description: 渐变色配置 * @author liu.shiyi * @date 2023/4/13 16:01 */
  2. <template>
  3. <div class="bs-gradual-wrap">
  4. <el-color-picker
  5. v-model="startColor"
  6. class="bs-el-color-picker"
  7. popper-class="bs-el-color-picker"
  8. />
  9. <div :class="positionIcon" @click="positionChange" />
  10. <el-color-picker
  11. v-model="endColor"
  12. class="bs-el-color-picker"
  13. popper-class="bs-el-color-picker"
  14. />
  15. </div>
  16. </template>
  17. <script>
  18. export default {
  19. name: 'GradualSetting',
  20. model: {
  21. prop: 'colors',
  22. event: 'change'
  23. },
  24. props: {
  25. colors: {
  26. type: String,
  27. default: ''
  28. }
  29. },
  30. data () {
  31. return {
  32. startColor: '', // 初始颜色
  33. endColor: '', // 终止颜色
  34. position: '', // 渐变方向
  35. colorsValue: '',// 拼接后的符合g2语法的颜色值
  36. positionList: ['l(0)', 'l(90)', 'l(180)', 'l(270)'],
  37. positionIconList: {
  38. 'l(0)': 'el-icon-right',
  39. 'l(90)': 'el-icon-bottom',
  40. 'l(180)': 'el-icon-back',
  41. 'l(270)': 'el-icon-top'
  42. },
  43. positionIcon: 'el-icon-right'
  44. }
  45. },
  46. computed: {
  47. },
  48. watch: {
  49. startColor () {
  50. this.colorChange()
  51. },
  52. endColor () {
  53. this.colorChange()
  54. }
  55. },
  56. mounted () {
  57. this.init()
  58. },
  59. methods: {
  60. init () {
  61. // color 格式是 'l(0) 0:#ffffff 1:#000000'
  62. const arr = this.colors?.split(' ') || []
  63. this.position = arr[0] || 'l(0)'
  64. this.positionIcon = this.positionIconList[this.position] || 'el-icon-right'
  65. const s = arr[1].split(':')[1] && arr[1].split(':')[1] !== 'null' ? arr[1].split(':')[1] : ''
  66. const e = arr[2].split(':')[1] && arr[2].split(':')[1] !== 'null' ? arr[2].split(':')[1] : ''
  67. this.startColor = s
  68. this.endColor = e
  69. },
  70. colorChange (val) {
  71. this.colorsValue = `${this.position} 0:${this.startColor} 1:${this.endColor}`
  72. this.$emit('change', this.colorsValue)
  73. },
  74. positionChange(){
  75. // 将position的值循环移动一位
  76. const index = this.positionList.indexOf(this.position)
  77. this.position = this.positionList[(index + 1) % 4]
  78. this.positionIcon = this.positionIconList[this.position]
  79. this.colorChange()
  80. },
  81. }
  82. }
  83. </script>
  84. <style lang="scss" scoped>
  85. .bs-gradual-wrap{
  86. width: 100%;
  87. display: flex;
  88. align-items: center;
  89. .el-icon-right{
  90. width: 40px;
  91. text-align: center;
  92. cursor: pointer;
  93. }
  94. .el-icon-bottom{
  95. width: 40px;
  96. text-align: center;
  97. cursor: pointer;
  98. }
  99. .el-icon-back{
  100. width: 40px;
  101. text-align: center;
  102. cursor: pointer;
  103. }
  104. .el-icon-top {
  105. width: 40px;
  106. text-align: center;
  107. cursor: pointer;
  108. }
  109. }
  110. </style>