開發商城總免不了主題色的更換, 總不能一個個顏色手動粘貼復制吧,一鍵切換不香嘛
首先需要用到css3的var()函數,用法如下
1 body { 2 --size: 20px; 3 font-size: var(--size); // 20px 4 padding:var(--size); // 20px 5 }
思路:
- 就是在vue的最外層盒子app中添加主題色的色值, 需要用到主題色的地方皆用變量取值
上代碼:
app.vue中
1 <template> 2 <div id="app" :style="appStyle"> 3 <router-view class="app" ></router-view> 4 </div> 5 </template> 6 7 <script> 8 export default { 9 name: "app", 10 11 computed: { 12 appStyle(){ 13 const themeColor = 1 14 15 // console.log('[App路由啟動函數]', props, context, router) 16 const themeColors = [ 17 '', 18 '#DA251D', 19 '#f56fa6', 20 '#ff605c', 21 '#86b902', 22 '#40baff', 23 ] 24 return `--theme: ${themeColors[themeColor]}` 25 } 26 }, 27 28 }; 29 </script> 30 31 <style lang="scss" rel="stylesheet/scss"> 32 @import "assets/style/index.scss"; 33 34 #app { 35 height: 100%; 36 width: 100%; 37 .app { 38 background: #f5f5f5; 39 padding-top: 45px; 40 } 41 } 42 </style>
通過更改 themeColor 的數值作為 themeColors 數組的索引值來進行更換色值
需要用到的地方, 用以下寫法即可
1 background-color: var(--theme); 2 color: var(--theme);
效果圖


