最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

vue+element-ui實現主題切換功能

 更新時間:2022年06月26日 10:33:09   作者:邵先森~  
這篇文章主要介紹了vue+element-ui實現主題切換功能,本文通過示例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下

element-ui提供了自定義主題 自定義主題

一、安裝

  • npm i element-theme -g
  • npm i element-theme-chalk -D
  • npm i https://github.com/ElementUI/theme-chalk -D
  • 官網說明安裝完成后需要et -i并會有element-variables.scss文件,但是我文件中并未找到 node_modules/.bin/et,所以手動生成了

下圖element-variables.scss是自己寫的,因為安裝完成后并未生成此文件

文件中讓內容如下`/* 改變主題色變量 */

 ```
 /* 改變主題色變量,設置完成后會有按鈕字體等組件會變化 */
 $--color-primary: #d85f6a;  
 
 /* 改變 icon 字體路徑變量,必需 */
 $--font-path: '~element-ui/lib/theme-chalk/fonts';
 
 @import "~element-ui/packages/theme-chalk/src/index";
 ```

頁面文件:index.vue

     <el-radio-group v-model="themeRadio" @change="changeClick" size="mini">
       <el-radio label="#d85f6a">紅色主題</el-radio>
          <el-radio label="#409EFF">藍色主題</el-radio>
      </el-radio-group>
	
	文件引入:
	import ColorPicker from "@/layout/colorpicker/index";  
	使用:
    <color-picker :colorVal="colorVal"></color-picker>
	方法:
    changeClick(value){
      this.colorVal = value
      localStorage.setItem('skin',value)
      this.$store.commit("setSkin",value)
    },

colorpicker.vue文件,文件內容如下:

<template>
  <el-color-picker
    v-if="false"
    v-model="theme"
    :predefine="['#409EFF', '#1890ff', '#304156','#212121','#11a983', '#13c2c2', '#6959CD', '#f5222d', ]"
    class="theme-picker"
    popper-class="theme-picker-dropdown"
  />
</template>
<script>

const version = require('element-ui/package.json').version // element-ui version from node_modules
const ORIGINAL_THEME = '#409EFF' // default color
import { Loading } from 'element-ui';
export default {
  props:{   //在頁面中將colorVal傳進來
    colorVal:{
      type:String,  
    }
  },
  created(){
    // let option={
    //   background:'#FFFFFF'
    // }
      this.loadingInstance =Loading.service();  //這里增加加載loadding,因為刷新頁面會出現延遲

  },
  data() {
    return {
      chalk: '', // content of theme-chalk css
      theme: '',
      loadingInstance:true
    }
  },
  computed: {
    defaultTheme() {
    // 將選擇的顏色屬性保存在vuex中,如果頁面刷新娶不到就從ocalStorage中取
      if(this.$store.state.skin)  return this.$store.state.skin;
      else return  localStorage.getItem('skin')
      // return this.$store.state.skin
    }
  },
  watch: {
    defaultTheme:{
      handler: function(val) {
        this.$nextTick(() => {
          // this.$emit('input', this.model);
        this.theme = val
        })
      },
      immediate: true
    },
    async theme(val) {
      const oldVal = this.chalk ? this.theme : ORIGINAL_THEME
      if (typeof val !== 'string') return
      const themeCluster = this.getThemeCluster(val.replace('#', ''))
      const originalCluster = this.getThemeCluster(oldVal.replace('#', ''))
      const $message = this.$message({
        message: '  Compiling the theme',
        customClass: 'theme-message',
        type: 'success',
        duration: 0,
        iconClass: 'el-icon-loading'
      })
      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
        }
      }
      if (!this.chalk) {
        const url = `https://unpkg.com/element-ui@${version}/lib/theme-chalk/index.css`
        await this.getCSSString(url, 'chalk')
      }
      const chalkHandler = getHandler('chalk', 'chalk-style')
      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('change', val)
      $message.close()
      // this.$message({
      //     message:'皮膚切換成功',
      //     type:'success'
      // })
      this.$nextTick(() => { // 以服務的方式調用的 Loading 需要異步關閉
        // this.loadingInstance.close();
      })
      setTimeout(()=>{
        this.loadingInstance.close();

      },1000)
    }
  },
  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, variable) {
      return new Promise(resolve => {
        const xhr = new XMLHttpRequest()
        xhr.onreadystatechange = () => {
          if (xhr.readyState === 4 && xhr.status === 200) {
            this[variable] = xhr.responseText.replace(/@font-face{[^}]+}/, '')
            resolve()
          }
        }
        xhr.open('GET', url)
        xhr.send()
      })
    },

    //將傳入的24進制顏色標號進行處理,
    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-message,
.theme-picker-dropdown {
  z-index: 99999 !important;
}
.theme-picker .el-color-picker__trigger {
  height: 26px !important;
  width: 26px !important;
  padding: 2px;
}
.theme-picker-dropdown .el-color-dropdown__link-btn {
  display: none;
}
</style>

效果:

