CSS_scss切换主题

server/2024/9/18 12:31:52/ 标签: css, scss, 前端

目录assets/theme以下新建文件

_handle.scss

css">@import './_themes.scss';// 定义混合指令, 切换主题,并将主题中的所有规则添加到theme-map中
@mixin themify() {// 将themes中规则放入theme-map@each $theme-name,$theme-map in $themes {$theme-map: $theme-map  !global;[data-theme="#{$theme-name}"] & {// 表示包含下面函数 themed(), 类似于插槽@content;$theme-map : null !global;}}
}@function themed($key) {@return map-get($theme-map, $key);
}

_themes.scss

css">$themes: (
light:(main_background: #fff,text_color: #333,text_color1: #868C9A,text_color2: #868C9A,text_color3: #000000, //tab_background: #F5F5F5,tab_background1: #fff, //tab_background2: #868C9A, //active_line: #00D6CA,footer_background: #fff,btn_main: #00D6CA,color_main: #00D6CA,btn_background: #00D6CA, //按钮颜色btn_background1: #eff7ff, //按钮颜色btn_background2: #C8CAD2, //按钮颜色input_background: #F5F5F5,cont_background: #f6f6f6,
),
dark:(main_background: #121212, //主题色text_color: #fff, //文字颜色text_color1: #868D9A, //文字浅色text_color2: #fff, // 资金页面所用对应text_color3: #fff, //tab_background: #242424, //tab切换背景颜色tab_background1: #000, //tab切换背景颜色tab_background2: #242424, //tab切换背景颜色active_line: #00D6CA, //tab选中样式颜色footer_background: #1a1a1a, // 底部颜色btn_main: #00D6CA, //主题色按钮color_main: #00D6CA, //主题色文字btn_background: #474B62, //按钮颜色btn_background1: #112542, //按钮颜色btn_background2: #1B2134, //按钮颜色input_background: #242424, //input背景颜色cont_background: #242424, //title背景色
))

index.scss

css">@import './_handle.scss';.mainBackground {@include themify() {background: themed("main_background") !important;}
}.textColor {@include themify() {color: themed("text_color") !important;}
}

修改vue.config.js文件

const scss_path = `@/assets/theme/index.scss`module.exports = defineConfig({css: {loaderOptions: {scss: {additionalData: `@import "${scss_path}";`},}},
})

app.vue设置默认主题

mounted() {window.document.documentElement.setAttribute('data-theme', '替换本地存储的theme')
},

状态管理设置点击设置主题

