自己开源的vue2颜色选择器

复制代码
<template>
    <div> 
      <a-popover overlayClassName="color-popover" placement="bottomLeft" trigger="click" v-model="visible">
          <template slot="content">
            <a-icon type="close-square" theme="filled" class="color-close" @click="visible = false"/>
            <sketch-picker 
                :value="colorRbgaValue"
                @input="changeColor"
                :palette="defaultColors"
            />
          </template>
          <div class="ant-input color-input" :style="{ width, height }" @click="visible = true">
            <div class="color-area" :style="{ backgroundColor: colorRbgaValue }"></div>
          </div>
       </a-popover>
    </div>
</template>
<script>
import { Sketch } from 'vue-color';
export default {
  name: 'colorPicker',
  components: {
     'sketch-picker': Sketch
  },
  props: {
    value: {
      type: String,
      default: '#000' //hex值
    },
    alpha:{
      type: String || Number,
      default: '1'
    },
    width: {
      type: String,
      default: '88px' //色块宽度
    },
    height: {
      type: String,
      default: '32px' //色块高度
    }
  },
  data() {
    return {
        colorRbgaValue: 'rbga(0,0,0)',
        visible: false,
        defaultColors:[
            '#000000', '#FFFFFF', '#FF1900', '#F47365', '#FFB243', 
            '#FFE623', '#6EFF2A', '#1BC7B1', '#00BEFF', 
            '#2E81FF', '#5D61FF', '#FF89CF', '#FC3CAD', 
            '#BF3DCE', '#8E00A7', 'rgba(0,0,0,0)'
        ],
    };
  },
  mounted() {
    const {r, g, b} = this.hex2rgb(this.value);
    this.colorRbgaValue = `rgba(${r}, ${g}, ${b}, ${this.alpha !== null ? this.alpha : '1'})`;
  },
  methods: {
      changeColor(color) {
        const { r, g, b, a } = color.rgba;
        this.colorRbgaValue = `rgba(${r}, ${g}, ${b}, ${a})`;
        this.$emit('update:value', color.hex);
        this.$emit('update:alpha', a.toString());
      },
      hex2rgb(hex) {
          hex = hex.slice(1)
          const change = val => parseInt(val, 16) || 0 // 避免NaN的情况
          return {
              r: change(hex.slice(0, 2)),
              g: change(hex.slice(2, 4)),
              b: change(hex.slice(4, 6))
          }
      }, 
  }
};
</script>
<style lang="scss">
.color-popover {
  .ant-popover-inner-content {
    padding: 0;
  }
  .ant-popover-arrow {
    z-index:1;
  }
  .vc-sketch {
    padding: 14px 10px 0;
  }
}
</style>
<style lang="scss" scoped>
.color-close {
  position: absolute;
  right: 0;
  color: #9d9d9d;
  font-size: 12px;
  z-index: 2;
}
.color-area {
   width: 98%;
   height: 98%;
   border:1px solid #d9d9d9;
}
</style>
复制代码
 <color-picker width="88px" 
                  :alpha.sync="Transparency"
                  :value.sync="borderColor"/> 

 

posted @   创业男生  阅读(279)  评论(0编辑  收藏  举报
(评论功能已被禁用)
相关博文:
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· [AI/GPT/综述] AI Agent的设计模式综述
历史上的今天:
2016-05-19 css样式重置方案 -解决浏览器差异
点击右上角即可分享
微信分享提示