Vue實(shí)現(xiàn)色板功能的示例代碼
效果:

動(dòng)態(tài)添加顏色 隨機(jī)色

代碼:
<div
class="mt-10 firstTitle"
v-show="pictureType != 'card' && pictureType != 'table' && pictureType != 'inventory'"
>
<i
:class="[colorSystemShow ? 'el-icon-com-xiajiantou' : 'el-icon-com-youjiantou']"
@click="colorSystem_before"
class="c-pointer"
></i>
<span class="ml-5">色板</span>
<span class="c-pointer pull-right" @click="colorConfig">配置</span>
<div v-show="colorSystemShow" class="mt-10 secondTitle colorSystem pl-5">
<div v-for="itemOut in colorList" style="height:20px;" class="mt-5">
<span
v-for="itemIn in itemOut.value"
:style="{background:itemIn}"
style="display:inline-block;width:13.5px;height:20px;"
></span>
</div>
</div>
</div>組件:
<template>
<el-dialog
title="色板配置"
:visible.sync="isColorConfigDialog"
width="690px"
:close-on-click-modal="false"
:show-close="false"
top="90px"
>
<div class="mainDiv">
<el-button class="ml-10" @click="add()" type="primary" size="mini" icon="el-icon-plus">新增色板</el-button>
<div v-for="(itemOut,i) in colorListCopy" style="height:20px;" class="mt-5">
<span>{{i+1}}、</span>
<span
v-for="(itemIn,index) in itemOut.value"
:style="{background:itemIn}"
style="display:inline-block;height:20px;"
>
<el-input
style="width:30px;"
v-model="aaa"
:value="itemIn"
type="color"
@change="colorChange($event,i,index)"
></el-input>
</span>
<i class="el-icon-delete ml-10 mt-3 font16 c-pointer" @click="del(i)" title="刪除"></i>
<!-- <span class="pull-right" style="margin-top:3px;">
</span>-->
</div>
</div>
<span slot="footer" class="dialog-footer">
<el-button size="mini" type="primary" @click="save">確 定</el-button>
<el-button size="mini" @click="exit">取 消</el-button>
</span>
</el-dialog>
</template>
<script>
export default {
name: 'colorConfigDialog',
props: ['isColorConfigDialog', 'colorList'],
data() {
return {
aaa: '',
colorListCopy: []
}
},
mounted() {
this.colorListCopy = JSON.parse(JSON.stringify(this.colorList))
},
methods: {
save() {
this.$emit('saveColorConfig', this.colorListCopy)
this.$emit('closeColorConfigDialog')
},
exit() {
this.$emit('closeColorConfigDialog')
},
colorChange(e, i, index) {
this.aaa = e
this.colorListCopy[i].value[index] = e
this.$forceUpdate()
},
color16() {
//十六進(jìn)制顏色隨機(jī)
const r = Math.floor(Math.random() * 256)
const g = Math.floor(Math.random() * 256)
const b = Math.floor(Math.random() * 256)
const color = `#${r.toString(16)}${g.toString(16)}${b.toString(16)}`
return color
},
// 添加
add() {
let arr = []
for (const item in this.colorListCopy[0].value) {
let color = this.color16()
arr.push(color)
}
let inParamsNew = [...this.colorListCopy]
let obj = {
key: this.colorListCopy.length,
value: arr
}
inParamsNew.push(obj)
this.colorListCopy = inParamsNew
},
del(index) {
let inParamsNew = [...this.colorListCopy]
if (inParamsNew.length < 2) {
this.$message({
type: 'warning',
duration: '2000',
message: '必須保留一行'
})
return
}
inParamsNew.splice(index, 1)
this.colorListCopy = inParamsNew
}
}
}
</script>
<style lang="less" rel="stylesheet/less" scoped="true">
@import url('../../../common/less/variable.less');
.mainDiv {
height: calc(100vh - 260px);
overflow: auto;
}
/deep/input {
border: 0; // 去除未選中狀態(tài)邊框
outline: none; // 去除選中狀態(tài)邊框
background-color: rgba(0, 0, 0, 0); // 透明背景
}
/deep/input[type='"color"']::-webkit-color-swatch-wrapper {
padding: 0;
}
/deep/input[type='color']::-webkit-color-swatch {
border: 0;
}
/deep/.el-input--mini .el-input__inner {
height: 20px;
}
</style>數(shù)據(jù):
colorList: [
{
key: 1,
value: ["#5B8FF9","#CDDDFD","#61DDAA","#CDF3E4","#65789B","#CED4DE","#F6BD16","#FCEBB9","#7262fd","#D3CEFD","#78D3F8","#D3EEF9","#9661BC","#DECFEA","#F6903D","#FFE0C7","#008685","#BBDEDE","#F08BB4","#FFE0ED"]
},
{
key: 2,
value: ["#FF6B3B","#626681","#FFC100","#9FB40F","#76523B","#DAD5B5","#0E8E89","#E19348","#F383A2","#247FEA","#2BCB95","#B1ABF4","#1D42C2","#1D9ED1","#D64BC0","#255634","#8C8C47","#8CDAE5","#8E283B","#791DC9"]
},
{
key: 3,
value: ["#025DF4","#DB6BCF","#2498D1","#BBBDE6","#4045B2","#21A97A","#FF745A","#007E99","#FFA8A8","#2391FF","#FFC328","#A0DC2C","#946DFF","#626681","#EB4185","#CD8150","#36BCCB","#327039","#803488","#83BC99"]
},
{
key: 4,
value: ["#FF4500","#1AAF8B","#406C85","#F6BD16","#B40F0F","#2FB8FC","#4435FF","#FF5CA2","#BBE800","#FE8A26","#946DFF","#6C3E00","#6193FF","#FF988E","#36BCCB","#004988","#FFCF9D","#CCDC8A","#8D00A1","#1CC25E"]
}
]到此這篇關(guān)于Vue實(shí)現(xiàn)色板功能的示例代碼的文章就介紹到這了,更多相關(guān)Vue色板內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue-cli對(duì)element-ui組件進(jìn)行二次封裝的實(shí)戰(zhàn)記錄
組件類(lèi)似于需要多個(gè)地方用到的方法,在Vue中組件就是一種復(fù)用(經(jīng)常使用)一個(gè)功能的手段,下面這篇文章主要給大家介紹了關(guān)于Vue?element?ui二次封裝的相關(guān)資料,需要的朋友可以參考下2022-06-06
vue學(xué)習(xí)筆記之Vue中css動(dòng)畫(huà)原理簡(jiǎn)單示例
這篇文章主要介紹了vue學(xué)習(xí)筆記之Vue中css動(dòng)畫(huà)原理,結(jié)合簡(jiǎn)單實(shí)例形式分析了Vue中css樣式變換動(dòng)畫(huà)效果實(shí)現(xiàn)原理與相關(guān)操作技巧,需要的朋友可以參考下2020-02-02
簡(jiǎn)單聊一聊axios配置請(qǐng)求頭content-type
最近在工作中碰到一個(gè)問(wèn)題,后端提供的get請(qǐng)求的接口需要在request header設(shè)置,下面這篇文章主要給大家介紹了關(guān)于axios配置請(qǐng)求頭content-type的相關(guān)資料,需要的朋友可以參考下2022-04-04
Element Carousel 走馬燈的具體實(shí)現(xiàn)
這篇文章主要介紹了Element Carousel 走馬燈的具體實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07
基于better-scroll 實(shí)現(xiàn)歌詞聯(lián)動(dòng)功能的代碼
BetterScroll 是一款重點(diǎn)解決移動(dòng)端(已支持 PC)各種滾動(dòng)場(chǎng)景需求的插件,BetterScroll 是使用純 JavaScript 實(shí)現(xiàn)的,這意味著它是無(wú)依賴的。本文基于better-scroll 實(shí)現(xiàn)歌詞聯(lián)動(dòng)功能,感興趣的朋友跟隨小編一起看看吧2020-05-05

