【VUE】ElementPlus之动态主题色调切换(Vue3 + Element Plus+Scss + Pinia)

前言

关于ElementPlus的基础主题色自定义可以参阅《【VUE】ElementPlus之自定义主题样式和命名空间》

有了上面基础的了解,我们知道ElementPlus的主题色调是基于CSS3变量特性进行全局控制的,
那么接下来我们也基于CSS3变量来实现主题色调的动态切换效果;

主要控制的色调类型有:primarysuccesswarningdangererrorinfo
针对这六个色调类型分别进行035789级的渐变色定制

以下是默认情况下的主题颜色定义:

ElementPlus默认主题颜色

接下来,我们基于以下环境来实操下:

  • vue: ^3.3.4
  • vite: ^4.4.11
  • sass: ^1.58.3
  • element-plus: ^2.3.4
  • pinia: ^2.1.7

实现

默认主题色下的按钮组件色调:
在这里插入图片描述

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

在实现具体交互之前,我们先准备几个辅助小工具

颜色状态管理器

既然是动态切换,那么我们就需要一个容器来记录当下的一些色调信息,便于整体性的调整
以下仅抛个砖,具体业务调整根据自己需要来哈~

import { defineStore } from "pinia";
import ColorUnit from "@/unit/ColorUnit";

export const useColorStore = defineStore("color", () => {
  function setThemeColor(colorMap) {
    let _namespace = "el";
    colorMap.forEach((colorItem) => {
      setPropertyColor(`--${_namespace}-color-${colorItem[0]}`, colorItem[1]);
      themeColorGradient(`--${_namespace}-color-${colorItem[0]}-light-#level#`,"lighten",colorItem[1]);
      setPropertyColor(`--${_namespace}-color-${colorItem[0]}-dark-2`,colorItem[1],"darken");
      // themeColorGradient(`--${_namespace}-color-${colorItem[0]}-dark-#level#`,"darken",colorItem[1]);
    });
  }

  /**
   * 将css3变量设置到document中方便全局调用
   */
  function setPropertyColor(varName, color, funName, level) {
    level = level ? level : 0;
    funName = funName ? funName : "lighten";
    document.documentElement.style.setProperty(
      varName,
      ColorUnit[funName](color, level / 10)
    );
  }

  /**
   * 生成主色的其余渐变色并修改对应CSS3变量值
   */
  function themeColorGradient(varName, funName, themeColor, themeLevel) {
    themeColor = themeColor ? themeColor : '#409eff';
    themeLevel = themeLevel ? themeLevel : [3, 5, 7, 8, 9];

    themeLevel.forEach(function (level) {
      setPropertyColor(
        varName.replace("#level#", level),
        themeColor,
        funName,
        level
      );
    });
  }

  return {
    setThemeColor,
  };
});

颜色编码生成工具

根据前言描述,我们得知,需要根据一个十六进制的色值,生成其余的渐变色值出来,手动配置的话就太麻烦了
所以我们先来封装一个ColorUnit工具来辅助我们进行色调的配置

// file: src/unit/ColorUnit.js
// 代码载取来至:https://gitee.com/lolicode/scui/blob/master/src/utils/color.js
export default {
	//hex颜色转rgb颜色
	HexToRgb(str) {
		str = str.replace("#", "")
		var hxs = str.match(/../g)
		for (var i = 0; i < 3; i++) hxs[i] = parseInt(hxs[i], 16)
		return hxs
	},
	//rgb颜色转hex颜色
	RgbToHex(a, b, c) {
		var hexs = [a.toString(16), b.toString(16), c.toString(16)]
		for (var i = 0; i < 3; i++) {
			if (hexs[i].length == 1) hexs[i] = "0" + hexs[i]
		}
		return "#" + hexs.join("");
	},
	//加深
	darken(color, level) {
		var rgbc = this.HexToRgb(color)
		for (var i = 0; i < 3; i++) rgbc[i] = Math.floor(rgbc[i] * (1 - level))
		return this.RgbToHex(rgbc[0], rgbc[1], rgbc[2])
	},
	//变淡
	lighten(color, level) {
		var rgbc = this.HexToRgb(color)
		for (var i = 0; i < 3; i++) rgbc[i] = Math.floor((255 - rgbc[i]) * level + rgbc[i])
		return this.RgbToHex(rgbc[0], rgbc[1], rgbc[2])
	}
}

