资讯专栏INFORMATION COLUMN

vue中使用Element主题自定义肤色

番茄西红柿 / 657人阅读

摘要:一搭建好项目的环境。二根据官网的自定义主题来安装主题生成工具。三在文件里修改,即你想要的主题颜色。然后,执行主题编译命令生成主题,根目录会生成一个文件夹。四封装动态换肤色组件。关闭浏览器再次打开依旧是你所选中的主题肤色

一、搭建好项目的环境。

二、根据ElementUI官网的自定义主题(http://element.eleme.io/#/zh-CN/component/custom-theme)来安装【主题生成工具】。

三、在 element-variables.scss 文件里修改 $–color-primary:#409EFF,即你想要的主题颜色。然后,执行主题编译命令生成主题(et),根目录会生成一个theme文件夹。

 

 四、封装动态换肤色ThemePicker.vue组件。

 

</>复制代码

  1. <template>
  2. <el-color-picker
  3. class="theme-picker"
  4. popper-class="theme-picker-dropdown"
  5. v-model="theme"
  6. :size="size">
  7. el-color-picker>
  8. template>
  9. <script>
  10. const version = require(element-ui/package.json).version // element-ui version from node_modules
  11. const ORIGINAL_THEME = #409EFF // default color
  12. export default {
  13. name: ThemePicker,
  14. props: {
  15. default: { // 初始化主题,可由外部传入
  16. type: String,
  17. //default: #EB815B
  18. default: ""+localStorage.getItem("tremePackers")+""
  19. },
  20. size: { // 初始化主题,可由外部传入
  21. type: String,
  22. default: small
  23. }
  24. },
  25. data() {
  26. return {
  27. chalk: , // content of theme-chalk css
  28. theme: ORIGINAL_THEME,
  29. showSuccess: true, // 是否弹出换肤成功消息
  30. }
  31. },
  32. mounted() {
  33. if(this.default != null) {
  34. this.theme = this.default
  35. this.$emit(onThemeChange, this.theme)
  36. this.showSuccess = false
  37. }
  38. },
  39. watch: {
  40. theme(val, oldVal) {
  41. if (typeof val !== string) return
  42. const themeCluster = this.getThemeCluster(val.replace(#, ))
  43. const originalCluster = this.getThemeCluster(oldVal.replace(#, ))
  44. const getHandler = (variable, id) => {
  45. return () => {
  46. const originalCluster = this.getThemeCluster(ORIGINAL_THEME.replace(#, ))
  47. const newStyle = this.updateStyle(this[variable], originalCluster, themeCluster)
  48. let styleTag = document.getElementById(id)
  49. if (!styleTag) {
  50. styleTag = document.createElement(style)
  51. styleTag.setAttribute(id, id)
  52. document.head.appendChild(styleTag)
  53. }
  54. styleTag.innerText = newStyle
  55. }
  56. }
  57. const chalkHandler = getHandler(chalk, chalk-style)
  58. if (!this.chalk) {
  59. const url = `https://unpkg.com/element-ui@${version}/lib/theme-chalk/index.css`
  60. this.getCSSString(url, chalkHandler, chalk)
  61. } else {
  62. chalkHandler()
  63. }
  64. const styles = [].slice.call(document.querySelectorAll(style))
  65. .filter(style => {
  66. const text = style.innerText
  67. return new RegExp(oldVal, i).test(text) && !/Chalk Variables/.test(text)
  68. })
  69. styles.forEach(style => {
  70. const { innerText } = style
  71. if (typeof innerText !== string) return
  72. style.innerText = this.updateStyle(innerText, originalCluster, themeCluster)
  73. })
  74. // 响应外部操作
  75. this.$emit(onThemeChange, val)
  76. //存入localStorage
  77. localStorage.setItem(tremePackers,val);
  78. if(this.showSuccess) {
  79. this.$message({
  80. message: 换肤成功,
  81. type: success
  82. })
  83. } else {
  84. this.showSuccess = true
  85. }
  86. }
  87. },
  88. methods: {
  89. updateStyle(style, oldCluster, newCluster) {
  90. let newStyle = style
  91. oldCluster.forEach((color, index) => {
  92. newStyle = newStyle.replace(new RegExp(color, ig), newCluster[index])
  93. })
  94. return newStyle
  95. },
  96. getCSSString(url, callback, variable) {
  97. const xhr = new XMLHttpRequest()
  98. xhr.onreadystatechange = () => {
  99. if (xhr.readyState === 4 && xhr.status === 200) {
  100. this[variable] = xhr.responseText.replace(/@font-face{[^}]+}/, )
  101. callback()
  102. }
  103. }
  104. xhr.open(GET, url)
  105. xhr.send()
  106. },
  107. getThemeCluster(theme) {
  108. const tintColor = (color, tint) => {
  109. let red = parseInt(color.slice(0, 2), 16)
  110. let green = parseInt(color.slice(2, 4), 16)
  111. let blue = parseInt(color.slice(4, 6), 16)
  112. if (tint === 0) { // when primary color is in its rgb space
  113. return [red, green, blue].join(,)
  114. } else {
  115. red += Math.round(tint * (255 - red))
  116. green += Math.round(tint * (255 - green))
  117. blue += Math.round(tint * (255 - blue))
  118. red = red.toString(16)
  119. green = green.toString(16)
  120. blue = blue.toString(16)
  121. return `#${red}${green}${blue}`
  122. }
  123. }
  124. const shadeColor = (color, shade) => {
  125. let red = parseInt(color.slice(0, 2), 16)
  126. let green = parseInt(color.slice(2, 4), 16)
  127. let blue = parseInt(color.slice(4, 6), 16)
  128. red = Math.round((1 - shade) * red)
  129. green = Math.round((1 - shade) * green)
  130. blue = Math.round((1 - shade) * blue)
  131. red = red.toString(16)
  132. green = green.toString(16)
  133. blue = blue.toString(16)
  134. return `#${red}${green}${blue}`
  135. }
  136. const clusters = [theme]
  137. for (let i = 0; i <= 9; i++) {
  138. clusters.push(tintColor(theme, Number((i / 10).toFixed(2))))
  139. }
  140. clusters.push(shadeColor(theme, 0.1))
  141. return clusters
  142. }
  143. }
  144. }
  145. script>
  146. <style>
  147. .theme-picker .el-color-picker__trigger {
  148. vertical-align: middle;
  149. }
  150. .theme-picker-dropdown .el-color-dropdown__link-btn {
  151. display: none;
  152. }
  153. style>

五、直接在组件中引用

 

 六、换肤效果测试。(关闭浏览器再次打开依旧是你所选中的主题肤色)

 

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/1236.html

相关文章

  • VUE UI框架对比 element-ui 与 iView

    摘要:而则是用到到指令结合的方式去生成,批量生成元素。表格操作列自定义渲染的时,使用的是的函数,直接在中插入对应模板表格分页都需要引入分页组件配合使用两者总体比较,要比简洁许多。 element VS iview(最近项目UI框架在选型 ,做了个分析, 不带有任何利益相关)主要从以下几个方面来做对比使用率(npm 平均下载频率,组件数量,star, issue…)API风格打包优化与设计师友...

    ZHAO_ 评论0 收藏0
  • Vue ui 大法哪家强?

    Element iView Vuex Mint UI Vant cube-ui,对比六大 vue ui 组件库,选中最适合的那个。 Element(pc) 介绍 & 版本 饿了么前端团队开发的桌面端组件库,当前最新版本:2.4.8 Star:32.067k 项目特色 团队维护 支持三个版本:vue、react、angular 支持 Nuxt.js 常规支持:多语言、自定义主题、按需引入、内置...

    Edison 评论0 收藏0
  • Element 一套优雅的 Vue 2 组件库是如何开发的

    摘要:今年的大会上,受到作者现场开源项目的感染,我们也在现场宣布开源这套基于开发的组件库。一个文件夹下有所有的官方插件,直到发现他们用了一个叫的工具。那么如何写样式同时单独发布的组件如何引用样式文件也是我们要解决的问题。 showImg(https://segmentfault.com/img/bVDD9H?w=2502&h=1222); 今年的 JSConf 大会上,受到 gridcont...

    lscho 评论0 收藏0
  • 手摸手,带你用vue撸后台 系列三(实战篇)

    摘要:社区的认可目前已经是相关最多的开源项目了,体现出了社区对其的认可。监听事件手动维护列表这样我们就简单的完成了拖拽排序。 完整项目地址:vue-element-admin 系类文章一:手摸手,带你用vue撸后台 系列一(基础篇)系类文章二:手摸手,带你用vue撸后台 系列二(登录权限篇)系类文章三:手摸手,带你用vue撸后台 系列三(实战篇)系类文章四:手摸手,带你用vue撸后台 系列...

    Channe 评论0 收藏0
  • 手摸手,带你用vue撸后台 系列三(实战篇)

    摘要:社区的认可目前已经是相关最多的开源项目了,体现出了社区对其的认可。监听事件手动维护列表这样我们就简单的完成了拖拽排序。 完整项目地址:vue-element-admin 系类文章一:手摸手,带你用vue撸后台 系列一(基础篇)系类文章二:手摸手,带你用vue撸后台 系列二(登录权限篇)系类文章三:手摸手,带你用vue撸后台 系列三(实战篇)系类文章四:手摸手,带你用vue撸后台 系列...

    zgbgx 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<