自定义颜色选择功能

开箱即用

1.效果:

2.代码

复制代码
<template>
	<div class="snowy-color-picker" @click="forceResize">
		<color-picker v-bind="$attrs" format="hex" :pureColor="props.value" @update:pureColor="update" />
	</div>
</template>

<script setup>
	import { ColorPicker } from 'vue3-colorpicker'
	import 'vue3-colorpicker/style.css'

	const emit = defineEmits(['update:value'])

	const props = defineProps({
		value: {
			type: String,
			default: '#1677FF'
		}
	})

	const forceResize = () => {
		window.dispatchEvent(new Event('resize'))
	}

	const update = (val) => {
		showTxt(val)
		emit('update:value', val)
	}
	onMounted(() => {
		showTxt(props.value)
	})
	const showTxt = (val) => {
		const currentColor = document.querySelector('.current-color')
		if (currentColor) {
			currentColor.textContent = val
		}
	}
</script>

<style lang="less">
	.snowy-color-picker {
		.vc-color-wrap {
			width: auto;
		}
		.current-color {
			color: #fff;
			padding: 0 10px;
		}
	}
</style>

使用:

复制代码
<color-picker v-model:value="formData.color" />
相关推荐
风之舞_yjf几秒前
Vue基础(26)_单文件组件
前端·vue.js
weixin_422201301 分钟前
Element Plus中el-tree组件默认选中第一个节点的实现方法
前端·javascript·vue.js
_OP_CHEN2 分钟前
【前端开发之CSS】(六)CSS 弹性布局(Flex)完全指南:从入门到精通,搞定所有布局需求
前端·css·html·flex布局·弹性布局·界面美化·页面开发
LuminescenceJ3 分钟前
RPC通信中的Context上下文如何跨进程传递消息,gRPC为例分析
开发语言·网络·后端·网络协议·rpc·golang
雄狮少年3 分钟前
简单react agent(没有抽象成基类、子类,直接用)--- langgraph workflow版 ------demo1
前端·python·react.js
css趣多多4 分钟前
组件没有原生事件
前端·javascript·vue.js
IT陈图图5 分钟前
Flutter × OpenHarmony 实战:优雅构建确认对话框的组件化方案
开发语言·javascript·flutter
雨季6666 分钟前
Flutter 三端应用实战:OpenHarmony 简易文本末尾字符查看器开发指南
开发语言·javascript·flutter
小小弯_Shelby6 分钟前
el-form表单简洁有效地解决新增与查看详情共用一个页面问题,控制el-form的disabled设置和修改样式
前端·vue.js·elementui
Lxinccode6 分钟前
python(70) : 网页IDE
开发语言·ide·python·网页ide