国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

【VUE】ElementPlus之動態(tài)主題色調(diào)切換(Vue3 + Element Plus+Scss + Pinia)

這篇具有很好參考價值的文章主要介紹了【VUE】ElementPlus之動態(tài)主題色調(diào)切換(Vue3 + Element Plus+Scss + Pinia)。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

前言

關(guān)于ElementPlus的基礎(chǔ)主題色自定義可以參閱《【VUE】ElementPlus之自定義主題樣式和命名空間》

有了上面基礎(chǔ)的了解,我們知道ElementPlus的主題色調(diào)是基于CSS3變量特性進行全局控制的,
那么接下來我們也基于CSS3變量來實現(xiàn)主題色調(diào)的動態(tài)切換效果;

主要控制的色調(diào)類型有:primary、successwarning、danger、error、info
針對這六個色調(diào)類型分別進行0、3、57、8、9級的漸變色定制

以下是默認情況下的主題顏色定義:

【VUE】ElementPlus之動態(tài)主題色調(diào)切換(Vue3 + Element Plus+Scss + Pinia),Web前端,# Vue,vue.js,前端,javascript

接下來,我們基于以下環(huán)境來實操下:

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

實現(xiàn)

默認主題色下的按鈕組件色調(diào):
【VUE】ElementPlus之動態(tài)主題色調(diào)切換(Vue3 + Element Plus+Scss + Pinia),Web前端,# Vue,vue.js,前端,javascript

預(yù)想效果:
【VUE】ElementPlus之動態(tài)主題色調(diào)切換(Vue3 + Element Plus+Scss + Pinia),Web前端,# Vue,vue.js,前端,javascript

在實現(xiàn)具體交互之前,我們先準備幾個輔助小工具

顏色狀態(tài)管理器

