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

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

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);
    }
})
相关推荐
じòぴé南冸じょうげん3 小时前
小程序的project.private.config.json是无依赖文件,那可以删除吗?
前端·小程序·json
2501_916013744 小时前
HTTPS 抓包难点分析,从端口到工具的实战应对
网络协议·http·ios·小程序·https·uni-app·iphone
2501_915918416 小时前
uni-app 项目 iOS 上架效率优化 从工具选择到流程改进的实战经验
android·ios·小程序·uni-app·cocoa·iphone·webview
00后程序员张6 小时前
如何在不同 iOS 设备上测试和上架 uni-app 应用 实战全流程解析
android·ios·小程序·https·uni-app·iphone·webview
微三云-轩7 小时前
区块链:重构企业数字化的信任核心与创新动力
人工智能·小程序·区块链·生活·我店
阿隆_趣编程9 小时前
为了方便相亲,我用AI写了一款小程序
前端·javascript·微信小程序
2501_915918411 天前
iOS 开发全流程实战 基于 uni-app 的 iOS 应用开发、打包、测试与上架流程详解
android·ios·小程序·https·uni-app·iphone·webview
黑马源码库miui520861 天前
JAVA同城打车小程序APP打车顺风车滴滴车跑腿源码微信小程序打车源码
java·微信·微信小程序·小程序·uni-app
一口十个小甜虾1 天前
微信小程序体验版,当打开调试模式正常访问,关闭之后无法访问
微信小程序·小程序
悟空码字1 天前
微信开放平台第三方平台,可以管理多个微信小程序
微信·小程序·开放平台