使用 Vue-Color
目录
本篇博客提供一个实用的例子,以及例子对应的代码还有该组件其他类型的选择器的Demo
Vue-Color实战
npm install vue-color --save
结合ElementUI 实现如下效果
下面是具体代码
引入依赖
import {Sketch} from "vue-color";
引入组件
components: {"sketch-picker": Sketch},
组件代码
<el-popover
placement="bottom"
width="200"
trigger="click"
content="这是一段内容,这是一段内容,这是一段内容,这是一段内容。">
<el-button slot="reference" class="color-box"
:style="{ backgroundColor: OtherForm.colors.color,padding: '0px' }">
<div style="width: 100%;height:100%;border: 2px solid gainsboro">
<i style="font-size: 25px; font-weight: lighter;line-height: 34px; color: white" class="el-icon-arrow-down icon">
</i>
</div>
</el-button>
<sketch-picker v-model="OtherForm.colors.color" @input="colorValueChange"/>
</el-popover>
数据格式
起作用的是colors
OtherForm: {
ID: "",
FileName: "",
colors: {
color: "",
hex: "#194d33",
hsl: {h: 150, s: 0.5, l: 0.2, a: 1},
hsv: {h: 150, s: 0.66, v: 0.3, a: 1},
rgba: {r: 25, g: 77, b: 51, a: 1},
a: 1
}
},
颜色更新方法
// 颜色值改变事件处理
colorValueChange(val) {
// console.log(val)
// 取颜色对象的十六进制值
this.OtherForm.colors.color = val.hex
},
覆盖ElementUI的CSS样式
不要加 scoped 加了就覆盖不了了
我放到APP里了
.vc-sketch {
position: relative;
width: 200px;
padding: 0 0 0;
box-sizing: initial;
background: #fff;
border-radius: 4px;
box-shadow: 0 0 0 0 transparent, 0 0 0 transparent;
}
.el-popper[x-placement^=bottom] {
margin-top: 0;
padding: 0;
}
.el-popover {
position: absolute;
background: white;
min-width: 150px;
border-radius: 4px;
border: 0 solid white;
padding: 0px;
z-index: 2000;
color: #606266;
line-height: 1.4;
text-align: justify;
font-size: 14px;
box-shadow: 0 2px 12px 0 rgba(0,0,0,.0);
word-break: break-all;
}
.el-popover--plain {
padding: 20px;
}
该组件的一些其他颜色选择器Demo以及代码
该组件所有类型颜色选取的展示效果图
<template>
<div class="color">
<div> 1.material
<material-picker v-model="colors" />
</div>
<div> 2.compact
<compact-picker v-model="colors" />
</div>
<div> 3.swatches
<swatches-picker v-model="colors" />
</div>
<div> 4.slider
<slider-picker v-model="colors" />
</div>
<div> 5.sketch
<sketch-picker v-model="colors" />
</div>
<div> 6.chrome
<chrome-picker v-model="colors" />
</div>
<div> 7.photoshop
<photoshop-picker v-model="colors" />
</div>
</div>
</template>
<script>
import {
Material,
Compact,
Swatches,
Slider,
Photoshop,
Chrome,
Sketch,
Twitter,
Grayscale
} from "vue-color";
export default {
components: {
"material-picker": Material,
"compact-picker": Compact,
"swatches-picker": Swatches,
"slider-picker": Slider,
"photoshop-picker": Photoshop,
"chrome-picker": Chrome,
"sketch-picker": Sketch,
"twitter-picker": Twitter,
"grayscale-picker": Grayscale
},
data() {
return {
/* 颜色选择器 */
colors: {
color: "",
hex: "#194d33",
hsl: { h: 150, s: 0.5, l: 0.2, a: 1 },
hsv: { h: 150, s: 0.66, v: 0.3, a: 1 },
rgba: { r: 25, g: 77, b: 51, a: 1 },
a: 1
}
};
},
mounted() {},
methods: {
/* 颜色选择器 */
updateValue(value) {
console.log(value);
console.log(this.color);
}
}
};
</script>
<style lang="less" scoped>
.color {
display: flex;
flex-wrap: wrap;
text-align: center;
div {
margin: 20px;
font-size: 18px;
font-weight: bold;
border: 1px solid #eee;
padding: 10px;
}
}
</style>