既然是動態(tài)切換,那么我們就需要一個容器來記錄當下的一些色調(diào)信息,便于整體性的調(diào)整
以下僅拋個磚,具體業(yè)務(wù)調(diào)整根據(jù)自己需要來哈~

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變量設(shè)置到document中方便全局調(diào)用
   */
  function setPropertyColor(varName, color, funName, level) {
    level = level ? level : 0;
    funName = funName ? funName : "lighten";
    document.documentElement.style.setProperty(
      varName,
      ColorUnit[funName](color, level / 10)
    );
  }

  /**
   * 生成主色的其余漸變色并修改對應(yīng)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,
  };
});

顏色編碼生成工具

根據(jù)前言描述,我們得知,需要根據(jù)一個十六進制的色值,生成其余的漸變色值出來,手動配置的話就太麻煩了
所以我們先來封裝一個ColorUnit工具來輔助我們進行色調(diào)的配置

// file: src/unit/ColorUnit.js
// 代碼載取來至:https://gitee.com/lolicode/scui/blob/master/src/utils/color.js
export default {
	//hex顏色轉(zhuǎn)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顏色轉(zhuǎn)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])
	}
}

整合

有了上面?zhèn)z個幫手,現(xiàn)在整合起來的具體應(yīng)用

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部分文章來源地址http://www.zghlxwxcb.cn/news/detail-718877.html

<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>

到了這里,關(guān)于【VUE】ElementPlus之動態(tài)主題色調(diào)切換(Vue3 + Element Plus+Scss + Pinia)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔相關(guān)法律責任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經(jīng)查實,立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費用

相關(guān)文章

  • Vue3 + Vite + Css3切換主題

    Vue3 + Vite + Css3切換主題

    1、css3中變量的作用 一個系統(tǒng)或者說一個項目中,往往涉及到很多顏色,但是如果系統(tǒng)看起來樣式規(guī)整統(tǒng)一的話可能在色值方面偏靠一個色系,字體,顏色,背景顏色,圖標顏色等等。 所有可以在css中定義統(tǒng)一的變量,就不用到處去改防止在修改的時候遺漏。 2、css3中如何聲

    2024年01月21日
    瀏覽(48)
  • vue使用element-ui 實現(xiàn)多套自定義主題快速切換

    vue使用element-ui 實現(xiàn)多套自定義主題快速切換

    下載到本地項目文件 配置所需主題樣式文件和className,例如上面代碼:theme-black、theme-blue… 在根目錄執(zhí)行以下命令:

    2024年02月11日
    瀏覽(24)
  • vue3后臺管理系統(tǒng)實現(xiàn)動態(tài)側(cè)邊導(dǎo)航菜單管理(ElementPlus組件)

    vue3后臺管理系統(tǒng)實現(xiàn)動態(tài)側(cè)邊導(dǎo)航菜單管理(ElementPlus組件)

    記住 一級(el-sub-menu)的都是只是展示的 點擊跳轉(zhuǎn)的都是一級下的子級(el-menu-item) 完整展示 1:在登陸功能進行登陸 獲取menu列表 注冊路由表的時候 把文件進行創(chuàng)建好 因為注冊的方法需要獲取這個路徑 整個router下的main product等等都要創(chuàng)建 2:側(cè)邊菜單界面 router/index.ts

    2024年02月16日
    瀏覽(31)
  • vue3 + TS + elementplus + pinia實現(xiàn)后臺管理系統(tǒng)左側(cè)菜單聯(lián)動實現(xiàn) tab根據(jù)路由切換聯(lián)動內(nèi)容

    vue3 + TS + elementplus + pinia實現(xiàn)后臺管理系統(tǒng)左側(cè)菜單聯(lián)動實現(xiàn) tab根據(jù)路由切換聯(lián)動內(nèi)容

    效果圖: ?home.vue頁面代碼 left.vue頁面代碼 tab.vue頁面代碼 pinia里面的代碼 安裝 使用插件 ?在main.ts中注冊 路由代碼 我把代碼放git上了,有需要的自行拉取 https://gitee.com/Flechazo7/vue3.git

    2024年02月09日
    瀏覽(28)
  • uni-app+vue3微信小程序切換主題皮膚

    思路來源: https://blog.csdn.net/qq_42611074/article/details/128236458 引用store做全局css變量替換; store.js 添加全局的監(jiān)聽函數(shù) common/themeMixin.js main.js 給要切換的頁面加上css變量 login.vue 升級版 在base.css寫好主題配色; 引用store做全局css變量替換; store.js 添加全局的監(jiān)聽函數(shù) common/themeM

    2024年02月12日
    瀏覽(100)
  • Vue3在點擊菜單切換路由時,將ElementPlus UI庫中el-main組件的內(nèi)容滾動恢復(fù)到頂部

    Vue3在點擊菜單切換路由時,將ElementPlus UI庫中el-main組件的內(nèi)容滾動恢復(fù)到頂部

    功能:Vue3在點擊菜單切換路由時,將頁面el-main的內(nèi)容滾動到頂部,布局如下,使用UI組件庫為ElementPlus ?在網(wǎng)上搜很多都是在route.js中的router.beforeEach中使用window.scrollTop(0,0) 或?window.scrollTo(0,0) 滾動,但是我使用無效,于是使用操作dom的方法,如下 可以使用 watch 函數(shù)來? 監(jiān)聽

    2024年01月18日
    瀏覽(36)
  • vue3+element Plus+ts 自定義主題色,以及生成主題色各種透明度

    vue3+element Plus+ts 自定義主題色,以及生成主題色各種透明度

    目錄 思路? 安裝css-color-function【接收一個顏色值,生成不同的透明度】 獲取后臺配置的主題色或者使用ColorPicker修改主題色 ?最終結(jié)果如下 本篇文章的主體思路是從element Plus官網(wǎng)引申而來。結(jié)合了我以前用vue2+element-ui配置主題色生成透明度(light-1至linght-9)的方法。 utils/

    2024年02月21日
    瀏覽(17)
  • Naive UI+Vue3來實現(xiàn)點擊按鈕一鍵切換明暗主題的功能

    Naive UI+Vue3來實現(xiàn)點擊按鈕一鍵切換明暗主題的功能

    記錄一下如何使用Naive UI+Vue3代碼來實現(xiàn)一鍵切換明暗主題的功能。 效果如下: 終端下輸入: 起好項目的名稱,然后一路回車即可。 至此Vue3的項目已經(jīng)搭建完畢,打開http://127.0.0.1:5173/就可以看到項目的默認首頁了。 安裝Naive UI依賴庫 然后我們根據(jù)官方出的配置對項目進行

    2024年02月12日
    瀏覽(75)
  • vue3+vite+element-plus創(chuàng)建項目,修改主題色

    vue3+vite+element-plus創(chuàng)建項目,修改主題色

    根據(jù)官方文檔安裝依賴 vite.config.js配置 新建一個文修改全局樣式的文件 在src下新建styles/element/index.scss文件,內(nèi)容如下: @forward \\\'element-plus/theme-chalk/src/common/var.scss\\\' with ( ? ? $colors: ( ? ? ? ? \\\'primary\\\': ( ? ? ? ? ? ? //主色 ? ? ? ? ? ? \\\'base\\\':green ? ? ? ? ) ? ? ? ? //修改其他

    2024年02月10日
    瀏覽(26)
  • vue3 +element動態(tài)表單實現(xiàn)

    可以直接復(fù)制,接口看后端 父頁面 子頁面

    2024年02月15日
    瀏覽(21)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包