微信小程序 主题皮肤切换(switch开关)

本文详细介绍了如何在微信小程序中实现主题皮肤的动态切换,包括创建皮肤样式文件、使用switch开关控制皮肤、全局保存和读取皮肤设置、设置标题栏和tabBar的样式。通过这一功能,用户可以自由选择黑色或正常主题,并在应用启动时自动加载上次选择的皮肤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

示例效果:

 

功能点分析:

1.点击switch开关,切换主题皮肤(包括标题栏、底部tabBar);
2.把皮肤设置保存到全局变量,在访问其它页面时也能有效果
3.把设置保存到本地,退出应用再进来时,依然加载上次设置的皮肤

 

步骤:

1、需要切换的皮肤:新建一个skin目录,下面写一个skin.wxss

black_box就是最外面,也是最大的盒子(除了默认的page哈);  black_box就是切换的皮肤;

还有关键一步,在app.wxss文件中把这个皮肤文件引入,因为换肤是所有页面都变化

@import "skin/skin.wxss";

 通过控制skinStyle的值“black”来改变皮肤样式,我们还能写个blue_box的皮肤,然后设置变量为skinStyle为blue就行了

 

<view class="container {{skinStyle}}_box"></view>

 

2、switch开关: 

 user.wxml:

 

<view class="container {{skinStyle}}_box"> 

   <switch bindchange="switchChange" color ="#ef384a" class="switch" checked='{{skinSwitch}}'/>

</view>

 

user.js:根据皮肤开关设置皮肤模式(包括标题栏、tabBar等),并保存到本地; 最后,在每个页面,包括切换皮肤的页面的Page中的onLoad事件里,设置标题栏背景及SkinStyle的值;

1

2

3

4

5

6

7

8

9

10

11

12

13

14

const app = getApp()

 

Page({

  data: {

      skinStyle: '',

  },

  //皮肤开关

  switchChange: function (e) {

      var that = this;

      //开启

      if (e.detail.value == true) {

          app.globalData.skin = "black"

          app.setSkinBlackTitle(); //设置标题栏

          app.globalData.skinSwitch = true<br>          app.setBlackTabBar(); //设置tabBar

1

2

3

4

} else {

    app.globalData.skin = 'normal'

    app.setSkinNormalTitle()

    app.globalData.skinSwitch = false<br>          app.setNormalTabBar();

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

      }

      that.setData({

          skinStyle: app.globalData.skin

      })

      //保存到本地

      wx.setStorage({

          key: "skin",

          data: app.globalData.skin

      })

      wx.setStorage({

          key: "skinSwitch",

          data: app.globalData.skinSwitch

      })

  },

 

  onLoad: function (options) {

      app.setNavBarBg();//设置标题栏背景色

      var that = this     

      that.setData({

          skinStyle: app.globalData.skin,

          skinSwitch: app.globalData.skinSwitch

      })

  }

})

 

app.js:

在app.js的文件中,Page里的globalData中设置:skin:"normal",即默认为normal皮肤;tabBar数据配置;

 我们要在程序打开时就获取皮肤设置,所以要在app.js去get与皮肤、标题、tabBar相关的信息:getSkin() 

复制代码

    //app.js
    App({

        onLaunch: function () {
            console.log('进入app');
            // 展示本地存储能力
            var logs = wx.getStorageSync('logs') || []
            logs.unshift(Date.now())
            wx.setStorageSync('logs', logs)

            this.login();   
          
            this.getSkin(); 
        },
        globalData: {
            userInfo: null,
            skin: 'normal',
            skinSwitch: '',
        },
    
       //设置tabBar -- 默认模式
        setNormalTabBar(){
            wx.setTabBarItem({
                index: 0,
                text: '首页',
                iconPath: "images/icons/home_1.png",
                selectedIconPath: "images/icons/home_2.png",
            })
            wx.setTabBarItem({
                index: 1,
                text: '报名',
                iconPath: "images/icons/apply_1.png",
                selectedIconPath: "images/icons/apply_2.png",
            })
            wx.setTabBarItem({
                index: 2,
                text: '我的',
                iconPath: "images/icons/user_1.png",
                selectedIconPath: "images/icons/user_2.png",
            })
            wx.setTabBarStyle({
                color: '#7f8389',
                selectedColor: '#329fff',
                backgroundColor: '#f7f7fa',
                borderStyle: 'black'
            })
        },
       //设置tabBar -- 黑色模式
        setBlackTabBar(){
            wx.setTabBarItem({
                index: 0,
                text: '首页',
                iconPath: "images/icons/icon_home_1.png",
                selectedIconPath: "images/icons/icon_home_2.png",
            })
            wx.setTabBarItem({
                index: 1,
                text: '报名',
                iconPath: "images/icons/icon_apply_1.png",
                selectedIconPath: "images/icons/icon_apply_2.png",
            })
            wx.setTabBarItem({
                index:2,
                text: '我的',
                iconPath: "images/icons/icon_user_1.png",
                selectedIconPath: "images/icons/icon_user_2.png",
            })
            wx.setTabBarStyle({
                color: '#2e3136',
                selectedColor: '#ef384a',
                backgroundColor: '#ffffff',
                borderStyle: 'black'
            })
        },        
        //皮肤
        getSkin: function () {
            var that = this
            wx.getStorage({
                key: 'skin',
                success: function (res) {
                    that.globalData.skin = res.data
                    if (that.globalData.skin == 'normal') {
                        that.globalData.skinSwitch = false
                        that.setSkinNormalTitle()
                        that.setNormalTabBar();
                    } else {
                        that.globalData.skinSwitch = true
                        that.setSkinBlackTitle()
                        that.setBlackTabBar()
                    }
                }
            })
        },

        //导航栏标题背景
        setNavBarBg: function () {
            var that = this
            if (that.globalData.skin == "normal") {
                that.setSkinNormalTitle()
            } else {
                that.setSkinBlackTitle()
            }
        },
        setSkinBlackTitle: function () {
            wx.setNavigationBarColor({
                frontColor: '#ffffff',
                backgroundColor: '#2e3136',
            })
        },
        setSkinNormalTitle: function () {
            wx.setNavigationBarColor({
                frontColor: '#000000',
                backgroundColor: '#ffffff',
            })
        },   
    }); 

复制代码

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值