轉至 https://www.cnblogs.com/dengqichang/p/10364455.html
一、搭建好項目的環境。 二、根據ElementUI官網的自定義主題(http://element.eleme.io/#/zh-CN/component/custom-theme)來安裝【主題生成工具】。 首先安裝「主題生成工具」,可以全局安裝或者安裝在當前項目下,推薦安裝在項目里,方便別人 clone 項目時能直接安裝依賴並啟動,這里以全局安裝做演示。 npm i element-theme -g 安裝白堊主題,可以從 npm 安裝或者從 GitHub 拉取最新代碼。 # 從 npm npm i element-theme-chalk -D # 從 GitHub npm i https://github.com/ElementUI/theme-chalk -D ¶初始化變量文件 主題生成工具安裝成功后,如果全局安裝可以在命令行里通過 et 調用工具,如果安裝在當前目錄下,需要通過 node_modules/.bin/et 訪問到命令。執行 -i 初始化變量文件。默認輸出到 element-variables.scss,當然你可以傳參數指定文件輸出目錄。 et -i [可以自定義變量文件] > ✔ Generator variables file 如果使用默認配置,執行后當前目錄會有一個 element-variables.scss 文件。內部包含了主題所用到的所有變量,它們使用 SCSS 的格式定義。大致結構如下: $--color-primary: #409EFF !default; $--color-primary-light-1: mix($--color-white, $--color-primary, 10%) !default; /* 53a8ff */ $--color-primary-light-2: mix($--color-white, $--color-primary, 20%) !default; /* 66b1ff */ $--color-primary-light-3: mix($--color-white, $--color-primary, 30%) !default; /* 79bbff */ $--color-primary-light-4: mix($--color-white, $--color-primary, 40%) !default; /* 8cc5ff */ $--color-primary-light-5: mix($--color-white, $--color-primary, 50%) !default; /* a0cfff */ $--color-primary-light-6: mix($--color-white, $--color-primary, 60%) !default; /* b3d8ff */ $--color-primary-light-7: mix($--color-white, $--color-primary, 70%) !default; /* c6e2ff */ $--color-primary-light-8: mix($--color-white, $--color-primary, 80%) !default; /* d9ecff */ $--color-primary-light-9: mix($--color-white, $--color-primary, 90%) !default; /* ecf5ff */ $--color-success: #67c23a !default; $--color-warning: #e6a23c !default; $--color-danger: #f56c6c !default; $--color-info: #909399 !default; ... ¶修改變量 直接編輯 element-variables.scss 文件,例如修改主題色為紅色。 $--color-primary: red; ¶編譯主題 保存文件后,到命令行里執行 et 編譯主題,如果你想啟用 watch 模式,實時編譯主題,增加 -w 參數;如果你在初始化時指定了自定義變量文件,則需要增加 -c 參數,並帶上你的變量文件名 et > ✔ build theme font > ✔ build element theme ¶引入自定義主題 默認情況下編譯的主題目錄是放在 ./theme 下,你可以通過 -o 參數指定打包目錄。像引入默認主題一樣,在代碼里直接引用 theme/index.css 文件即可。 import '../theme/index.css' import ElementUI from 'element-ui' import Vue from 'vue' Vue.use(ElementUI) 三、在 element-variables.scss 文件里修改 $–color-primary:#409EFF,即你想要的主題顏色。然后,執行主題編譯命令生成主題(et),根目錄會生成一個theme文件夾。
四、封裝動態換膚色ThemePicker.vue組件。
<template> <el-color-picker class="theme-picker" popper-class="theme-picker-dropdown" v-model="theme" :size="size"> </el-color-picker> </template> <script> const version = require('element-ui/package.json').version // element-ui version from node_modules const ORIGINAL_THEME = '#409EFF' // default color export default { name: 'ThemePicker', props: { default: { // 初始化主題,可由外部傳入 type: String, //default: '#EB815B' default: ""+localStorage.getItem("tremePackers")+"" }, size: { // 初始化主題,可由外部傳入 type: String, default: 'small' } }, data() { return { chalk: '', // content of theme-chalk css theme: ORIGINAL_THEME, showSuccess: true, // 是否彈出換膚成功消息 } }, mounted() { if(this.default != null) { this.theme = this.default this.$emit('onThemeChange', this.theme) this.showSuccess = false } }, watch: { theme(val, oldVal) { if (typeof val !== 'string') return const themeCluster = this.getThemeCluster(val.replace('#', '')) const originalCluster = this.getThemeCluster(oldVal.replace('#', '')) 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.$emit('onThemeChange', val) //存入localStorage localStorage.setItem('tremePackers',val); if(this.showSuccess) { this.$message({ message: '換膚成功', type: 'success' }) } else { this.showSuccess = true } } }, 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) => { let red = parseInt(color.slice(0, 2), 16) let green = parseInt(color.slice(2, 4), 16) let blue = parseInt(color.slice(4, 6), 16) 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> <style> .theme-picker .el-color-picker__trigger { vertical-align: middle; } .theme-picker-dropdown .el-color-dropdown__link-btn { display: none; } </style>
五、直接在組件中引用
六、換膚效果測試。