Vue+scss白天和夜間模式切換功能的實(shí)現(xiàn)方法
本文主要介紹了Vue+scss白天和夜間模式切換功能的實(shí)現(xiàn)方法,分享給大家,具體如下:
效果圖
圖片被壓縮了不夠清晰。
安裝Scss
注:若安裝失敗可以考慮使用cnpm,或者切換npm源等方式安裝。
npm install node-sass --save-dev //安裝node-sass npm install sass-loader --save-dev //安裝sass-loader npm install style-loader --save-dev //安裝style-loader
新建頁(yè)面DarkModelPage.vue
文件所在位置:src/DarkModelPage.vue
命名路由路徑:/dark-model-page
<template>
<div id="DarkModelPage">
</div>
</template>
<script>
export default {
}
</script>
<style scoped lang="scss">
</style>
在src/assets新建目錄scss
在src/assets/scss新建目錄common
然后需要新建三個(gè)scss文件分別是
- _themes.scss
- _handle.scss
- common.scss
_themes.scss
$themes: ( light: ( background_color: #cccccc,//背景色 text-color: rgba(0, 0, 0, 0.65), // 主文本色 ), dark: ( background_color: #181c27,//背景 text-color: rgba(255, 255, 255, 0.65), // 主文本色 ) );
_handle.scss
@import "./_themes.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)識(shí) @content是混合器插槽,像vue的slot
[data-theme="#{$theme-name}"] & {
@content;
}
}
}
//聲明一個(gè)根據(jù)Key獲取顏色的function
@function themed($key) {
@return map-get($theme-map, $key);
}
//獲取背景顏色
@mixin background_color($color) {
@include themeify {
background: themed($color)!important;
}
}
//獲取字體顏色
@mixin font_color($color) {
@include themeify {
color: themed($color)!important;
}
}
common.scss
@import "@/assets/scss/common/_handle.scss"; /** 自定義的公共樣式... **/
DarkModelPage.vue中使用
<template>
<div id="DarkModelPage">
<div>
<h1 class="title">天小天個(gè)人網(wǎng)</h1>
<a class="btn" @click="modelBrn">模式切換</a>
</div>
</div>
</template>
<script>
export default {
name: "DarkModelPage",
data(){
return {
dark:false,
}
},
methods:{
modelBrn(){
this.dark = !this.dark;
if(this.dark){
window.document.documentElement.setAttribute( "data-theme", 'dark' );
}else{
window.document.documentElement.setAttribute( "data-theme", 'light' );
}
},
},
mounted() {
window.document.documentElement.setAttribute( "data-theme", 'light' );
},
}
</script>
<style scoped lang="scss">
@import '@/assets/scss/common/common';
#DarkModelPage{
//在此使用了背景顏色變量
@include background_color("background_color");
//再次使用了文字顏色變量
@include font_color("text-color");
width: 100vw;
height: 100vh;
display: flex;
justify-content:center;
align-items: center;
transition: background 1s , color 0.6s;
.title{
margin-bottom: 20px;
}
.btn{
cursor: pointer;
display: flex;
justify-content: center;
align-items: center;
width: 100px;
height: 40px;
margin: 0 auto;
}
}
</style>
注:如需更多顏色及樣式切換,只需要在_themes.scss設(shè)置好變量,通過(guò) _handle.scss設(shè)置切換函數(shù),即可以在頁(yè)面中通過(guò)該函數(shù)對(duì)指定樣式進(jìn)行設(shè)置。
本文演示視頻: 點(diǎn)擊瀏覽
到此這篇關(guān)于Vue+scss白天和夜間模式切換功能的實(shí)現(xiàn)方法的文章就介紹到這了,更多相關(guān)Vue 白天和夜間模式切換 內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
用Vue實(shí)現(xiàn)頁(yè)面訪問(wèn)攔截的方法詳解
大家在做前端項(xiàng)目的時(shí)候,大部分頁(yè)面, 游客都可以直接訪問(wèn) , 如遇到 需要登錄才能進(jìn)行的操作,頁(yè)面將提示并跳轉(zhuǎn)到登錄界面,那么如何才能實(shí)現(xiàn)頁(yè)面攔截并跳轉(zhuǎn)到對(duì)應(yīng)的登錄界面呢,本文小編就來(lái)給大家介紹一下Vue實(shí)現(xiàn)頁(yè)面訪問(wèn)攔截的方法,需要的朋友可以參考下2023-08-08
vue3中實(shí)現(xiàn)拖拽排序代碼示例(vue-draggable-next的使用)
在Vue3中使用拖拽功能時(shí)應(yīng)選用vue-draggable-next插件,傳統(tǒng)的draggable插件不兼容Vue3,可能導(dǎo)致TypeError錯(cuò)誤,安裝后,需在項(xiàng)目中引入并使用,具體步驟包括安裝插件、引入使用、查看效果和相關(guān)說(shuō)明,需要的朋友可以參考下2024-09-09
vue elementui表格獲取某行數(shù)據(jù)(slot-scope和selection-change方法使用)
這篇文章主要介紹了vue elementui表格獲取某行數(shù)據(jù)(slot-scope和selection-change方法使用),本文通過(guò)示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-01-01
Vue使用Echarts實(shí)現(xiàn)數(shù)據(jù)可視化的方法詳解
這篇文章主要為大家詳細(xì)介紹了Vue使用Echarts實(shí)現(xiàn)數(shù)據(jù)可視化的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助2022-03-03
Antd中單個(gè)DatePicker限定時(shí)間輸入范圍操作
這篇文章主要介紹了Antd中單個(gè)DatePicker限定時(shí)間輸入范圍操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-10-10
利用vue + koa2 + mockjs模擬數(shù)據(jù)的方法教程
這篇文章主要給大家介紹了關(guān)于利用vue + koa2 + mockjs模擬數(shù)據(jù)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2017-11-11