整合

有了上面俩个帮手,现在整合起来的具体应用

template部分

<template>
  <main>
    <el-row style="margin-bottom: 15px">
      <div class="demo-color-warp">
        <div class="demo-color-box" v-for="(item, key) in _theme" :key="key"
             @click="setThemeColor(item.color, item.label)"
             :style="{'--color':item.color[0][1]}">
          <span class="demo-color__label">{{ item.label }}</span>
          <span class="demo-color__value">{{ item.color[0][0] }}</span>
          <span class="demo-color__value">{{ item.color[0][1] }}</span>
          <ul class="demo-color__list">
            <template v-for="(colorItem, colorKey) in item.color" :key="colorKey">
              <li v-if="colorKey>0" :style="{'--color':colorItem[1]}">
<!--                <span>{{ colorItem[0] }}</span>-->
<!--                <span>{{ colorItem[1] }}</span>-->
              </li>
            </template>
          </ul>
        </div>
      </div>
      <span>当前主题:{{ themeName }}</span>
    </el-row>

    <el-row class="mb-4">
      <el-button>Default</el-button>
      <el-button type="primary">Primary</el-button>
      <el-button type="success">Success</el-button>
      <el-button type="info">Info</el-button>
      <el-button type="warning">Warning</el-button>
      <el-button type="danger">Danger</el-button>
    </el-row>

    <el-row class="mb-4">
      <el-button plain>Plain</el-button>
      <el-button type="primary" plain>Primary</el-button>
      <el-button type="success" plain>Success</el-button>
      <el-button type="info" plain>Info</el-button>
      <el-button type="warning" plain>Warning</el-button>
      <el-button type="danger" plain>Danger</el-button>
    </el-row>

    <el-row class="mb-4">
      <el-button round>Round</el-button>
      <el-button type="primary" round>Primary</el-button>
      <el-button type="success" round>Success</el-button>
      <el-button type="info" round>Info</el-button>
      <el-button type="warning" round>Warning</el-button>
      <el-button type="danger" round>Danger</el-button>
    </el-row>

    <el-row>
      <el-button :icon="Search" circle />
      <el-button type="primary" :icon="Edit" circle />
      <el-button type="success" :icon="Check" circle />
      <el-button type="info" :icon="Message" circle />
      <el-button type="warning" :icon="Star" circle />
      <el-button type="danger" :icon="Delete" circle />
    </el-row>
  </main>
</template>

script部分

<script setup>
import {ref} from "vue"
import { useColorStore } from "@/stores/color";
// ...
const themeName = ref('默认主题');
const colorStore = useColorStore();
// ...
let _theme = [
  {label:'默认主题', color:[["primary", "#409EFF"], ["success", "#67C23A"], ["warning", "#E6A23C"], ["danger", "#F56C6C"], ["error", "#F56C6C"], ["info", "#909399"]]},
  {label:'自定义主题1', color:[["primary", "#1984f3"], ["success", "#55DE12"], ["warning", "#EA9412"], ["danger", "#E12020"], ["error", "#E12020"], ["info", "#209399"]]},
  {label:'自定义主题2', color:[["primary", "#0A4680"], ["success", "#276409"], ["warning", "#815410"], ["danger", "#931d1d"], ["error", "#931D1D"], ["info", "#454A55"]]},
];
// ...
function setThemeColor(colorMap, label) {
  themeName.value = label;
  colorStore.setThemeColor(colorMap);
}
// ...
</script>

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/105511.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

ChinaSoft 论坛巡礼 | 开源软件生态健康度量论坛

