Vue項目配置、切換主題顏色詳細(xì)教程(mixin+scss方式,簡單高效)
直接開始,高手話不多
但圖多
基本樣式:
紅色主題:
藍(lán)色主題:
看到這里,是不是有人已經(jīng)開始安耐不住了???
Action
一. 首先,引入scss依賴(node-sass, sass-loader)
npm install node-sass sass-loader --save-dev
二.項目樣式文件目錄介紹
1.此處我將項目中的公共樣式文件放到了 src/style目錄下,其中 index.scss是以供全局使用的一些基本樣式,在main.js文件中引入即可全局使用(圖2)。 _theme.scss, _handle.scss兩個文件為我們稍后進(jìn)行主題顏色配置的文件.
三.主題目錄scss文件配置
1.src/style目錄下的_themes.scss,里面可以配置不同的主題配色方案,這里我配置了三個主題顏色,分別為basic、red、blue。
_themes.scss文件內(nèi)容:
$themes: ( basic: ( basic_color: #3064E7,// 主題色 logo_color: #3064E7, // 主題色字體 title_color: #494D50, //新增、注冊、行業(yè)標(biāo)頭字體顏色 foot_tolor: #5E6165, // 頁腳字體顏色 font_color1: #909399, font_color2: #909399, // 小logo圖片 logo_image: url('@/assets/image/logo.png'), // 大logo圖片 big_logo_image: url('@/assets/image/logo_big.png'), // banner圖片 banner_image: url('@/assets/image/basic_banner.png'), // 首頁搜索按鈕 search_btn: linear-gradient(187deg, #5E9DF5 0%, #3064E7 47%), // 注冊動態(tài)下邊框 border_bottom_1: 1px solid #3064E7, // 注冊動態(tài)邊框 card_border_1: 1px solid rgba(208,211,219,1), // 注冊動態(tài)卡片背景 zhuce_card: #fff, // 新聞標(biāo)題聚焦顏色 font_hover: #3064E7, //背景 navbar_background: #fff, background_color2: #f0f2f5, // 新聞時事背景 // news_background: #fff, // 注冊動態(tài)背景 zhuce_background: #fff, // 行業(yè)資訊背景 hangye_background: #fff, background_color3: red, background_color4: #2674e7, //邊框 border_bottom: 5px solid #4554DE, ), red: ( basic_color: #D0021B, logo_color: #fff, title_color: #494D50, //新增、注冊、行業(yè)標(biāo)頭字體顏色 foot_tolor: #5E6165, // 頁腳字體顏色 font_color1: #909399, font_color2: #fff, // 新聞標(biāo)題聚焦顏色 font_hover: #D0021B, // 小logo圖片 logo_image: url('@/assets/image/logo_white.png'), // 大logo圖片 big_logo_image: url('@/assets/theme/redTheme/logo-b.png'), // banner圖片 banner_image: url('@/assets/theme/redTheme/banner-bg.png'), // 首頁搜索按鈕 search_btn: linear-gradient(187deg, #d20000 0%, #da0707de 47%), // 注冊動態(tài)下邊框 border_bottom_1: 2px solid #D0021B , // 注冊動態(tài)邊框 card_border_1: 1px solid rgba(208,211,219,1), // 注冊動態(tài)卡片背景 zhuce_card: #fff, //背景 navbar_background: url('@/assets/theme/basicTheme/top-bg.png'), background_color2: #283142, // 新聞時事背景 // news_background: #fff, // 注冊動態(tài)背景 zhuce_background: #fff, // 行業(yè)資訊背景 hangye_background: #fff, background_color3: #1e6ceb, background_color4: #323e4e, //邊框 border_bottom: 5px solid #fff, ), blue: ( basic_color: #0DECFF , logo_color: #fff, title_color: #fff, //新增、注冊、行業(yè)標(biāo)頭字體顏色 foot_tolor: #B2D4F5 , // 頁腳字體顏色 font_color1: #909399, font_color2: #fff, // 新聞標(biāo)題聚焦顏色 font_hover: #0E458C, // 小logo圖片 logo_image: url('@/assets/image/logo_white.png'), // 大logo圖片 big_logo_image: url('@/assets/theme/redTheme/logo-b.png'), // banner圖片 banner_image: url('@/assets/theme/blueTheme/y.png'), // 首頁搜索按鈕 search_btn: linear-gradient(187deg, #0076FF 0%, #0076FF 47%), // 注冊動態(tài)下邊框 border_bottom_1: 2px solid #0DECFF , // 注冊動態(tài)卡片背景 zhuce_card: #034488, // 注冊動態(tài)邊框 card_border_1: 2px solid rgba(8,93,185,1), //背景 navbar_background: #0E458C, foot_background: linear-gradient(131deg, #005FBC 0%, #08215F 100%), // 新聞時事背景 news_background: url('@/assets/theme/blueTheme/科技藍(lán).png') no-repeat, // 注冊動態(tài)背景 zhuce_background: #033367, // 行業(yè)資訊背景 hangye_background: #033367, background_color2: #283142, background_color3: #1e6ceb, background_color4: #323e4e, //邊框 border_bottom: 5px solid #fff, ), );
2.src/style/下的 _handle.scss來操作上述1中的$theme變量(當(dāng)然兩個文件可以合并,分開寫是想把配置和操作解耦),上代碼:
_handle.scss文件內(nèi)容:
@import "./_theme.scss"; //遍歷主題map @mixin themeify { @each $theme-name, $theme-map in $themes { //!global 把局部變量強(qiáng)升為全局變量 $theme-map: $theme-map !global; //判斷html的data-theme的屬性值 #{}是sass的插值表達(dá)式 //& sass嵌套里的父容器標(biāo)識 @content是混合器插槽,像vue的slot [data-theme="#{$theme-name}"] & { @content; } } } //聲明一個根據(jù)Key獲取顏色的function @function themed($key) { @return map-get($theme-map, $key); } //獲取背景顏色 @mixin background_color($color) { @include themeify { background-color: themed($color)!important; } } //獲取背景圖片 @mixin background($color) { @include themeify { background: themed($color)!important; } } //獲取圖片 @mixin content($color) { @include themeify { content: themed($color)!important; } } //獲取背景圖片 @mixin border_bottom($color) { @include themeify { border-bottom: themed($color)!important; } } //獲取背景圖片 @mixin border($color) { @include themeify { border: themed($color)!important; } } //獲取字體顏色 @mixin font_color($color) { @include themeify { color: themed($color)!important; } } //獲取邊框顏色 @mixin border_color($color) { @include themeify { border-color: themed($color)!important; } }
3.主題樣式文件都配置好了,接下來就是怎么全局引入這兩個文件及 怎么在vue頁面中使用了,此處我們先看 vue頁面中怎么使用 我們在_theme.scss中配置好的樣式:
這里以其中一個頁面為例,其他頁面同理:
4.樣式文件基本都配置完成了,最后說最重要的一點(diǎn):怎么知道當(dāng)前我們需要用的樣式呢?
此處根據(jù)具體情況具體分析,在合適的頁面或位置寫入即可,此處我是放到了 App.vue項目入口文件中,在進(jìn)入文件后 通過window.document.documentElement.setAttribute()方法傳入你當(dāng)前想要使用的主題,此處我傳入的’blue‘(藍(lán)色主題),則vue頁面中使用的即為_theme.scss中的 blue對象下配置好的顏色或者其他屬性,為其他主題色(如’red’,‘basic’同理,前提是_theme.scss文件中有已經(jīng)配置好的這兩個主題色)
總結(jié)
到此這篇關(guān)于Vue項目配置、切換主題顏色(mixin+scss方式)的文章就介紹到這了,更多相關(guān)Vue項目配置切換主題顏色內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
關(guān)于Vue-extend和VueComponent問題小結(jié)
這篇文章主要介紹了Vue-extend和VueComponent問題,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-08-08vue+element-ui集成隨機(jī)驗(yàn)證碼+用戶名+密碼的form表單驗(yàn)證功能
在登入頁面,我們往往需要通過輸入驗(yàn)證碼才能進(jìn)行登入,那我們下面就詳講一下在vue項目中如何配合element-ui實(shí)現(xiàn)這個功能,需要的朋友可以參考下2018-08-08圖文詳解vue中proto文件的函數(shù)調(diào)用
這篇文章主要給大家介紹了vue中proto文件函數(shù)調(diào)用的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用vue具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2021-08-08element的el-tree多選樹(復(fù)選框)父子節(jié)點(diǎn)關(guān)聯(lián)不關(guān)聯(lián)
最近想要實(shí)現(xiàn)多選框關(guān)聯(lián)的功能,但是卻出現(xiàn)了element的el-tree多選樹(復(fù)選框)父子節(jié)點(diǎn)關(guān)聯(lián)不關(guān)聯(lián)的問題,本文就來介紹一下解決方法,一起來了解一下2021-05-05