vue项目如何做主题切换?方法是什么?
Admin 2021-10-18 群英技术资讯 741 次浏览
vue项目如何做主题切换?在项目中,主题切换的需求还是比较常见的,这篇文章就给大家分享一下怎样做一个深色主题和浅色主题切换的效果,本文给大家分享了两种方法,有需要的朋友可以参考。
方法一 多套css
这个方法也是最简单,也是最无聊的。
<!-- 中心 --> <template> 动态获取父级class名称,进行一个父级class的多次定义 <div :class="className"> <div class="switch" v-on:click="chang()"> {{ className == "box" ? "开灯" : "关灯" }} </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"> 当class为box 使用witch的css @import "./style/witch.scss"; 当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>
每个css文件样式大致相同,只是最外层的父级不一样,分别为.box 和.boxs
方法二 scss动态切换变量
我自己是分为了2个主要文件来做的
_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))
创建的_variable.scss 文件我在vue.config.js进行了一个全局的配置,没有在组件中引入
css: { loaderOptions: { // 此文件为主题切换文件 sass: { prependData: `@import "./src/styles/_variable.scss";`, }, }, },
2.publicStyle.js
这个方法可以去修改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: { // 左侧导航隐藏或显示 // 切换主题 switchs() { cut(this.cutcheack); }, }, }; </script> <style lang='scss' scope> .home { height: 100%; width: 100%; background:$bgColor; .el-container { height: 100%; color:$fontColor; } } </style>
关于“ue项目如何做主题切换”的内容就介绍到这,本文代码具有一定的借鉴价值,感兴趣的朋友可以参考,希望能对大家有帮助,想要了解更多大家可以关注其它的相关文章。
文本转载自脚本之家
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
JS实现数组倒序排列的方法是什么?在JavaScript中,我们能用reverse() 方法来实现元素倒序排列,一些新手可能对于reverse() 方法的使用不是很了解,下面我们就来看看reverse() 是如何实现颠倒数组中元素的顺序的。
这篇文章给大家分享的是有关js实现长图滚动效果的内容,小编觉得挺实用的,在很多常见都能应用到,因此分享给大家做个参考,感兴趣的朋友接下来一起跟随小编看看吧。
这篇文章给大家分享的是用JS实现静态的验证码干扰效果。小编觉得挺实用的,因此分享给大家做个参考,文中示例代码介绍的非常详细,感兴趣的朋友接下来一起跟随小编看看吧。
众所周知,使用element-ui,为了达到减小项目体积的目的 ,我们在实际项目中更多的是采用按需引入的方法, 下面就来讲讲那些我踩过的坑。步骤:第一步:安装 element-ui 时把 element 也安装一下,执行命令 npm i element-ui -S 和 npm i element -S第二步:安装 ba
ES6提供了更接近面向对象(注意:javascript本质上是基于对象的语言)语言的写法,引入了Class(类)这个概念,作为对象的模板,下面这篇文章主要给大家介绍了关于JavaScript ES6中class定义类的相关资料,需要的朋友可以参考下
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008