element-ui制作多颜色选择器

嚴華  金牌会员 | 2025-1-18 19:00:13 | 显示全部楼层 | 阅读模式
打印 上一主题 下一主题

主题 911|帖子 911|积分 2733


将颜色存储到一个数组中去。
  1. <template>
  2.   <div class="color-picker-container" style="margin-top: 10px;">
  3.     <!-- 显示已选颜色 -->
  4.     <div class="color-selection">
  5.       <div
  6.         v-for="(color, index) in selectedColors"
  7.         :key="index"
  8.         class="color-chip"
  9.         :style="{ backgroundColor: color }"
  10.       >
  11.         <button @click="removeColor(index)" class="remove-btn">X</button>
  12.       </div>
  13.       <!-- 颜色选择器 -->
  14.       <el-color-picker
  15.         v-if="showColorPicker"
  16.         v-model="currentColor"
  17.         @change="addColor"
  18.         show-alpha
  19.         size="small"
  20.         class="color-picker"
  21.       />
  22.       <div v-if="selectedColors.length < maxColors" class="add-color-btn" @click="showColorPicker = !showColorPicker">
  23.         +
  24.       </div>
  25.     </div>
  26.   </div>
  27. </template>
  28. <script>
  29. export default {
  30.   data() {
  31.     return {
  32.       selectedColors: ['#409EFF', '#67C23A', '#E6A23C', '#F56C6C', '#7265E6', '#EBEEF5'], // 默认6个颜色
  33.       currentColor: null,
  34.       showColorPicker: false,
  35.       maxColors: 10 // 最大可选颜色数量
  36.     };
  37.   },
  38.   methods: {
  39.     addColor(color) {
  40.       if (color && this.selectedColors.length < this.maxColors && !this.selectedColors.includes(color)) {
  41.         this.selectedColors.push(color);
  42.       }
  43.       this.showColorPicker = false;
  44.     },
  45.     removeColor(index) {
  46.       this.selectedColors.splice(index, 1);
  47.     },
  48.   },
  49. };
  50. </script>
  51. <style scoped>
  52. .color-picker-container {
  53.   position: relative;
  54. }
  55. .color-selection {
  56.   display: flex;
  57.   flex-wrap: wrap;
  58.   gap: 8px;
  59.   margin-bottom: 16px;
  60. }
  61. .color-chip {
  62.   width: 40px;
  63.   height: 40px;
  64.   border-radius: 8px; /* 圆角 */
  65.   position: relative;
  66.   cursor: pointer;
  67.   transition: transform 0.1s ease-in-out;
  68. }
  69. .color-chip:hover {
  70.   transform: scale(1.1); /* 悬停时放大 */
  71. }
  72. .remove-btn {
  73.   position: absolute;
  74.   top: -6px;
  75.   right: -6px;
  76.   background-color: white;
  77.   border: 1px solid #ccc;
  78.   border-radius: 50%;
  79.   width: 16px;
  80.   height: 16px;
  81.   line-height: 14px;
  82.   text-align: center;
  83.   font-size: 10px;
  84.   cursor: pointer;
  85.   display: none;
  86. }
  87. .color-chip:hover .remove-btn {
  88.   display: block;
  89. }
  90. .add-color-btn {
  91.   width: 40px;
  92.   height: 40px;
  93.   border-radius: 8px; /* 圆角 */
  94.   background-color: #f0f0f0;
  95.   border: 1px dashed #dcdcdc;
  96.   display: flex;
  97.   align-items: center;
  98.   justify-content: center;
  99.   font-size: 20px;
  100.   color: #8c8c8c;
  101.   cursor: pointer;
  102.   transition: background-color 0.3s ease;
  103. }
  104. .add-color-btn:hover {
  105.   background-color: #e0e0e0;
  106. }
  107. .color-picker {
  108.   /*position: absolute;*/
  109.   bottom: -5px;
  110.   left: 0;
  111. }
  112. </style>
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

嚴華

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表