微信小程序动态切换窗口主题色

在微信开发者工具中动态切换主题颜色会感觉切换页面时有主题颜色闪烁问题,但在真机调试中没有闪烁问题。

1. 设置支持的主题色

在app.js中添加以下内容:

javascript 复制代码
App({
    /* 省略其它无关代码 */
  
    onLaunch() {
        // 从缓存读取主题色
        const theme = wx.getStorageSync('theme') || this.globalData.theme;
        if (theme) {
            this.globalData.theme = theme
            this.setTheme(theme)
        }
    },
  
    /**
     * 设置主题色
     * @param theme
     */
    setTheme(theme) {
        // 动态更新CSS变量
        wx.setStorageSync('theme', theme)
        this.globalData.theme = theme

        const page = getCurrentPages()[getCurrentPages().length - 1]
        page.setData({theme: this.globalData.themeColors[theme]});
    },

    globalData: {
        theme: 'black', // 默认主题
        themeColors: {
            pink: {
                name:'粉色',
                value: 'pink',
                primary: '#1890ff',
                background: '#ffffff', // 仅支持黑白两色
                text: '#ff55a0'
            },
            black: {
                name:'黑色',
                value: 'black',
                primary: '#E6E7E9',
                background: '#ffffff',
                text: '#000000'
            }
        }
    },
})

2. 创建themeMixin.js文件

建议放到utils目录下:如下代码中会将小程序的窗口颜色、窗口标题、TabBar选中文字设置成指定主题颜色,如果有其他需求,可自行添加。

javascript 复制代码
function applyTheme(pageInstance) {
    const app = getApp()
    const theme = app.globalData.theme
    wx.setNavigationBarColor({
        frontColor: app.globalData.themeColors[theme].background,
        backgroundColor: app.globalData.themeColors[theme].text
    })
    wx.setTabBarStyle({
        selectedColor: app.globalData.themeColors[theme].text
    })
    pageInstance.setData({
        theme: app.globalData.themeColors[theme]
    })
}

module.exports = {
    applyTheme
}

3. 页面js中初始化主题样式

下面是某页面的JS文件

javascript 复制代码
const { applyTheme } = require('../../utils/themeMixin.js')
Page({

    /* 省略其它无关代码 */
  
    onLoad(options) {
      this.applyTheme();
    },
})

4. 页面wxml中应用主题颜色

这里只示例了字体颜色,背景颜色相同道理。

javascript 复制代码
<view style="color:{{theme.text}}">

    /* 省略其它无关代码 */
  
</view>

5. 动态切换主题颜色

javascript 复制代码
// 当前是动态切换主题的页面js省略版本
const {applyTheme} = require('../../utils/themeMixin.js')

Page({

    /* 省略其它无关代码 */

    switchTheme(){
        // 这里的black是模拟赋值,通过选择框等方式让用户动态选择
        const theme = "black"; 
        getApp().setTheme(theme);
        applyTheme(this);
    }
})
相关推荐
玖月晴空31 分钟前
Uniapp 速查文档
前端·微信小程序·uni-app
2501_915918412 小时前
App 上架苹果商店全流程详解 从开发者账号申请到开心上架(Appuploader)跨平台免 Mac 上传实战指南
macos·ios·小程序·uni-app·objective-c·cocoa·iphone
说私域2 小时前
定制开发开源AI智能名片S2B2C商城小程序中的羊群效应应用研究
人工智能·小程序
一匹电信狗2 小时前
【C++】红黑树详解(2w字详解)
服务器·c++·算法·leetcode·小程序·stl·visual studio
CsharpDev-奶豆哥3 小时前
微信小程序通过主键ID修改json数据的技术分享
微信小程序·小程序·json
汤姆yu3 小时前
基于微信小程序的防诈骗管理系统
微信小程序·小程序·防诈骗管理
2501_916007473 小时前
从零开始学习iOS App开发:Xcode、Swift和发布到App Store完整教程
android·学习·ios·小程序·uni-app·iphone·xcode
2501_916008895 小时前
前端工具全景实战指南,从开发到调试的效率闭环
android·前端·小程序·https·uni-app·iphone·webview
2501_915921438 小时前
iOS 26 CPU 使用率监控策略 多工具协同构建性能探索体系
android·ios·小程序·https·uni-app·iphone·webview
狂团商城小师妹8 小时前
JAVA国际版同城打车源码同城服务线下结账系统源码适配PAD支持Android+IOS+H5
android·java·ios·小程序·交友