基于element-ui?動態(tài)換膚的代碼詳解
1、在安裝好element-ui@2.x 以后,首先安裝sass-loader
npm i sass-loader node-sass -D
2、安裝element-theme
npm i element-theme -D
3、安裝theme-chalk
npm i element-theme-chalk -D # or from github npm i https://github.com/ElementUI/theme-chalk -D
4、初始化變量文件
et -i // 默認(rèn)的文件是element-variables.scss,也可以自定義文件名 et --init [file path]
安裝成功以后,在項(xiàng)目里會自動生成一個(gè)element-variables.scss 文件,如下圖:
里面定義的是所有的顏色變量
當(dāng)然,這一步也有可能失敗,命令行提示找不到et 這個(gè)命令。這個(gè)時(shí)候需要按照步驟一,重新裝一下sass-loader
5、修改變量
直接編輯 element-variables.scss 文件,例如修改主題色為紅色
6、編譯主題
保存文件后,到命令行里執(zhí)行 et 編譯主題,如果你想啟用 watch 模式,實(shí)時(shí)編譯主題,增加 -w 參數(shù);如果你在初始化時(shí)指定了自定義變量文件,則需要增加 -c 參數(shù),并帶上你的變量文件名
此時(shí),項(xiàng)目中會自動生成一個(gè)theme文件夾,里面是編譯后所有的字體文件和樣式文件
7、引入自定義主題
默認(rèn)情況下編譯的主題目錄是放在 ./theme 下,你可以通過 -o 參數(shù)指定打包目錄。像引入默認(rèn)主題一樣,在代碼里直接引用 theme/index.css 文件即可。
import '../theme/index.css' import ElementUI from 'element-ui' import Vue from 'vue' Vue.use(ElementUI)
啟動項(xiàng)目,會發(fā)現(xiàn)原來默認(rèn)的藍(lán)色會變成紅色
官網(wǎng)提供的這種方法僅適用于一次性的更改全局主題顏色,如果想實(shí)現(xiàn)官網(wǎng)2.0版本右上角,使用 ColorPicker 顏色選擇器 動態(tài)換膚。那么建議參考vue-element-admin,作者的 《手摸手,帶你用vue擼后臺》系列文章非常精彩
ThemePicker.vue <template> <el-tooltip effect="dark" content="theme" placement="bottom"> <el-color-picker v-model="theme" class="theme-picker" size="small" popper-class="theme-picker-dropdown"/> </el-tooltip> </template> <script> const version = require('element-ui/package.json').version // element-ui version from node_modules const ORIGINAL_THEME = '#409EFF' // default color export default { data() { return { chalk: '', // content of theme-chalk css theme: ORIGINAL_THEME } }, watch: { theme(val, oldVal) { if (typeof val !== 'string') return const themeCluster = this.getThemeCluster(val.replace('#', '')) const originalCluster = this.getThemeCluster(oldVal.replace('#', '')) console.log(themeCluster, originalCluster) const getHandler = (variable, id) => { return () => { const originalCluster = this.getThemeCluster(ORIGINAL_THEME.replace('#', '')) const newStyle = this.updateStyle(this[variable], originalCluster, themeCluster) let styleTag = document.getElementById(id) if (!styleTag) { styleTag = document.createElement('style') styleTag.setAttribute('id', id) document.head.appendChild(styleTag) } styleTag.innerText = newStyle } } const chalkHandler = getHandler('chalk', 'chalk-style') if (!this.chalk) { const url = `https://unpkg.com/element-ui@${version}/lib/theme-chalk/index.css` this.getCSSString(url, chalkHandler, 'chalk') } else { chalkHandler() const styles = [].slice.call(document.querySelectorAll('style')) .filter(style => { const text = style.innerText return new RegExp(oldVal, 'i').test(text) && !/Chalk Variables/.test(text) }) styles.forEach(style => { const { innerText } = style if (typeof innerText !== 'string') return style.innerText = this.updateStyle(innerText, originalCluster, themeCluster) }) this.$message({ message: '換膚成功', type: 'success' methods: { updateStyle(style, oldCluster, newCluster) { let newStyle = style oldCluster.forEach((color, index) => { newStyle = newStyle.replace(new RegExp(color, 'ig'), newCluster[index]) return newStyle }, getCSSString(url, callback, variable) { const xhr = new XMLHttpRequest() xhr.onreadystatechange = () => { if (xhr.readyState === 4 && xhr.status === 200) { this[variable] = xhr.responseText.replace(/@font-face{[^}]+}/, '') callback() xhr.open('GET', url) xhr.send() getThemeCluster(theme) { const tintColor = (color, tint) => { let red = parseInt(color.slice(0, 2), 16) let green = parseInt(color.slice(2, 4), 16) let blue = parseInt(color.slice(4, 6), 16) if (tint === 0) { // when primary color is in its rgb space return [red, green, blue].join(',') } else { red += Math.round(tint * (255 - red)) green += Math.round(tint * (255 - green)) blue += Math.round(tint * (255 - blue)) red = red.toString(16) green = green.toString(16) blue = blue.toString(16) return `#${red}${green}${blue}` const shadeColor = (color, shade) => { red = Math.round((1 - shade) * red) green = Math.round((1 - shade) * green) blue = Math.round((1 - shade) * blue) red = red.toString(16) green = green.toString(16) blue = blue.toString(16) return `#${red}${green}${blue}` const clusters = [theme] for (let i = 0; i <= 9; i++) { clusters.push(tintColor(theme, Number((i / 10).toFixed(2)))) clusters.push(shadeColor(theme, 0.1)) return clusters } } </script>
Navbar.vue <template> <el-menu class="navbar" mode="horizontal"> <hamburger class="hamburger-container" :toggleClick="toggleSideBar" :isActive="!sidebar.opened"> </hamburger> <div class="right-menu"> <screenfull class="screenfull"></screenfull> <div class="lang"> <el-dropdown> <i class="iconfont icon-language4"></i> <el-dropdown-menu slot="dropdown"> <el-dropdown-item @click.native="toggleLang('zh')" :disabled="$i18n.locale == 'zh'">中文</el-dropdown-item> <el-dropdown-item @click.native="toggleLang('en')" :disabled="$i18n.locale == 'en'">English</el-dropdown-item> </el-dropdown-menu> </el-dropdown> </div> <theme-picker></theme-picker> </div> </el-menu> </template>
以上demo代碼地址:https://github.com/frwupeng517/element-admin
Element-UI 官方文檔地址:http://element-cn.eleme.io/#/zh-CN/component/custom-theme
PanJiachen Git地址:https://github.com/PanJiaChen/vue-element-admin
到此這篇關(guān)于element-ui 動態(tài)換膚的文章就介紹到這了,更多相關(guān)element-ui 動態(tài)換膚內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
js字符串轉(zhuǎn)換成數(shù)字與數(shù)字轉(zhuǎn)換成字符串的實(shí)現(xiàn)方法
本篇文章主要是對js字符串轉(zhuǎn)換成數(shù)字與數(shù)字轉(zhuǎn)換成字符串的實(shí)現(xiàn)方法進(jìn)行了詳細(xì)的介紹,需要的朋友可以過來參考下,希望對大家有所幫助2014-01-01JavaScript實(shí)現(xiàn)選項(xiàng)卡功能(面向過程與面向?qū)ο?
本文主要介紹了JavaScript實(shí)現(xiàn)選項(xiàng)卡功能(面向過程與面向?qū)ο?,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02jJavaScript中toFixed()和正則表達(dá)式的坑
這篇文章主要介紹了jJavaScript中toFixed()和正則表達(dá)式的坑,toFixed方法可以把Number四舍五入為指定小數(shù)位數(shù)的數(shù)字,具體詳細(xì)內(nèi)容需要的小伙伴可以參考一下2022-04-04一個(gè)簡易時(shí)鐘效果js實(shí)現(xiàn)代碼
這篇文章主要為大家詳細(xì)介紹了一個(gè)簡易時(shí)鐘效果js實(shí)現(xiàn)代碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10JavaScript提高網(wǎng)站性能優(yōu)化的建議(二)
這篇文章主要介紹了JavaScript提高網(wǎng)站性能優(yōu)化的建議(二)的相關(guān)資料,需要的朋友可以參考下2016-07-07javascript 瀏覽器類型和版本號檢測代碼(兼容多瀏覽器)
果對javascript了解不是特別深入的話,很容易就會寫出不兼容的代碼(就像我),這時(shí)候就得判斷瀏覽器了。比如事件偵聽、一些鼠標(biāo)和鍵盤事件、Range等,一些都會不一樣.下面列出幾種常用的檢測瀏覽器方法,以饗觀眾!2010-04-04