使用VUE3实现简单颜色盘,吸管组件,useEyeDropper和<input type=“color“ />的使用

1.使用vueuse中的useEyeDropper来实现滴管的功能和使用input中的type="color"属性来实现颜色盘

 效果:

图标触发吸管

input触发颜色盘
 

组件代码部分 :<dropper>  ----  vueuse使用

<template>
    <div class="sRGBHexWrap fbc">
        <span class="iconStyle fec" @click="handleOpen">
            <el-icon :size="20">
                <EditPen />
            </el-icon>
        </span>

        <span class="colorSpan">
            <input type="color" :value="defaultValue" @input="updateColor" class="color" v-if="showInput" />
        </span>

    </div>
</template>

<script setup>

import { useEyeDropper } from '@vueuse/core'
const { open, sRGBHex } = useEyeDropper()
// 引入 Vue 相关的 API
import { ref, watch, onMounted } from "vue";

// 定义组件的 props 和 emits
const props = defineProps(['modelValue'])
const emit = defineEmits(["update:modelValue"]);
//默认颜色显示
let defaultValue = '#4EAF31' //默认展示的颜色,使用ref无法触发初始化显示
let showInput = ref(true)   //因为defaultValue不是ref所以需要手动刷新dom
let Value = ref(null) //派发的颜色

//获取颜色盘的颜色
const getColor = (newValue) => {
    showInput.value = false
    defaultValue = newValue
    Value.value = newValue;
    showInput.value = true
};

//监听接受的值然后进行复制
watch(() => props.modelValue, async (newValue) => {
    if (newValue) {
        getColor(newValue)
    }
}, { immediate: true })

//监听滴管颜色
watch(sRGBHex, async (newmodelValue) => {
    if (newmodelValue) {
        getColor(newmodelValue)
    }
})

//监听值的变化
watch(Value, async (newValue) => {
    if (newValue) {
        emit("update:modelValue", newValue);
    }
})

//获取颜色盘的颜色
const updateColor = (event) => {
    Value.value = event.target.value;
};

//处理打开滴管时候按Esc按钮报错
const handleOpen = () => {
    try {
        open();
    } catch (error) {
        console.error('Error while opening EyeDropper:', error);
    }
};


</script>

<style lang="scss" scoped>
.color {
    background: var(--background-color2);
    outline: none;
    box-shadow: none;
    border: none;
}

.sRGBHexWrap {
    width: 100%;
    height: 100%;
    /* background-color: aliceblue; */

}

.iconStyle {
    width: 100%;
    height: 100%;
    padding: 0px 10px;
    cursor: pointer;
}

.colorSpan {
    cursor: pointer;
    width: 50%;
    height: 50%;
    border-radius: 5px;
}

input {
    padding: 0px;
    margin: 0px;
}
</style>

使用组件<dropper>

<dropper v-model="VRColor"></dropper>

 

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
你可以在主页中使用动态组件实现点击图片列表跳转到其余两页的功能,具体实现步骤如下: 1. 在主页中创建一个包含三个按钮的列表,分别对应三个组件(包括goodslist组件和另外两个组件)。 2. 使用Vue Router来实现页面跳转功能,配置路由表,并将三个组件分别对应到不同的路由路径。 3. 在主页中使用动态组件<router-view>,并通过路由跳转来实现展示不同的组件。 以下是示例代码: ``` <template> <div> <ul> <li @click="toGoodsList">跳转到GoodsList组件</li> <li @click="toComponent1">跳转到Component1组件</li> <li @click="toComponent2">跳转到Component2组件</li> </ul> <router-view></router-view> </div> </template> <script> import GoodsList from './GoodsList.vue' import Component1 from './Component1.vue' import Component2 from './Component2.vue' export default { components: { GoodsList, Component1, Component2 }, methods: { toGoodsList() { this.$router.push('/goodslist') }, toComponent1() { this.$router.push('/component1') }, toComponent2() { this.$router.push('/component2') } } } </script> ``` 在路由表中,你需要定义三个路由路径,分别对应三个组件: ``` import GoodsList from './GoodsList.vue' import Component1 from './Component1.vue' import Component2 from './Component2.vue' const routes = [ { path: '/goodslist', component: GoodsList }, { path: '/component1', component: Component1 }, { path: '/component2', component: Component2 } ] export default new VueRouter({ routes }) ``` 当你点击列表中的某个选项时,路由会根据对应的路径跳转到相应的组件,并在<router-view>中渲染该组件
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

码农六六

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值