VUE項(xiàng)目實(shí)現(xiàn)主題切換的多種方法
需求是 做一個(gè)深色主題和淺色主題切換的效果
方法一 多套css
這個(gè)方法也是最簡單,也是最無聊的。
<!-- 中心 -->
<template>
動(dòng)態(tài)獲取父級(jí)class名稱,進(jìn)行一個(gè)父級(jí)class的多次定義
<div :class="className">
<div class="switch" v-on:click="chang()">
{{ className == "box" ? "開燈" : "關(guān)燈" }}
</div>
</div>
</template>
<script>
export default {
name: "Centre",
data() {
return {
className: "box"
};
},
methods: {
// 改變class
chang() {
this.className === "box"
? (this.className = "boxs")
: (this.className = "box");
}
},
};
</script>
<style lang="scss">
當(dāng)class為box 使用witch的css
@import "./style/witch.scss";
當(dāng)class為boxs 使用black的css
@import "./style/black.scss";
.switch {
position: fixed;
top: 4px;
right: 10px;
z-index: 50;
width: 60px;
height: 60px;
background: #fff;
line-height: 60px;
border-radius: 20%;
}
</style>
每個(gè)css文件樣式大致相同,只是最外層的父級(jí)不一樣,分別為.box 和.boxs
方法二 scss動(dòng)態(tài)切換變量
我自己是分為了2個(gè)主要文件來做的
_variable.scss 變量管理文件
var()為css3中提出的聲明樣式變量的方法
var(屬性名,屬性值)注意屬性值不能是字符串
// 主題切換 $bgColor:var(--backgroundColor,rgb(255,255,255)); $fontColor:var(--fonntColor,rgb(0,0,0)); $bgmColor:var(--backgroundMColor,rgb(238,238,238)); $tableColor:var(--tableColor,rgb(218,218,218)); $borderColor:var(--borderColor,rgb(238,238,238)); $tablesColor:var(--tablesColor,rgb(255,255,255)); $inputColor:var(--inputColor,rgb(255,255,255))
創(chuàng)建的_variable.scss 文件我在vue.config.js進(jìn)行了一個(gè)全局的配置,沒有在組件中引入
css: {
loaderOptions: {
// 此文件為主題切換文件
sass: {
prependData: `@import "./src/styles/_variable.scss";`,
},
},
},
2.publicStyle.js
這個(gè)方法可以去修改var定義的變量
document.getElementsByTagName("body")[0].style.setProperty("屬性名", "替換的屬性值f");
// 主題切換
const cut = (cutcheack) => {
document.getElementsByTagName("body")[0].style.setProperty("--backgroundColor", cutcheack ? "#121212" : "#fff");
document.getElementsByTagName("body")[0].style.setProperty("--fonntColor", cutcheack ? "#cecece" : "#333");
document.getElementsByTagName("body")[0].style.setProperty("--backgroundMColor", cutcheack ? "#333" : "#eee");
document.getElementsByTagName("body")[0].style.setProperty("--tableColor", cutcheack ? "#000" : "#d8d8d8");
document.getElementsByTagName("body")[0].style.setProperty("--tablesColor", cutcheack ? "#222" : "#fff");
document.getElementsByTagName("body")[0].style.setProperty("--inputColor", cutcheack ? "#666" : "#fff");
document.getElementsByTagName("body")[0].style.setProperty("--borderColor", cutcheack ? "#666" : "#fff");
};
export default cut;
組件中使用
<!-- 首頁 -->
<template>
<div class='home'>
<el-switch v-model="cutcheack" active-color="#333" inactive-color="#13ce66" active-text="主題" @change="switchs"></el-switch>
</div>
</template>
<script>
import cut from "../../utils/publicStyle.js";
export default {
name: "Home",
data() {
return {
cutcheack: false, //主題切換
};
},
methods: {
// 左側(cè)導(dǎo)航隱藏或顯示
// 切換主題
switchs() {
cut(this.cutcheack);
},
},
};
</script>
<style lang='scss' scope>
.home {
height: 100%;
width: 100%;
background:$bgColor;
.el-container {
height: 100%;
color:$fontColor;
}
}
</style>
到此這篇關(guān)于VUE項(xiàng)目實(shí)現(xiàn)主題切換的文章就介紹到這了,更多相關(guān)VUE 實(shí)現(xiàn)主題切換內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue使用Element實(shí)現(xiàn)增刪改查+打包的步驟
這篇文章主要介紹了Vue使用Element實(shí)現(xiàn)增刪改查+打包的步驟,幫助大家更好的理解和學(xué)習(xí)vue框架,感興趣的朋友可以了解下2020-11-11
基于vue開發(fā)微信小程序mpvue-docs跳轉(zhuǎn)頁面功能
這篇文章主要介紹了基于vue寫微信小程序mpvue-docs跳轉(zhuǎn)頁面,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-04-04
vue百度地圖修改折線顏色,添加icon和文字標(biāo)注方式
這篇文章主要介紹了vue百度地圖修改折線顏色,添加icon和文字標(biāo)注方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03
vue中json格式化顯示數(shù)據(jù)(vue-json-viewer)
這篇文章主要給大家介紹了關(guān)于vue中json格式化顯示數(shù)據(jù)(vue-json-viewer)的相關(guān)資料,Vue-json-viewer是一個(gè)Vue組件,用于在Vue應(yīng)用中顯示JSON數(shù)據(jù)的可視化工具,需要的朋友可以參考下2024-05-05
WebStorm啟動(dòng)vue項(xiàng)目報(bào)錯(cuò)代碼:1080?throw?err解決辦法
在使用webstorm新建vue項(xiàng)目時(shí)常會(huì)遇到一些報(bào)錯(cuò),下面這篇文章主要給大家介紹了關(guān)于WebStorm啟動(dòng)vue項(xiàng)目報(bào)錯(cuò)代碼:1080?throw?err的解決辦法,文中將解決辦法介紹的非常詳細(xì),需要的朋友可以參考下2023-12-12
vue?parseHTML函數(shù)解析器遇到結(jié)束標(biāo)簽
這篇文章主要介紹了vue?parseHTML函數(shù)源碼解析之析器遇到結(jié)束標(biāo)簽的示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07