但是到現在有個問題,就是element-ui有自己的ui主題,每次radio切換主題時沒問題,但是F5刷新后頁面元素顏色會閃爍,順序:element-ui顏色>當前設置緩存顏色,為了解決這個問題,就優(yōu)化了代碼,在APP.vue中設置,當頁面刷新時能更快的渲染
1.新建colorpicker.js文件

2.APP.vue中引入

//colorpicker.js中只保留了 colorpicker.vue 中 methods:中的方法
import  colorpicker from '@/mixins/colorpicker.js'

3.使用mixins模式

mixins:[colorpicker],

在created中使用

  async created() {
      await this.getColor('#409EFF')
      await this.configRouter();
  },
  //將colorpicker.vue中此方法拿出來
  async getColor(val){
      let theme = val
      const oldVal = this.chalk ? theme : ORIGINAL_THEME
      if (typeof val !== 'string') return
      const themeCluster = this.getThemeCluster(val.replace('#', ''))
      const originalCluster = this.getThemeCluster(oldVal.replace('#', ''))
      const $message = this.$message({
        message: '  Compiling the theme',
        customClass: 'theme-message',
        type: 'success',
        duration: 0,
        iconClass: 'el-icon-loading'
      })
      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
        }
      }
      if (!this.chalk) {
        const url = `https://unpkg.com/element-ui@${version}/lib/theme-chalk/index.css`
        await this.getCSSString(url, 'chalk')
      }
      const chalkHandler = getHandler('chalk', 'chalk-style')
      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('change', val)
      $message.close()
    },

到此這篇關于vue+element-ui實現主題切換的文章就介紹到這了,更多相關vue主題切換內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • vue中的.$mount(''#app'')手動掛載操作

    vue中的.$mount(''#app'')手動掛載操作

    這篇文章主要介紹了vue中.$mount('#app')手動掛載操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-09-09
  • Vue?編程式路由導航的實現示例

    Vue?編程式路由導航的實現示例

    本文主要介紹了Vue?編程式路由導航
    2022-04-04
  • vue-router路由跳轉問題 replace

    vue-router路由跳轉問題 replace

    這篇文章主要介紹了vue-router路由跳轉問題 replace,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-09-09
  • Vue 進入/離開動畫效果

    Vue 進入/離開動畫效果

    這篇文章主要介紹了Vue 進入/離開動畫效果,代碼簡單易懂,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
    2017-12-12
  • vue調用微信JSDK 掃一掃,相冊等需要注意的事項

    vue調用微信JSDK 掃一掃,相冊等需要注意的事項

    這篇文章主要介紹了vue調用微信JSDK 掃一掃,相冊等需要注意的事項,幫助大家更好的理解和使用vue框架,感興趣的朋友可以了解下
    2021-01-01
  • vue實例配置對象中el、template、render的用法

    vue實例配置對象中el、template、render的用法

    這篇文章主要介紹了vue實例配置對象中el、template、render的用法,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-11-11
  • 一文快速學會阻止事件冒泡的4種方法(原生js阻止,vue中使用修飾符阻止)

    一文快速學會阻止事件冒泡的4種方法(原生js阻止,vue中使用修飾符阻止)

    冒泡就是事件開始是由最具體的元素接收,然后逐層向上級傳播到較為不具體的元素,這篇文章主要給大家介紹了關于阻止事件冒泡的4種方法,文中介紹的方法分別是原生js阻止以及vue中使用修飾符阻止的相關資料,需要的朋友可以參考下
    2023-12-12
  • vue.js?diff算法原理詳細解析

    vue.js?diff算法原理詳細解析

    這篇文章主要介紹了vue.js?diff算法原理詳細解析,diff算法可以看作是一種對比算法,對比的對象是新舊虛擬Dom。顧名思義,diff算法可以找到新舊虛擬Dom之間的差異,但diff算法中其實并不是只有對比虛擬Dom,還有根據對比后的結果更新真實Dom
    2022-06-06
  • Vue監(jiān)視數據的原理詳解

    Vue監(jiān)視數據的原理詳解

    這篇文章主要為大家詳細介紹了Vue監(jiān)視數據的原理,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-02-02
  • vue中v-model動態(tài)生成的實例詳解

    vue中v-model動態(tài)生成的實例詳解

    這篇文章主要介紹了vue中v-model動態(tài)生成的實例詳解的相關資料,希望通過本文能幫助到大家,讓大家理解掌握這部分內容,需要的朋友可以參考下
    2017-10-10

最新評論

封开县| 定安县| 南城县| 普兰店市| 甘肃省| 云安县| 延安市| 平安县| 武隆县| 绥阳县| 延安市| 沁阳市| 清苑县| 同德县| 池州市| 泾川县| 临颍县| 石林| 军事| 镇原县| 太仆寺旗| 遂昌县| 水城县| 库车县| 青田县| 灵石县| 苏州市| 黎平县| 蓝田县| 娄烦县| 包头市| 霍城县| 离岛区| 株洲市| 磐石市| 常山县| 屯门区| 理塘县| 开鲁县| 江城| 高要市|