2023年CCF中国软件大会&#xff08;CCF ChinaSoft 2023&#xff09;由CCF主办&#xff0c;CCF系统软件专委会、形式化方法专委会、软件工程专委会以及复旦大学联合承办&#xff0c;将于2023年12月1-3日在上海国际会议中心举行。 本次大会主题是“智能化软件创新推动数字经济与社…

Python字典-dict “ “ ---记一次查缺补漏“ “

文章目录 0x0 前言0x1 字典 &#xff08;Dictionary&#xff09;0x01 访问字典里的值0x02 修改字典0x03 删除字典元素0x04 判断字典是否包含指定key&#xff0c;用in或not in 运算符 0x2 字典键的特性0x010x2 0x3 字典内置函数&方法0x4 使用格式化字符串 0x0 前言 python没…

Kotlin(九) 集合以及集合API

目录 一&#xff1a;集合的创建 List 集合的创建&#xff1a; 集合的遍历&#xff1a; Set Map 创建 遍历 二&#xff1a;集合的函数式API maxBy函数 map函数 filter函数 any和all函数 一&#xff1a;集合的创建 List 集合的创建&#xff1a; ① listOf() 不…

Visual Studio Code (VS Code)安装教程

Visual Studio Code&#xff08;简称“VS Code”&#xff09;。 1.下载安装包 VS Code的官网&#xff1a; Visual Studio Code - Code Editing. Redefined 首先提及一下&#xff0c;vscode是不需要破解操作的&#xff1b; 第一步&#xff0c;看好版本&#xff0c;由于我的系…

网络协议--BOOTP:引导程序协议

16.1 引言 在第5章我们介绍了一个无盘系统&#xff0c;它在不知道自身IP地址的情况下&#xff0c;在进行系统引导时能够通过RARP来获取它的IP地址。然而使用RARP有两个问题&#xff1a;&#xff08;1&#xff09;IP地址是返回的唯一结果&#xff1b;&#xff08;2&#xff09;…

031-从零搭建微服务-监控中心(一)

写在最前 如果这个项目让你有所收获&#xff0c;记得 Star 关注哦&#xff0c;这对我是非常不错的鼓励与支持。 源码地址&#xff08;后端&#xff09;&#xff1a;mingyue: &#x1f389; 基于 Spring Boot、Spring Cloud & Alibaba 的分布式微服务架构基础服务中心 源…

「实用技巧」后端如何使用 Eolink Apikit 快速调试接口?

程序员最讨厌的两件事&#xff1a; 写文档 别人不写文档 写文档、维护文档比较麻烦&#xff0c;而且费时&#xff0c;还会经常出现 API 更新了&#xff0c;但文档还是旧的&#xff0c;各种同步不一致的情况&#xff0c;从而耽搁彼此的时间&#xff0c;大多数开发人员不愿意写…

学习笔记-MongoDB(命令增删改查,聚合,权限管理,索引,java使用)

基础概念 1 什么是mogodb&#xff1f; MongoDB 是一个基于分布式文件/文档存储的数据库&#xff0c;由 C 编写&#xff0c;可以为 Web 应用提供可扩展、高性能、易部署的数据存储解决方案。MongoDB 是一个介于关系数据库和非关系数据库之间的产品&#xff0c;是非关系数据库中功…

并发编程- 线程池ForkJoinPool工作原理分析(实践)

数据结构加油站&#xff1a; Comparison Sorting Visualization 并发设计模式 单线程归并排序 public class MergeSort {private final int[] arrayToSort; //要排序的数组private final int threshold; //拆分的阈值&#xff0c;低于此阈值就不再进行拆分public MergeSort…

haproxy 负载均衡

haproxy负载均衡 haproxy&#xff1a;基于C语言开发的开源软件 支持高性能的tcp和http负载均衡器&#xff0c;工作中用的版本1.5.9 haproxy功能&#xff1a;主要用于高并发的web站点&#xff0c;工作原理和nginx、lvs都一样 haproxy缺点: 单节点部署&#xff0c;单实例运行。代…

【postman】postman的使用与postman汉化

