亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

vue-element如何實(shí)現(xiàn)動(dòng)態(tài)換膚存儲(chǔ)

 更新時(shí)間:2023年04月25日 09:33:27   作者:微笑的魚(yú)_  
這篇文章主要介紹了vue-element如何實(shí)現(xiàn)動(dòng)態(tài)換膚存儲(chǔ)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

需要實(shí)現(xiàn)的效果

選擇顏色塊或者顏色選擇器切換網(wǎng)站主題色,選擇主題后保存到本地,下次打開(kāi)頁(yè)面是緩存的主題色

在這里插入圖片描述

在這里插入圖片描述

在這里插入圖片描述

在這里插入圖片描述

原理

根據(jù)ElementUI官網(wǎng)的自定義主題,新建一個(gè)樣式文件:element-variables(或者參考官網(wǎng)生成),在文件中寫(xiě)入:

/* 改變主題色變量 */
$--color-primary: #409eff;
$--color-success: #67c23a;
/*....還可以定義更多變量*/

/* 改變 icon 字體路徑變量,必需 */
$--font-path: '~element-ui/lib/theme-chalk/fonts';

@import "~element-ui/packages/theme-chalk/src/index";

//主要的一步:
:export {
  theme: $--color-primary;
}

在項(xiàng)目store文件夾中新建theme.js文件,用于存儲(chǔ)主題色變化狀態(tài),代碼如下

//引入element-variables文件,文件路徑根據(jù)自己項(xiàng)目存放位置來(lái)
import variables from '@/assets/css/element-variables.scss'
const state = {
  theme: variables.theme
}

const getters = {
	theme: function (state) {
		return state.theme;
	}
};

const mutations = {
  CHANGE_SETTING: (state, { key, value }) => {
    // eslint-disable-next-line no-prototype-builtins
    if (state.hasOwnProperty(key)) {
      state[key] = value
    }
  }
}

const actions = {
  changeSetting({ commit }, data) {
    commit('CHANGE_SETTING', data)
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions,
  getters
}

然后在store.js中引入theme.js

import Vue from 'vue';
import Vuex from 'vuex';
import theme from '@/store/theme.js';
Vue.use(Vuex);
export default new Vuex.Store({
	modules: {
		theme
	}
});

切換主圖部分template代碼如下:

<el-form-item label="可選主題" label-width="140px">
	<div class="color-box">
		<div 
			v-for="(item,i) in themeArr"
			:key="i"
			:class="['color-item',theme===item?'active':'']"
			:style="{backgroundColor:item}"
			@click="changeTheme(item)">
			<span v-if="theme===item" class="iconfont icon-f-right"></span>	
		</div>
	</div>
</el-form-item>
<el-form-item label="自定義主題" label-width="140px">
	<el-color-picker
		v-model="theme"
		:predefine="['#409EFF', '#1890ff', '#304156','#212121','#11a983', '#13c2c2', '#6959CD', '#f5222d', ]"
		class="theme-picker"
		popper-class="theme-picker-dropdown"
	/>
</el-form-item>

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 {
			themeArr: ['#409EFF','#202225','#F56C6C','#FFAB42','#17a969','#888C8F'],
			chalk: '',
			theme: ORIGINAL_THEME,
		}
	},
	watch: {
		//異步監(jiān)聽(tīng)theme的變化
		async 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 $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
	        }
	      }
	
		  //  初次進(jìn)入或刷新時(shí)動(dòng)態(tài)加載CSS文件
	      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)
	      })
	
	       // 將修改的主題保存到本地,下次打開(kāi)頁(yè)面是保存的主題色
	    	localStorage.setItem('theme',val)
	    	//調(diào)用vuex中改變主題色方法
			this.$store.dispatch('theme/changeSetting', {
		        key: 'theme',
		        value: val
		    })
	
	      $message.close()
	
	    },
	},
	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()
	      })
	    },
	    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
	    },
		// 顏色塊點(diǎn)擊切換主題事件
		changeTheme(item) {
		  this.theme = item
	      this.$store.dispatch('theme/changeSetting', {
	        key: 'theme',
	        value: item
	      })
		},
	},
	mounted() {
		//從本地存儲(chǔ)獲取保存的主題
		if(localStorage.getItem("theme")) {
			this.theme = localStorage.getItem("theme")
			this.$store.dispatch('theme/changeSetting', {
		        key: 'theme',
		        value: this.theme
		    })
		}
	}
}

顏色塊選擇樣式style:

.color-box {
	display: flex;
	flex-wrap: wrap;
	.color-item {
		position: relative;
		margin: 2px;
		width: 34px;
		height: 34px;
		border-radius: 3px;
		border: 2px solid transparent;
		cursor: pointer;
		span {
			position: absolute;
			right: 0;
			top: 0;
			width: 15px;
			height: 15px;
			background-color: #000;
			color: #fff;
			font-size: 10px;
    	text-align: center;
		}
		&.active {
			border-color: #000;
		}
	}
}

最后感謝:vue-element-admin作者 本實(shí)現(xiàn)方案借鑒于此

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Vue2中如何使用全局事件總線實(shí)現(xiàn)任意組件間通信

    Vue2中如何使用全局事件總線實(shí)現(xiàn)任意組件間通信

    全局事件總線就是一種組件間通信的方式,適用于任意組件間通信,下面這篇文章主要給大家介紹了關(guān)于Vue2中如何使用全局事件總線實(shí)現(xiàn)任意組件間通信的相關(guān)資料,需要的朋友可以參考下
    2022-12-12
  • Vue2 輪播圖slide組件實(shí)例代碼

    Vue2 輪播圖slide組件實(shí)例代碼

    這篇文章主要介紹了Vue2 輪播圖slide組件實(shí)例代碼,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2018-05-05
  • VUE數(shù)組根據(jù)索引刪除數(shù)據(jù),頁(yè)面同時(shí)更新的實(shí)現(xiàn)方法

    VUE數(shù)組根據(jù)索引刪除數(shù)據(jù),頁(yè)面同時(shí)更新的實(shí)現(xiàn)方法

    這篇文章主要介紹了VUE數(shù)組根據(jù)索引刪除數(shù)據(jù),頁(yè)面同時(shí)更新的實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-07-07
  • Vue子組件調(diào)用父組件方法案例詳解

    Vue子組件調(diào)用父組件方法案例詳解

    這篇文章主要介紹了Vue子組件調(diào)用父組件方法案例詳解,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-09-09
  • el-table表格動(dòng)態(tài)合并相同數(shù)據(jù)單元格(可指定列+自定義合并)

    el-table表格動(dòng)態(tài)合并相同數(shù)據(jù)單元格(可指定列+自定義合并)

    工作時(shí)遇到的el-table合并單元格的需求,本文主要介紹了el-table表格動(dòng)態(tài)合并相同數(shù)據(jù)單元格(可指定列+自定義合并),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-07-07
  • vue項(xiàng)目引入百度地圖BMapGL鼠標(biāo)繪制和BMap輔助工具

    vue項(xiàng)目引入百度地圖BMapGL鼠標(biāo)繪制和BMap輔助工具

    這篇文章主要為大家介紹了vue項(xiàng)目引入百度地圖BMapGL鼠標(biāo)繪制和BMap輔助工具的踩坑分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-02-02
  • Vue簡(jiǎn)化用戶查詢/添加功能的實(shí)現(xiàn)

    Vue簡(jiǎn)化用戶查詢/添加功能的實(shí)現(xiàn)

    本文主要介紹了Vue簡(jiǎn)化用戶查詢/添加功能的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-01-01
  • Vue使用echarts的完整步驟及解決各種報(bào)錯(cuò)

    Vue使用echarts的完整步驟及解決各種報(bào)錯(cuò)

    最近在項(xiàng)目中需要對(duì)數(shù)據(jù)進(jìn)行可視化處理,而眾所周知echarts是非常強(qiáng)大的插件,下面這篇文章主要給大家介紹了關(guān)于Vue使用echarts的完整步驟及解決各種報(bào)錯(cuò)的相關(guān)資料,需要的朋友可以參考下
    2022-05-05
  • Vue開(kāi)發(fā)中Jwt的使用詳解

    Vue開(kāi)發(fā)中Jwt的使用詳解

    Vue中使用JWT進(jìn)行身份認(rèn)證也是一種常見(jiàn)的方式,它能夠更好地保護(hù)用戶的信息,本文主要介紹了Vue開(kāi)發(fā)中Jwt的使用詳解,具有一定的參考價(jià)值,感興趣的可以了解一下
    2023-12-12
  • 基于Axios 常用的請(qǐng)求方法別名(詳解)

    基于Axios 常用的請(qǐng)求方法別名(詳解)

    下面小編就為大家分享一篇Axios 常用的請(qǐng)求方法別名,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-03-03

最新評(píng)論