uni-app 动态修改主题色

x33g5p2x  于8个月前 转载在 uni-app  
字(10.1k)|赞(0)|评价(0)|浏览(428)

老是碰到初版制作完成没多久,就整一出说什么要更改整个项目的色彩体系。真的是宝宝心里苦啊!

起初都是通过uni项目自带的uni.scss中定义,在替换页面上对应的css。以便于达到一次性修改整体布局的样式。

一.uni.scss 使用方式

在该文件里定义: $名字 :颜色值;

使用时需要在 style 节点上加上 lang=“scss”

  1. <style lang="scss" scoped>.bg {
  2. height: 120px;
  3. width: 100%;
  4. background-color: $uni-color-primary;
  5. }
  6. </style>

该方法使用,适合单一颜色修改,一次修改全局统一。

二.暗黑主题

暗黑模式(Dark Mode),也被称为夜间模式或深色模式,是一种高对比度,或者反色模式的显示模式。是一种有利于在黑暗环境下观看手机的高对比度的模式。uni-app的暗黑模式,是帮助开发者完成自己应用的暗黑模式的一批配置和API。开发者可以参考本文实现自己应用的暗黑模式。

注:HBuilder X 3.6.9+ 支持 目前只支持深色和浅色

具体介绍看官网地址:https://uniapp.dcloud.net.cn/tutorial/darkmode.html

三.自定义主题配置

可自行定义多种主题配色,通过js动态修改导航栏等色彩。缺点在于,页面加载缓慢时前期会显示出原有的色彩。整体上不影响使用。

注:在APP 微信小程序 H5 都行

1.在根目录下新建 theme 文件夹

css-theme.scss主题适配主要css

css-variate.scss统一颜色值配置

cue-theme.jsvue 混入js

system-theme.js自定义的相关配置

css-theme

主要为使用sass切换主题,百度一下大部分都是按照以下配置,这里不过多介绍

注:uni中使用时 建议这个scss 在 uni.scss 中 引入该scss

  1. /** @author: Jay
  2. * @description: 通过监听 css 变量切换主题色
  3. * @createTime: 2022-12-13 11:29:00
  4. * @introduce: 需要在 uni.scss 中 引入该scss
  5. *///统一配置色彩
  6. @import "./css-variate.scss";
  7. /*---------------------方法一 使用css 控制变量 ---------------------------*/
  8. /*使用方法
  9. .css-theme {
  10. width: 100%;
  11. @include text-color();
  12. @include base-background();
  13. @include border-color();
  14. @include shadow-color();
  15. }
  16. */
  17. /*白天主题 颜色集合 */$day-theme:(
  18. bg-color:$day-bg,
  19. text-color:$day-text,
  20. border-color: $day-border,
  21. shadow-color:$day-shadow
  22. );
  23. /*夜间主题 颜色集合 */$night-theme:(
  24. bg-color:$night-bg,
  25. text-color:$night-text,
  26. border-color: $night-border,
  27. shadow-color: $night-shadow
  28. );
  29. /*玉红主题 颜色集合 */$jade-theme:(
  30. bg-color:$jade-bg,
  31. text-color:$jade-text,
  32. border-color: $jade-border,
  33. shadow-color: $jade-shadow
  34. );
  35. //定义主题对象
  36. $themes: (
  37. day-theme: $day-theme,
  38. night-theme: $night-theme,
  39. jade-theme: $jade-theme
  40. );
  41. // 生成主题背景色样式
  42. @mixin base-background(){@each $themename , $theme in $themes {
  43. &.#{$themename} {background-color:map-get($map: $theme, $key: bg-color);
  44. }}
  45. }
  46. // 生成主题字体色样式
  47. @mixin text-color(){@each $themename , $theme in $themes {
  48. &.#{$themename} {color:map-get($map: $theme, $key: text-color) !important;
  49. }}
  50. }
  51. // 生成主题边框色样式
  52. @mixin border-color($opa: 1.0){@each $themename , $theme in $themes {
  53. &.#{$themename} {border-color:rgba(map-get($map: $theme, $key: border-color), $opa) !important;
  54. }}
  55. }
  56. // 生成主题阴影
  57. @mixin shadow-color(){@each $themename , $theme in $themes {
  58. &.#{$themename} {box-shadow:0 16rpx 32rpx rgba(map-get($map: $theme, $key: shadow-color), 0.4);
  59. }}
  60. }
  61. /*---------------------方法二 使用css 属性选择器 ---------------------------*/
  62. /*使用方法
  63. <view class="padding-sm" :data-theme="cueTheme">
  64. 暗黑模式-官方自带:(只支持 白天黑夜)
  65. </view>
  66. */
  67. /*白天主题 */[data-theme='day-theme'] {background-color:$day-bg;color:$day-text;border-color:$day-border !important;shadow-color:$day-shadow;
  68. }
  69. /*夜间主题 */[data-theme='night-theme'] {background-color:$night-bg;color:$night-text;border-color:$night-border !important;shadow-color:$night-shadow;
  70. }
  71. /*玉红主题 */[data-theme='jade-theme'] {background-color:$jade-bg;color:$jade-text;border-color:$jade-border !important;shadow-color:$jade-shadow;
  72. }

