Vue 換膚的樣本實踐,vue換膚樣本

來源:互聯網
上載者:User

Vue 換膚的樣本實踐,vue換膚樣本

最近公司做的項目得到一個網站換膚的需求,也就是切換主題。那麼如何切換主題色呢?切換主題色其實就是切換 CSS,然而在項目中不僅只有 CSS 需要換膚,表徵圖和圖片也需要跟隨主題進行切換。於是,寫一篇文章來記錄下 Vue 中實現換膚的過程,先看下效果吧。

本文主要分三部分:CSS 切換,表徵圖切換和圖片切換。

CSS切換

關於 CSS 顏色的切換,我通過搜尋,參考了ElementUI 的方案,總的來說分為四步

在 static 目錄下建立一個 theme.css 檔案,將需要替換的 CSS 聲明在此檔案中

.side-bar { background: linear-gradient(#B7A3FF, #879FFF) !important;}.side-bar .account-info { background: #8981D8 !important;}

聲明所有可選的主題,每種顏色都對應於一個關鍵詞,方便區分

colors: [{ themeId: 1, familyPrimary: '#B7A3FF', familySecondary: '#879FFF', sideBarTop: '#8981D8'}, { themeId: 2, familyPrimary: '#FDC5C5', familySecondary: '#F070A0', sideBarTop: '#E7829F'}, { themeId: 3, familyPrimary: '#414D6C', familySecondary: '#2D1E3C', sideBarTop: '#423C50'}]

通過 AJAX 擷取 theme.css ,將顏色值替換為關鍵詞。

 getFile(`/static/theme.css`)  .then(({data}) => {   let style = getStyleTemplate(data)  })function getStyleTemplate (data) { const colorMap = {  '#B7A3FF': 'familyPrimary',  '#879FFF': 'familySecondary',  '#8981D8': 'sideBarTop' } Object.keys(colorMap).forEach(key => {  const value = colorMap[key]  data = data.replace(new RegExp(key, 'ig'), value) }) return data}

把關鍵詞再換回剛剛產生的相應的顏色值,並在頁面上添加 style 標籤

 getFile(`/static/theme.css`)  .then(({data}) => {   let style = getStyleTemplate(data)   writeNewStyle(style, this.color)  })function writeNewStyle (originalStyle, colors) { let oldEl = document.getElementById('temp-style') let cssText = originalStyle Object.keys(colors).forEach(key => {  cssText = cssText.replace(new RegExp(key, 'ig'), colors[key]) }) const style = document.createElement('style') style.innerText = cssText style.id = 'temp-style' oldEl ? document.head.replaceChild(style, oldEl) : document.head.appendChild(style)}

表徵圖切換

由於項目剛開始做的時候並沒有考慮到換膚的需求,於是所有表徵圖都是採用 img 標籤的方式引用的,

 <img src="../../assets/icon_edit.svg">

這樣就導致無法給 icon 動態切換顏色了,所以,我決定改為 font 檔案的方式來使用表徵圖。這裡推薦一個網站 icomoon ,這個網站可以輕鬆地將圖片轉換成 font 檔案。表徵圖也非常適合通過 font 的方式來使用,我們可以更加方便的修改表徵圖的大小和顏色。

通過線上轉換,我們將下載下來的 font 檔案放入項目中,並建立一個 CSS 檔案來聲明所有表徵圖。

@font-face { font-family: 'icomoon'; src: url('../assets/fonts/icomoon.eot?vpkwno'); src: url('../assets/fonts/icomoon.eot?vpkwno#iefix') format('embedded-opentype'), url('../assets/fonts/icomoon.ttf?vpkwno') format('truetype'), url('../assets/fonts/icomoon.woff?vpkwno') format('woff'), url('../assets/fonts/icomoon.svg?vpkwno#icomoon') format('svg'); font-weight: normal; font-style: normal;}[class^="icon-"], [class*=" icon-"] { /* use !important to prevent issues with browser extensions that change fonts */ font-family: 'icomoon' !important; speak: none; font-style: normal; font-weight: normal; font-variant: normal; text-transform: none; line-height: 1; vertical-align: sub; /* Better Font Rendering =========== */ -webkit-font-smoothing: antialiased; -moz-osx-font-smoothing: grayscale;}.icon-edit:before { content: "\e900";}

之後就能通過 CSS 類名的方式來引用表徵圖了。

 <span class="icon-edit"></span>

為了使主題生效,我們也需要把表徵圖的 CSS 寫入 theme.css 檔案中

.icon_edit:before { background-image: linear-gradient(-135deg, #879FFF 0%, #B7A3FF 100%);}

圖片切換

項目中還存在很多佔位元影像或者其他圖片會隨著主題的變化而變化。通過引入所有圖片,並用檔案名稱來區分不同主題所對應的圖片。在點擊切換主題時,切換到主題所對應的檔案,就能實現圖片切換了。為此,我寫了一個 mixin,並在組件中引入 mixin。

<img :src="userImg || placeholderWoman">

placeholderMixin

let callbackconst placeholderMixin = { data () {  return {   placeholderWoman: '',   placeHolderNoReply: '',   placeHolderNothing: ''  } }, created () {  let themeId = localStorage.getItem('themeId')  let theme = themeId2Name(themeId)  this.setThemeValue(theme)  callback = (theme) => {   this.setThemeValue(theme)  }  bus.$on('changeTheme', callback) }, destroyed () {  bus.$off('changeTheme', callback) }, methods: {  setThemeValue (theme) {   this.placeholderWoman = require(`@/assets/placeholder_woman_${theme}.svg`)   this.placeHolderNoReply = require(`@/assets/icon_noreply_${theme}.svg`)   this.placeHolderNothing = require(`@/assets/icon_nothing_${theme}.svg`)  } }}

在點擊切換主題時,會發射一個 changeTheme 事件,各組件接收到 changeTheme 事件,就會為圖片重新賦值,也就達到了切換圖片的效果。

let theme = themeId2Name(this.themeId)bus.$emit('changeTheme', theme)

這樣也就達到了切換主題的效果,但是這種方法需要在幾乎所有業務組件中引入 mixin,如果有更好的方法,歡迎與我交流。

以上就是本文的全部內容,希望對大家的學習有所協助,也希望大家多多支援幫客之家。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.