当前位置: 首页 > news >正文

vue自定义颜色选择器

vue自定义颜色选择器

效果图:
在这里插入图片描述

step0: 默认写法 调用系统自带的颜色选择器

       <input type="color">

step1:C:\Users\wangrusheng\PycharmProjects\untitled18\src\views\Home.vue

<template>
  <div class="container">
    <!-- 颜色选择器组件 -->
    <ColorPicker v-model="selectedColor" />

    <!-- 新增的动态背景按钮 -->
    <div>

    <button
      class="dynamic-button"
      :style="{ backgroundColor: selectedColor }"
    >
      我的背景色会变化!
    </button>

       <input type="color">

    <p>当前选中颜色: {{ selectedColor }}</p>

    </div>
     </div>
</template>

<script>
import ColorPicker from './ColorPicker.vue'

export default {
  components: { ColorPicker },
  data() {
    return {
      selectedColor: '#ff0000' // 默认颜色
    }
  }
}
</script>



step2:C:\Users\wangrusheng\PycharmProjects\untitled18\src\views\ColorPicker.vue

<template>
  <div class="color-picker">
    <!-- 饱和度/明度选择区域 -->
    <div
      class="saturation"
      :style="{ backgroundColor: `hsl(${hsv.h}, 100%, 50%)` }"
      @mousedown="startDrag"
    >
      <div
        class="selector"
        :style="{
          left: `${hsv.s * 100}%`,
          top: `${(1 - hsv.v) * 100}%`,
          backgroundColor: currentColor
        }"
      ></div>
    </div>

    <!-- 色相滑块 -->
    <div class="hue-slider" @mousedown="startHueDrag">
      <div
        class="hue-pointer"
        :style="{ left: `${(hsv.h / 360) * 100}%` }"
      ></div>
    </div>

    <!-- 颜色显示和输入 -->
    <div class="color-preview" :style="{ backgroundColor: currentColor }"></div>
    <input
      v-model="hexColor"
      class="hex-input"
      placeholder="#FFFFFF"
      @input="handleHexInput"
    >
  </div>
</template>

<script>
export default {
  props: {
    modelValue: String
  },
  emits: ['update:modelValue'],
  data() {
    return {
      hsv: { h: 0, s: 1, v: 1 },
      hexColor: '#ff0000',
      isDragging: false,
      isHueDragging: false
    }
  },
  computed: {
    currentColor() {
      return this.hsvToHex(this.hsv)
    }
  },
  methods: {
    startDrag(e) {
      this.isDragging = true
      this.handleDrag(e)
      window.addEventListener('mousemove', this.handleDrag)
      window.addEventListener('mouseup', this.stopDrag)
    },
    startHueDrag(e) {
      this.isHueDragging = true
      this.handleHueDrag(e)
      window.addEventListener('mousemove', this.handleHueDrag)
      window.addEventListener('mouseup', this.stopHueDrag)
    },
    handleDrag(e) {
      if (!this.isDragging) return
      const rect = e.target.getBoundingClientRect()
      const x = Math.max(0, Math.min(1, (e.clientX - rect.left) / rect.width))
      const y = Math.max(0, Math.min(1, (e.clientY - rect.top) / rect.height))

      this.hsv.s = x
      this.hsv.v = 1 - y
      this.updateHex()
    },
    handleHueDrag(e) {
      if (!this.isHueDragging) return
      const rect = e.target.getBoundingClientRect()
      const x = Math.max(0, Math.min(1, (e.clientX - rect.left) / rect.width))
      this.hsv.h = x * 360
      this.updateHex()
    },
    stopDrag() {
      this.isDragging = false
      window.removeEventListener('mousemove', this.handleDrag)
      window.removeEventListener('mouseup', this.stopDrag)
    },
    stopHueDrag() {
      this.isHueDragging = false
      window.removeEventListener('mousemove', this.handleHueDrag)
      window.removeEventListener('mouseup', this.stopHueDrag)
    },
    updateHex() {
      this.hexColor = this.hsvToHex(this.hsv)
      this.$emit('update:modelValue', this.hexColor)
    },
    handleHexInput() {
      if (/^#([0-9A-F]{3}){1,2}$/i.test(this.hexColor)) {
        this.hsv = this.hexToHsv(this.hexColor)
      }
    },
    // 颜色转换函数
    hsvToHex(hsv) {
      const h = hsv.h / 360
      let r, g, b

      const i = Math.floor(h * 6)
      const f = h * 6 - i
      const p = hsv.v * (1 - hsv.s)
      const q = hsv.v * (1 - f * hsv.s)
      const t = hsv.v * (1 - (1 - f) * hsv.s)

      switch (i % 6) {
        case 0: r = hsv.v, g = t, b = p; break
        case 1: r = q, g = hsv.v, b = p; break
        case 2: r = p, g = hsv.v, b = t; break
        case 3: r = p, g = q, b = hsv.v; break
        case 4: r = t, g = p, b = hsv.v; break
        case 5: r = hsv.v, g = p, b = q; break
      }

      return `#${[r, g, b]
        .map(x => Math.round(x * 255)
        .toString(16)
        .padStart(2, '0'))
        .join('')}`
    },
    hexToHsv(hex) {
      // 转换逻辑(此处省略具体实现)
      // 返回类似 {h: 0, s: 1, v: 1} 的HSV对象
    }
  }
}
</script>

