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

Vue 換膚的示例實(shí)踐

 更新時(shí)間:2018年01月23日 09:00:18   作者:bigggge  
本篇文章主要介紹了Vue 換膚的示例實(shí)踐,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧

最近公司做的項(xiàng)目得到一個(gè)網(wǎng)站換膚的需求,也就是切換主題。那么如何切換主題色呢?切換主題色其實(shí)就是切換 CSS,然而在項(xiàng)目中不僅只有 CSS 需要換膚,圖標(biāo)和圖片也需要跟隨主題進(jìn)行切換。于是,寫一篇文章來記錄下 Vue 中實(shí)現(xiàn)換膚的過程,先看下效果吧。

本文主要分三部分:CSS 切換,圖標(biāo)切換和圖片切換。

CSS切換

關(guān)于 CSS 顏色的切換,我通過搜索,參考了ElementUI 的方案,總的來說分為四步

在 static 目錄下新建一個(gè) theme.css 文件,將需要替換的 CSS 聲明在此文件中

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

.side-bar .account-info {
 background: #8981D8 !important;
}

聲明所有可選的主題,每種顏色都對(duì)應(yīng)于一個(gè)關(guān)鍵詞,方便區(qū)分

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 ,將顏色值替換為關(guān)鍵詞。

 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
}

把關(guān)鍵詞再換回剛剛生成的相應(yīng)的顏色值,并在頁面上添加 style 標(biāo)簽

 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)
}

圖標(biāo)切換

由于項(xiàng)目剛開始做的時(shí)候并沒有考慮到換膚的需求,于是所有圖標(biāo)都是采用 img 標(biāo)簽的方式引用的,

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

這樣就導(dǎo)致無法給 icon 動(dòng)態(tài)切換顏色了,所以,我決定改為 font 文件的方式來使用圖標(biāo)。這里推薦一個(gè)網(wǎng)站 icomoon ,這個(gè)網(wǎng)站可以輕松地將圖片轉(zhuǎn)換成 font 文件。圖標(biāo)也非常適合通過 font 的方式來使用,我們可以更加方便的修改圖標(biāo)的大小和顏色。

通過在線轉(zhuǎn)換,我們將下載下來的 font 文件放入項(xiàng)目中,并新建一個(gè) CSS 文件來聲明所有圖標(biāo)。

@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 類名的方式來引用圖標(biāo)了。

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

為了使主題生效,我們也需要把圖標(biāo)的 CSS 寫入 theme.css 文件中

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

圖片切換

項(xiàng)目中還存在很多占位圖或者其他圖片會(huì)隨著主題的變化而變化。通過引入所有圖片,并用文件名來區(qū)分不同主題所對(duì)應(yīng)的圖片。在點(diǎn)擊切換主題時(shí),切換到主題所對(duì)應(yīng)的文件,就能實(shí)現(xiàn)圖片切換了。為此,我寫了一個(gè) mixin,并在組件中引入 mixin。

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

placeholderMixin

let callback
const 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`)
  }
 }
}

在點(diǎn)擊切換主題時(shí),會(huì)發(fā)射一個(gè) changeTheme 事件,各組件接收到 changeTheme 事件,就會(huì)為圖片重新賦值,也就達(dá)到了切換圖片的效果。

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

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

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

华容县| 剑阁县| 清镇市| 鹤山市| 彰化市| 华容县| 韶山市| 龙里县| 周至县| 大化| 罗源县| 武陟县| 鹤壁市| 余干县| 永福县| 乐昌市| 河源市| 金寨县| 舒城县| 宁夏| 新干县| 宜昌市| 平舆县| 慈溪市| 板桥市| 从江县| 桑植县| 金秀| 湟中县| 莒南县| 宝坻区| 都江堰市| 鹤壁市| 晋州市| 改则县| 神农架林区| 上杭县| 麟游县| 霍邱县| 大足县| 阿鲁科尔沁旗|