news 2026/9/20 10:10:45

Vue—— Vue3 + Node.js 后台管理系统 之 【主题系统动态应用】

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
Vue—— Vue3 + Node.js 后台管理系统 之 【主题系统动态应用】

背景问题

现代应用需要支持主题切换,包括浅色、深色等多种主题,需要动态调整样式。

方案思考

  • 如何使用CSS变量实现主题切换
  • 如何持久化主题设置
  • 如何优化主题切换性能

具体实现

CSS变量主题:

// style/theme.scss - 主题样式系统 // 默认主题样式 :root { --primary-color: #409eff; --primary-light-1: #53a8ff; --primary-light-2: #66b1ff; --primary-light-3: #79bbff; --primary-light-4: #8cc5ff; --primary-light-5: #a0d2ff; --primary-light-6: #b3dffb; --primary-light-7: #c6e7ff; --primary-light-8: #d9f0ff; --primary-light-9: #ecf7ff; --bg-color: #ffffff; --text-color: #303133; --border-color: #dcdfe6; } // 深色主题样式 html.dark { --primary-color: #337ecc; --primary-light-1: #428bda; --primary-light-2: #519ce8; --primary-light-3: #60aef6; --primary-light-4: #70c2ff; --primary-light-5: #8ed0ff; --primary-light-6: #abe0ff; --primary-light-7: #c9eeff; --primary-light-8: #e6f8ff; --primary-light-9: #f2fbff; --bg-color: #1d1e1f; --text-color: #e4e7ed; --border-color: #4c4d4f; } // 使用主题变量的组件样式 .app-container { background-color: var(--bg-color); color: var(--text-color); border: 1px solid var(--border-color); .primary-button { background-color: var(--primary-color); color: white; &:hover { background-color: var(--primary-light-1); } } }

动态主题应用:

// utils/dynamicTheme.ts - 动态主题应用import{ref,watchEffect}from'vue';// 主题配置interfaceThemeConfig{primaryColor:string;backgroundColor:string;textColor:string;borderColor:string;borderRadius:string;}// 预设主题constpresetThemes:Record<string,ThemeConfig>={light:{primaryColor:'#409eff',backgroundColor:'#ffffff',textColor:'#303133',borderColor:'#dcdfe6',borderRadius:'4px'},dark:{primaryColor:'#337ecc',backgroundColor:'#1d1e1f',textColor:'#e4e7ed',borderColor:'#4c4d4f',borderRadius:'4px'},blue:{primaryColor:'#1890ff',backgroundColor:'#f0f8ff',textColor:'#001529',borderColor:'#91d5ff',borderRadius:'6px'}};// 动态主题应用exportfunctionuseDynamicTheme(){constcurrentTheme=ref('light');// 应用主题constapplyTheme=(themeName:string)=>{consttheme=presetThemes[themeName];if(!theme)return;constroot=document.documentElement;Object.entries(theme).forEach(([key,value])=>{root.style.setProperty(`--${key}`,value);});// 添加主题类名root.className=root.className.replace(/\btheme-\w+\b/g,'');root.classList.add(`theme-${themeName}`);};// 监听主题变化watchEffect(()=>{applyTheme(currentTheme.value);});// 切换主题constswitchTheme=(themeName:string)=>{currentTheme.value=themeName;localStorage.setItem('theme',themeName);};// 获取当前主题constgetCurrentTheme=()=>{returncurrentTheme.value;};// 初始化主题constinitTheme=()=>{constsavedTheme=localStorage.getItem('theme');if(savedTheme&&presetThemes[savedTheme]){currentTheme.value=savedTheme;}};return{currentTheme,switchTheme,getCurrentTheme,initTheme};}

效果验证

通过CSS变量和JavaScript结合,可以实现流畅的主题切换效果。

经验总结

主题系统不仅要考虑视觉效果,还要考虑性能和用户体验,特别是在主题切换过程中的过渡效果。

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/9/17 17:12:05

游戏模组管理工具2024版:多游戏兼容的新手友好解决方案

游戏模组管理工具2024版&#xff1a;多游戏兼容的新手友好解决方案 【免费下载链接】XXMI-Launcher Modding platform for GI, HSR, WW and ZZZ 项目地址: https://gitcode.com/gh_mirrors/xx/XXMI-Launcher XXMI启动器是2024年最受欢迎的多游戏模组管理工具&#xff0c…

作者头像 李华
网站建设 2026/9/13 2:54:27

笔记本散热优化指南:如何通过智能风扇调节提升ThinkPad使用体验

笔记本散热优化指南&#xff1a;如何通过智能风扇调节提升ThinkPad使用体验 【免费下载链接】TPFanCtrl2 ThinkPad Fan Control 2 (Dual Fan) for Windows 10 and 11 项目地址: https://gitcode.com/gh_mirrors/tp/TPFanCtrl2 你是否也曾在重要会议中被笔记本突然加速的…

作者头像 李华
网站建设 2026/9/17 13:46:13

忘记QQ号不用愁!手机号查QQ工具使用指南

忘记QQ号不用愁&#xff01;手机号查QQ工具使用指南 【免费下载链接】phone2qq 项目地址: https://gitcode.com/gh_mirrors/ph/phone2qq 你是否遇到过这样的尴尬&#xff1a;换新手机后想登录QQ&#xff0c;却怎么也想不起自己的QQ号码&#xff1f;或者帮家人找回账号时…

作者头像 李华
网站建设 2026/9/10 20:17:07

WorkshopDL 2024全新指南:高效获取Steam创意工坊资源的完整方案

WorkshopDL 2024全新指南&#xff1a;高效获取Steam创意工坊资源的完整方案 【免费下载链接】WorkshopDL WorkshopDL - The Best Steam Workshop Downloader 项目地址: https://gitcode.com/gh_mirrors/wo/WorkshopDL 当你在GOG平台购买的游戏想要使用Steam创意工坊模组…

作者头像 李华