<style>
.color-picker {
  width: 300px;
  padding: 20px;
  background: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 10px rgba(0,0,0,0.1);
}

.saturation {
  position: relative;
  width: 100%;
  height: 200px;
  border-radius: 4px;
  background: linear-gradient(to top, #000, transparent),
              linear-gradient(to right, #fff, transparent);
}

.selector {
  position: absolute;
  width: 16px;
  height: 16px;
  border: 2px solid white;
  border-radius: 50%;
  transform: translate(-8px, -8px);
  box-shadow: 0 1px 3px rgba(0,0,0,0.3);
}

.hue-slider {
  position: relative;
  height: 12px;
  margin: 15px 0;
  background: linear-gradient(to right,
    #ff0000 0%,
    #ffff00 17%,
    #00ff00 33%,
    #00ffff 50%,
    #0000ff 67%,
    #ff00ff 83%,
    #ff0000 100%);
  border-radius: 6px;
}

.hue-pointer {
  position: absolute;
  width: 16px;
  height: 16px;
  background: white;
  border-radius: 50%;
  transform: translate(-8px, -2px);
  box-shadow: 0 1px 3px rgba(0,0,0,0.3);
}

.color-preview {
  width: 40px;
  height: 40px;
  border-radius: 4px;
  border: 1px solid #ddd;
}

.hex-input {
  margin-left: 10px;
  padding: 8px;
  width: 100px;
  border: 1px solid #ddd;
  border-radius: 4px;
}
</style>

end

相关文章:

  • 接口请求控制工具
  • Modules模块NamespaceManagement命名空间管理
  • AI应用企业研发方案
  • 旋转位置编码
  • OpenHarmony-5.0.0-Risc-V架构搭建DeepSeek-R1
  • SS Block
  • Android Coil 3默认P3色域图加载/显示不出来
  • STM32 模块化开发指南 · 第 5 篇 STM32 项目中断处理机制最佳实践:ISR、回调与事件通知
  • Windows 录音格式为什么是 M4A?M4A 怎样转为 MP3 格式
  • 面向对象的需求分析与UML构造块详解
  • 设计模式:里氏代换原则 - 继承设计的稳定之道
  • 搜索插入位置 -- 二分查找
  • 每日一题(小白)暴力娱乐篇29
  • 新能源车「大三电」与「小三电」
  • GitLab之搭建(Building GitLab)
  • 【数据结构】堆排序详细图解
  • Python实现浏览器模拟访问及页面解析的全面指南
  • 智能自动化管理系统
  • 3.0/Q2,Charls最新文章解读
  • 自动化测试常用函数
  • 政府网站建设招标方案/武汉新闻最新消息
  • 帝国网站建设/搜索引擎优化培训中心
  • 网站开发业务怎么开展/泰州seo排名扣费
  • 网站建设 目标/晚上国网app
  • 外贸网站友情链接/免费关键词优化工具
  • 安庆网站建设服务网/高权重外链