css-theme

uni.scss中引入
  1. /*主题相关颜色 */@import './theme/css-theme.scss';
css-variate

主要为配置主题所需css 颜色值,方便统一修改。

  1. /*主题 统一配置色彩
  2. *///页面背景色
  3. $page-bg:var(--page-bg,#FFFFFF);
  4. // 白天主题
  5. $day-bg: #FFFFFF;
  6. $day-text: #333333;
  7. $day-border: #c8c7cc;
  8. $day-shadow: #c8c7cc;
  9. // 夜间主题
  10. $night-bg: #292929;
  11. $night-text: #FFFFFF;
  12. $night-border: #c8c7cc;
  13. $night-shadow: #FFFFFF;
  14. // 玉红主题
  15. $jade-bg: #c04851;
  16. $jade-text: #FFFFFF;
  17. $jade-border: #eea2a4;
  18. $jade-shadow: #f1939c;
  19. /*需要在js 中使用的css 导出
  20. APP 无法使用
  21. h5 微信小程序有值
  22. */:export {dayBg:$day-bg;nightBg:$night-bg;jadeBg:$jade-bg;
  23. }

css-variate

cue-theme

主要使用 混入 (mixin) ,方便与在页面中复用相同的功能。

该方法主要调用vuex的数据 和 system-theme 中的方法

注:需要在main.js 导入该js

  1. /** @author: Jay
  2. * @description: 监听主题变化
  3. * @createTime: 2022-12-12 15:22:19
  4. */import system from '../theme/system-theme'import {
  5. mapMutations,
  6. mapGetters
  7. } from 'vuex'export default{
  8. install(Vue) {
  9. Vue.mixin({
  10. onShow() {
  11. //修改导航栏 底部 tab
  12. system.setSystemTheme(this.cueTheme)
  13. //获取缓存 背景色
  14. let bgColor = uni.getStorageSync('pageColor') || '';
  15. if(bgColor) {
  16. this.getSystemBg(bgColor)
  17. }
  18. //获取缓存 主题名字
  19. let themeType = uni.getStorageSync('themeType') || '';
  20. if(themeType) {
  21. this.cueGetTheme(themeType)
  22. }
  23. //监听主题状态变化
  24. uni.onThemeChange((res) =>{
  25. //console.log("监听主题状态变化", res.theme);
  26. //黑夜
  27. if (res.theme == 'dark') {
  28. this.cueGetTheme('night-theme')
  29. }
  30. //白天
  31. if (res.theme == 'light') {
  32. //有多个主题时 判断 缓存是否为白色主题
  33. let type = uni.getStorageSync('themeType');
  34. if (type != 'day-theme') {
  35. this.cueGetTheme(type)
  36. } else{
  37. this.cueGetTheme('day-theme')
  38. }
  39. }
  40. });
  41. },
  42. computed: {
  43. //获取vuex 主题参数
  44. ...mapGetters({
  45. cueTheme: 'theme/theme',
  46. pageBg: 'theme/pageColor',
  47. }),
  48. },
  49. methods: {
  50. //修改主题
  51. ...mapMutations({
  52. cueGetTheme: 'theme/GET_THEME',
  53. themeCache: 'theme/SET_THEME_CACHE',
  54. pageColorCache: 'theme/SET_PAGE_COLOR'}),
  55. //设置 全局背景色
  56. getSystemBg() {
  57. //从 主题列表 获取 页面颜色
  58. let bgColor = system.systemThemeBg(this.cueTheme)
  59. //console.log(bgColor);
  60. //缓存 已设置 背景色
  61. this.pageColorCache(bgColor)
  62. }
  63. }
  64. })
  65. }
  66. }

cue-theme

main.js 导入
  1. //监听主题变化
  2. import theme from './theme/cue-theme.js'Vue.use(theme)
system-theme

主要用来放置一些需要重复使用的js。可根据需求自行添加

注: themeList 为系统主题列表参数相关配置,用于全局设置系统导航栏,底部tab颜色值的存放。

注:其中导入 css-variate.scss 在app 没有相关数据返回,h5,微信小程序则有数据返回。其他平台自行测试。

  1. /** @author: Jay
  2. * @description: 主题相关配置
  3. * @createTime: 2022-12-12 17:45:09
  4. */
  5. /*variables APP 拿不到值
  6. h5 微信小程序有值返回
  7. */import variables from './css-variate.scss'export default{
  8. /*系统主题列表
  9. */themeList() {
  10. return[{
  11. title: "白天",
  12. name: "day-theme",
  13. navBg: variables.dayBg,
  14. navBgApp: "#FFFFFF",
  15. tabBg: "",
  16. tabSeleText: "",
  17. tabText: "",
  18. }, {
  19. title: "黑夜",
  20. name: "night-theme",
  21. navBg: variables.nightBg,
  22. navBgApp: "#292929",
  23. tabBg: "",
  24. tabSeleText: "",
  25. tabText: "",
  26. }, {
  27. title: "玉红",
  28. name: "jade-theme",
  29. navBg: variables.jadeBg,
  30. navBgApp: "#c04851",
  31. tabBg: "",
  32. tabSeleText: "",
  33. tabText: "",
  34. }]
  35. },
  36. //根据主题 返回背景色
  37. systemThemeBg(name) {
  38. let color = ''
  39. this.themeList().map((item, index) =>{
  40. if (item.name ===name) {
  41. color =item.navBgApp
  42. }
  43. })
  44. returncolor
  45. },
  46. //根据主题 修改系统 导航栏 底部 tab
  47. setSystemTheme(name) {
  48. this.themeList().map((item, index) =>{
  49. if (item.name ===name) {
  50. //设置页面导航条颜色
  51. this.setNavigationColor(item.name, item.navBgApp)
  52. //设置 tabBar 样式
  53. this.setTabBarColor(item.tabBg, item.tabSeleText, item.tabText)
  54. }
  55. })
  56. },
  57. /*设置页面导航条颜色
  58. name 主题名字 该颜色值只支持2种 故判断对于白天 为 #000 其他均为 #FFF
  59. bgClor 背景色 可以随意修改
  60. */setNavigationColor(name, bgClor) {
  61. let navigationBar ={
  62. //前景颜色值 仅支持 #ffffff 和 #000000
  63. frontColor: name == 'day-theme' ? "#000000" : "#ffffff",
  64. //背景颜色值
  65. backgroundColor: bgClor || "#FFFFFF",
  66. //fail(err) {
  67. //console.error(err)
  68. //}
  69. }
  70. uni.setNavigationBarColor(navigationBar)
  71. },
  72. /*动态 设置 tabBar 样式
  73. */setTabBarColor(bgColor, seleColor, color) {
  74. let tabBar ={
  75. //背景色
  76. backgroundColor: bgColor || '#ffffff',
  77. //文字选中时的颜色
  78. selectedColor: seleColor || '#3cc51f',
  79. //文字默认颜色
  80. color: color || '#7A7E83',
  81. }
  82. uni.setTabBarStyle(tabBar)
  83. }
  84. }

system-theme

2.vuex 配置

使用vuex模块化开发(module)用于区分主题相关设置 与其他需求。

theme.js 模块

注:namespaced: true 主要为 cue-theme 用于模块化调用。缺少这个,在调用cue-theme中的方法时,拿不到所需参数

  1. //主题相关配置
  2. import system from '../../theme/system-theme'const theme ={
  3. namespaced: true,
  4. state: {
  5. theme: "day-theme",
  6. //主题列表
  7. theme: system.themeList(),
  8. //页面背景色
  9. pageColor: "",
  10. },
  11. mutations: {
  12. //设置主题色
  13. GET_THEME(state, provider) {
  14. state.theme =provider
  15. //修改导航栏 底部 tab
  16. system.setSystemTheme(state.theme)
  17. },
  18. //设置主题缓存
  19. SET_THEME_CACHE(state, provider) {
  20. uni.setStorage({
  21. key: 'themeType',
  22. data: provider
  23. });
  24. },
  25. //设置主题缓存
  26. SET_PAGE_COLOR(state, provider) {
  27. state.pageColor =provider
  28. //缓存
  29. uni.setStorage({
  30. key: 'pageColor',
  31. data: provider
  32. });
  33. },
  34. },
  35. getters: {
  36. theme: state =>state.theme,
  37. pageColor: state =>state.pageColor
  38. },
  39. actions: {
  40. }
  41. }
  42. export default theme

theme.js

index.js 全局导出

  1. import Vue from "vue"import Vuex from "vuex"
  2. //登录
  3. import logIn from "./modules/login.js"
  4. //主题切换
  5. import theme from "./modules/theme.js"Vue.use(Vuex)
  6. const store = newVuex.Store({
  7. modules: {
  8. theme,
  9. logIn
  10. }
  11. })
  12. export default store

index.js

main.js中导入
  1. //引入store
  2. import store from 'store/index.js'Vue.prototype.$store = store

3.页面中使用

class="conter" :style="{'--page-bg':pageBg}" 为该页面单独设置背景色 ,需要配合 page 设置页面高度使用

:data-theme="cueTheme" 给view设置data-theme属性,根据名字匹配对应颜色

:class="[cueTheme]" 设置对应的名字, css 中使用 @include text-color();

案例地址: https://gitee.com/jielov/uni-app-tabbar

插件市场地址: https://ext.dcloud.net.cn/plugin?id=10498

  1. <!--* @author: Jay
  2. * @description: 动态修改主题色
  3. * @createTime: 2022-12-12 14:55:31
  4. -->
  5. <template>
  6. <view class="conter":style="{'--page-bg':pageBg}">
  7. <view class="padding margin-bottom-xl css-theme":class="[cueTheme]">
  8. <view class="text-lg text-center text-bold">暗黑模式
  9. </view>
  10. <view class="margin-top-sm"@click="openDoc">uni-app的暗黑模式。<text class="text-blue">点击查看官方文档</text>
  11. </view>
  12. </view>
  13. <view class="css-theme padding":class="[cueTheme]">
  14. <view class="text-center text-bold text-lg">通过判断css 名字修改主题!!!
  15. </view>
  16. <view class="margin-tb-sm text-lg text-center">当前主题:{{cueTheme}}
  17. </view>
  18. <view class="margin-tb-sm text-lg text-center">当前页面背景色:{{pageBg}}
  19. </view>
  20. <view class="flex align-center justify-around">
  21. <button class="cu-btn round"@click="cssEditThemeBut('day-theme')">白天</button>
  22. <button class="cu-btn round"@click="cssEditThemeBut('night-theme')">黑夜</button>
  23. <button class="cu-btn round"@click="cssEditThemeBut('jade-theme')">玉红</button>
  24. </view>
  25. </view>
  26. <view class="padding margin-top-xl":data-theme="cueTheme">
  27. <view class="text-center text-bold text-lg">通过 data-theme 判断 名字修改主题!!!
  28. </view>
  29. </view>
  30. </view>
  31. </template>
  32. <script>export default{
  33. data() {
  34. return{
  35. url: 'https://uniapp.dcloud.net.cn/tutorial/darkmode.html#open-darkmode'};
  36. },
  37. onLoad() {
  38. console.log("当前主题:", this.cueTheme);
  39. },
  40. onShow() {},
  41. methods: {
  42. cssEditThemeBut(e) {
  43. //修改主题
  44. this.cueGetTheme(e)
  45. //设置主题缓存
  46. this.themeCache(e)
  47. //设置 全局背景色
  48. this.getSystemBg()
  49. //js自改变量值 h5 可用
  50. //document.getElementsByTagName('body')[0].style.setProperty('--page-bg', 'red');
  51. },
  52. openDoc() {
  53. //#ifdef APP
  54. plus.runtime.openWeb(this.url);
  55. //#endif
  56. //#ifdef H5
  57. let a =document.createElement('a');
  58. a.href = this.url;
  59. a.target = '__blank';
  60. a.click();
  61. a = null;
  62. //#endif
  63. }
  64. }
  65. }
  66. </script>
  67. <style>
  68. /*全局背景色 */page {height:100%;
  69. }
  70. </style>
  71. <style lang="scss"scoped>// 全局背景色
  72. .conter {height:100%;background-color:$page-bg;
  73. }.css-theme {// border:2px solid;@include text-color();
  74. @include base-background();
  75. @include border-color();
  76. @include shadow-color();
  77. }
  78. </style>

页面代码

四.黑夜 白天主题展示

本文来自博客园,作者:虚乄,转载请注明原文链接:https://www.cnblogs.com/lovejielive/p/16981875.html

相关文章