// 获取localStorage
const getStorage = function (key) {const str = window.localStorage.getItem(key)if (!str) {return null}return JSON.parse(str)
}// 移除localStorage
const removeStorage = function (key) {window.localStorage.removeItem(key)
}
// 设置sessionStorage
const setSessionStorage = function (key, obj) {let json = JSON.stringify(obj)window.sessionStorage.setItem(key, json)// console.log('设置语言', key, json)
}const changeTheme = (theme) => {if ((navigator.userAgent).indexOf('Html5Plus') > -1) {if (theme == 'light') {plus.navigator.setStatusBarBackground('#ffffff');plus.navigator.setStatusBarStyle('dark'); // 只能是dark 和 light} else {plus.navigator.setStatusBarBackground('#121212');plus.navigator.setStatusBarStyle('light'); // 只能是dark 和 light}} else {let meta = document.querySelector('meta[name="theme-color"]');if (theme == 'light') {meta.setAttribute('content', '#fff');} else {meta.setAttribute('content', '#121212');}}
}export default {state: {theme: getStorage('theme') || 'light'},getters: {theme: state => state.theme},mutations: {SET_THEME: (state, theme) => {state.theme = themewindow.document.documentElement.setAttribute('data-theme', theme)changeTheme(theme)setStorage('theme', theme)},
}

使用

import { mapGetters, mapMutations } from "vuex";export default {data() {return {}},computed: {...mapGetters(['theme'])},methods: {...mapMutations('SET_THEME'),changeModel(type) {this.type = type;this.SET_THEME(type)},/* changeModel() {let type = ''if (this.theme == 'light') {type = 'dark'} else {type = 'light'}this.SET_THEME(type)}, */}
}

http://www.ppmy.cn/server/22846.html

相关文章

Golang Colly批量爬取小红书图片

语言:Golang 库:Iris/Colly 先看输入日志: Saved file: images\20240428190531_2_0.jpg It is image 20240428190532_2_1.jpg Saved file: images\20240428190532_2_1.jpg It is image 20240428190533_2_2.jpg Saved file: images\20240428190533_2_2.jpg It is image 2024…

干货满满,Apollo7周年大会的心得体会

在云计算、人工智能、大数据等技术的助力下,自动驾驶已成为现代科技的一个标志性领域。 Apollo是一个开放、完整、安全的自动驾驶平台,助力开发者快速搭建自动驾驶系统。 Apollo开放平台自诞生以来,就扮演着这一领域的引领者角色。而今&…

AI预测体彩排列3第2套算法实战化测试第6弹2024年4月28日第6次测试

今天继续进行新算法的测试,今天是第6次测试。好了,废话不多说了,直接上图上结果。 2024年4月28日体彩排3预测结果 6码定位方案如下: 百位:3、2、1、0、5、6、7 十位:3、5、6、2、1、0 个位:3、4…

「生存即赚」链接现实与游戏,打造3T平台生态

当前,在线角色扮演游戏(RPG)在区块链游戏市场中正迅速崛起,成为新宠。随着区块链技术的不断进步,众多游戏开发者纷纷将其游戏项目引入区块链领域,以利用这一新兴技术实现商业价值的最大化。在这一趋势中&am…

物联网鸿蒙实训解决方案

一、建设背景 在数字化浪潮汹涌的时代,华为鸿蒙系统以其前瞻的技术视野和创新的开发理念,成为了引领行业发展的风向标。 据华为开发者大会2023(HDC. Together)公布的数据,鸿蒙生态系统展现出了强劲的发展动力&#x…

【Vivado那些事儿】使用 Python 提取 ILA 数据

ILA应该是调试AMD-Xilinx FPGA最常用的IP。 在调试中,我们希望 ILA 中的波形可以提供有关设计问题的所有信息,但情况并非如此。对于复杂的调试,我们还需要将 ILA 捕获的真实数据存储到可以进一步处理的文件中。根据放置 ILA 的位置&#xff0…

企微SCRM私域运营:构建高效客户关系管理的关键路径

随着市场竞争的日益激烈,客户关系管理(CRM)已成为企业提升竞争力的关键所在。而企业微信(企微)作为连接企业与客户的桥梁,其强大的SCRM(社会化客户关系管理)功能为企业提供了私域运营…

u盘量产工具拥有分区功能,它把一个U盘分成数个移动盘,更改U盘介质类型(Fixed 和 Removabe),供大家学习研究参考~

非常受欢迎的u盘量产工具。最新版拥有分区功能,它把一个U盘分成数个移动盘,更改U盘介质类型(Fixed 和 Removabel)。数码之家量产工具官方版不是数据恢复,是对U盘底层硬件信息的恢复(非硬件损坏),使因为底层硬件信息受损电脑无法识…

Golang基础1-基本类型、if、switch、string

基本类型 bool 整数:byte(相当于uint8), rune(相当于int32), int/uint ,int8/uint8 ,int16/uint16 ,int32/uint32 ,int64/uint64 浮点数: float32 ,float64, complex64 ,complex128 array(值类型)、slice、map、chan(引用类型…

『Linux』 第一章 基本操作指令(上)

目录 一. 使用XShell 远程登录Linux 1.下载安装 XShell 2.查看 Linux 主机 ip 3.使用 XShell 登录主机 4.XShell 下的复制粘贴 二. Linux 下基本指令 01. ls 指令 02. pwd命令 03. cd指令 04. touch指令 05. mkdir 指令 * 06. rmdir指令 * && rm指令 * 07.man 指令 …

设计模式(九):组合模式

设计模式(九):组合模式 1. 组合模式的介绍2. 组合模式的类图3. 组合模式的实现 1. 组合模式的介绍 组合模式(Composite Pattern)属于结构型模式,是用于把一组相似的对象当作一个单一的对象。 组合模式依据…

C++设计模式

设计原则 工厂模式 简单工厂模式 #include<iostream> #include<atomic> #include<memory> #include<thread> #include<mutex> #include<queue> using namespace std; //基类 class COperation { public:int m_First;int m_Second;virtual…

Elasticsearch索引监控全面解析

Elasticsearch作为强大的搜索引擎&#xff0c;其索引的健康状态直接影响着数据检索的效率与可靠性。有效的索引监控不仅能够帮助我们及时发现并解决潜在问题&#xff0c;还能指导我们进行性能调优。本文将详细介绍如何利用Elasticsearch内置的监控API&#xff0c;全面监控索引的…

基于uniapp+微信小程序的智能停车场管理小程序,附源码

博主介绍&#xff1a;✌程序员徐师兄、7年大厂程序员经历。全网粉丝12w、csdn博客专家、掘金/华为云/阿里云/InfoQ等平台优质作者、专注于Java技术领域和毕业项目实战✌ &#x1f345;文末获取源码联系&#x1f345; &#x1f447;&#x1f3fb; 精彩专栏推荐订阅&#x1f447;…

鸿蒙开发HarmonyOS4.0入门与实践

鸿蒙开发HarmonyOS4.0 配合视频一起食用&#xff0c;效果更佳 课程地址&#xff1a;https://www.bilibili.com/video/BV1Sa4y1Z7B1/ 源码地址&#xff1a;https://gitee.com/szxio/harmonyOS4 准备工作 官网地址 鸿蒙开发者官网&#xff1a;https://developer.huawei.com/con…

Windows使用bat远程操作Linux并执行命令

背景&#xff1a;让客户可以简单在Windows中能自己执行 Linux中的脚本&#xff0c;傻瓜式操作&#xff01; 方法&#xff1a;做一个简单的bat脚本&#xff01;能远程连接到Linux&#xff0c;并执行Linux命令&#xff01;客户双击就能使用&#xff01; 1、原先上网查询到使用P…

maven-安装maven

解压 修改配置文件 apache-maven-3.6.1\conf\settings.xml 新建文件夹mvn_repo为仓库 配置镜像 <mirror> <id>alimaven</id> <name>aliyun maven</name> <url>http://maven.aliyun.com/nexus/content/groups/public/</url><…

020Node.js的FS模块使用fs.mkdir创建目录

Node.js的FS模块使用fs.mkdir创建目录 //fs.mkdir 创建目录 /*path 将创建的目录路径mode 目录权限&#xff08;读写权限&#xff09;&#xff0c;默认777callback 回调&#xff0c;传递异常参数err*/ const fsrequire(fs);fs.mkdir(./css,(err)>{if(err){console.log(err)…

【深度学习实战(26)】标签处理之语义分割标签转换,数据集划分

一、标签转换 我们在使用labeme标签工具&#xff0c;标注完数据后会获得json文件。在标注结束过后&#xff0c;我们需要通过标签转换操作&#xff0c;生成jpg格式原始图片和png格式mask标签图。 1.1 使用img_b64_to_arr将json标签中二进制图像数据变成numpy格式数据&#xf…

Java基础知识总结(73)

1、今天学了什么 1、线程休眠 &#xff08;1&#xff09;sleep() 如果需要让当前正在执行的线程暂停一段时间&#xff0c;并进入阻塞状态&#xff08;Timed_Waiting)&#xff0c;则可以通过调用Thread类的静态sleep()方法来实现。 static void sleep(long millis)&#xff1a;让…