资讯专栏INFORMATION COLUMN

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

番茄西红柿 / 402人阅读

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

一、搭建好项目的环境。

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

三、在 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>

五、直接在组件中引用

 

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

 

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

转载请注明本文地址: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元查看
<