Uniapp动态切换主题与老年模式的实现方法_javascript技巧
步骤1:定义主题变量
在 uni.scss 或单独的 theme.scss 中定义CSS变量:
/* 默认主题 */:root { --primary-color: #007AFF; --text-color: #333; --bg-color: #FFFFFF; --font-size-base: 14px;}/* 老年模式 */.theme-elderly { --primary-color: #FF6A00; --text-color: #000; --bg-color: #F5F5F5; --font-size-base: 18px;}/* 暗黑主题 */.theme-dark { --primary-color: #4CD964; --text-color: #FFFFFF; --bg-color: #1A1A1A; --font-size-base: 16px;}
步骤2:在页面中使用变量
步骤3:动态切换主题
// 在App.vue或全局状态管理中export default { data() { return { themeClass: '' } }, methods: { switchTheme(theme) { this.themeClass = `theme-${theme}`; // 保存到本地存储 uni.setStorageSync('appTheme', theme); } }, onLaunch() { // 初始化主题 const savedTheme = uni.getStorageSync('appTheme') || 'default'; this.switchTheme(savedTheme); }}
// 定义主题映射$themes: ( default: ( primary-color: #007AFF, text-color: #333, bg-color: #FFFFFF, font-size-base: 14px ), elderly: ( primary-color: #FF6A00, text-color: #000, bg-color: #F5F5F5, font-size-base: 18px ), dark: ( primary-color: #4CD964, text-color: #FFFFFF, bg-color: #1A1A1A, font-size-base: 16px ));// 主题混合@mixin theme() { @each $theme, $map in $themes { .theme-#{$theme} & { $theme-map: () !global; @each $key, $value in $map { $theme-map: map-merge($theme-map, ($key: $value)) !global; } @content; $theme-map: null !global; } }}// 获取主题值的函数@function themed($key) { @return map-get($theme-map, $key);}// 使用示例.text { @include theme() { color: themed('text-color'); font-size: themed('font-size-base'); }}
对于更复杂的主题管理,可以结合 Vuex :
对于需要完全更换样式表的情况:
// 动态加载CSSfunction loadThemECSS(theme) { // 移除旧主题样式 const oldLink = document.getElementById('theme-style'); if (oldLink) { document.head.removeChild(oldLink); } // 创建新链接 const link = document.createElement('link'); link.id = 'theme-style'; link.rel = 'stylesheet'; link.href = `/static/css/theme-${theme}.css`; document.head.appendChild(link);}// 在uniapp中可能需要使用条件编译// #ifdef H5loadThemeCSS('dark');// #endif
老年模式除了主题变化外,通常还需要:
// 在老年模式下隐藏复杂元素
// 保存设置function saveSettings() { uni.setStorage({ key: 'appSettings', data: { theme: this.currentTheme, isElderlyMode: this.isElderlyMode } });}// 读取设置function loadSettings() { const settings = uni.getStorageSync('appSettings'); if (settings) { this.$store.commit('theme/SET_THEME', settings.theme || 'default'); this.$store.commit('theme/TOGGLE_ELDERLY_MODE', settings.isElderlyMode || false); }}
/* 定义主题变量 */:root { /* 默认主题 */ --primary-color: #007AFF; --text-color: #333333; --bg-color: #FFFFFF; --font-size-base: 14px; --btn-padding: 8px 16px;}/* 老年模式 */.theme-elderly { --primary-color: #FF6A00; --text-color: #000000; --bg-color: #F5F5F5; --font-size-base: 18px; --btn-padding: 12px 24px;}/* 暗黑主题 */.theme-dark { --primary-color: #4CD964; --text-color: #FFFFFF; --bg-color: #1A1A1A; --font-size-base: 16px; --btn-padding: 10px 20px;}
{ "pages": [ // ...其他页面 { "path": "pages/theme/theme", "style": { "navigationBarTitleText": "主题切换演示" } } ]}
如果项目未安装Vuex,先安装:
完整项目结构如下:
├── pages│ └── theme│ └── theme.vue # 主题演示页面├── static├── store│ ├── index.js # Vuex主文件│ └── modules│ └── theme.js # 主题模块(可选)├── uni.scss # 全局样式变量├── main.js # 项目入口├── App.vue # 根组件└── pages.json # 页面配置
老年模式布局错乱:
通过以上方案,你可以在 Uniapp 中实现灵活的主题切换和老年模式功能,根据项目需求选择适合的方案或组合使用多种方案。
以上就是Uniapp动态切换主题与老年模式的实现方法的详细内容,更多关于Uniapp动态切换主题的资料请关注脚本之家其它相关文章!
本文地址: https://www.earthnavs.com/jishuwz/ffe87520c3af250577c0.html






