postman的使用 Postman 是一个接口测试工具软件&#xff0c;可以帮助开发人员管理测试接口。 官网&#xff1a;Postman API Platform psotman环境 首先import的或则new 创建一个环境 Variable 变量名 Type 类型 Initial value 初始值 C…

prometheus监控kafka

一、前言 关于对kafka的监控&#xff0c;要求高的话可以使用kafka-exorter和jmx-exporter一起收集监控数据&#xff0c;要求不高的情况下可以使用kafka-exporter收集监控数据即可 二、部署 kafka-exporter 部署kafka-exporter&#xff0c;我是在k8s集群中部署的 编辑yaml文件…

D71X-16Q手柄蝶阀型号解析

D71X-16Q型号字母含义解析 D71X-16Q是德特森阀门常用的手柄蝶阀型号字母分别代表的意思是: D——代表阀门类型《蝶阀》 7——代表连接方式《对夹》 1——代表结构形式《中线》 X——代表阀座材质《橡胶》 -代表分隔键 16——代表公称压力《1.6MPA》 Q——代表阀体材料《…

【测试转型】人工智能的当下,测试团队如何敏捷转型 —— 无测试组织

文章目录 〇、引子一、什么是“无测试组织”&#xff1f;二、无测试组织适用于哪些场景&#xff1f;三、无测试组织还有哪些优势或特点&#xff1f;新书推荐 —— 《**无测试组织&#xff1a;测试团队的敏捷转型** 》 〇、引子 初次看到“无测试组织”的朋友可能会觉得有标题党…

Apache ActiveMQ RCE漏洞复现(CNVD-2023-69477)

0x01 产品简介 ActiveMQ是一个开源的消息代理和集成模式服务器&#xff0c;它支持Java消息服务(JMS) API。它是Apache Software Foundation下的一个项目&#xff0c;用于实现消息中间件&#xff0c;帮助不同的应用程序或系统之间进行通信。 0x02 漏洞概述 Apache ActiveMQ 中存…

Spring Boot集成Swagger接口分类与各元素排序问题

在上一篇中我们完成使用JSR-303校验&#xff0c;以及利用Swagger2得到相关接口文档&#xff0c;这节&#xff0c;我们在原先的基础之上&#xff0c;完成Swagger中关于对各个元素之间控制前后顺序的具体配置方法。 Swagger的接口的分组 首先我们需要对Swagger中的接口也就是以…

【LeetCode】102. 二叉树的层序遍历

题目链接 文章目录 Python3方法一&#xff1a; 广度优先搜索 (BFS) ⟮ O ( n ) ⟯ \lgroup O(n) \rgroup ⟮O(n)⟯方法二&#xff1a; 深度优先搜索 (DFS) ⟮ O ( n ) ⟯ \lgroup O(n) \rgroup ⟮O(n)⟯ C方法一&#xff1a; 广度优先搜索 (BFS) ⟮ O ( n ) ⟯ \lgroup O(n…

Android stdio 无法新建或打开AIDL文件(解决方法)

1.在gradle文件中添加如下代码 2.AIDL要求minsdk>16,并且要使aidl true&#xff08;在Gradle中添加&#xff09; android{ buildFeatures { aidl true } } 我们看见&#xff0c;可以创建AIDL文件了 3.接着&#xff0c;我们看到文件出现如下提示 4.在gradle…

hypercube背景设置为白色,绘制高光谱3D立方体

import scipy pip install wxpython PyOpenGL和Spectral需要本地安装 可参考链接https://blog.csdn.net/qq_43204333/article/details/119837870 参考&#xff1a;https://blog.csdn.net/Tiandailan/article/details/132719745?spm1001.2014.3001.5506Mouse Functions:left-cl…

系列六、FactoryBean vs ApplicationContext

一、FactoryBean vs ApplicationContext 1.1、概述 BeanFactory是一个工厂类&#xff0c;负责生产和管理bean&#xff0c;在Spring中BeanFactory是IOC容器的核心接口&#xff0c;它的主要职责就是生产bean及建立各个bean之间的依赖。applicationContext是BeanFactory的一个子接…
最新文章