当前位置:网站首页>小程序_动态设置tabBar主题皮肤
小程序_动态设置tabBar主题皮肤
2022-08-05 04:10:00 【逸曦穆泽】
序:需要动态设置页面的一些背景、按钮状态、文字的颜色,图片、图标等等,包括tabBar的图标和文字的颜色,切换按钮统一设置,这个在页面上实现比较容易,按理说tabBar的设置也是简单的,确实是!只要你可以调用tabBar组件的方法即可实现切换主题皮肤,关键在于你会不会使用小程序的方法了。
1、tabBar自定义
配置自定义tabBar的方法可以参考官方文档,这个就不用我累赘了,先熟悉这个,在言其他;
2、动态设置背景、按钮状态、文字的颜色,图片、图标等等
前提工作:主题样式文件 common/theme.wxss和主题图片文件夹/static/image/v1,/static/image/v2,如下图:

在每个需要主题替换样式 xxx.wxss 头部引入主题文件
@import "./../common/theme.wxss";1)首先在 app.js 全局文件中设定一个主题参数
globalData: {
theme: 1, // 主题
}data: {
theme: 1,
},
onShow(){
let that = this;
that.setData({
theme: app.globalData.theme,
})
},.wxml
<view class="limit_width theme_v{
{theme}}"> ... </view>3)最后在设置页面上的操作
setup.wxml
<view class="limit_width theme_v{
{theme}}">
<view class="item">
<view class="theme_tle">设置主题:</view>
<view class="flex">
<view bindtap="themeTab" class="theme_tab {
{theme==1?'tab_bg_br':''}}" data-num="1">新绿(默认)</view>
<view bindtap="themeTab" class="theme_tab {
{theme==2?'tab_bg_br':''}}" data-num="2">海蓝</view>
</view>
</view>
</view>setup.js
data: {
theme: 1,
},
onLoad(options) {
this.setData({
theme: app.globalData.theme,
})
},
// 主题切换(逸曦穆泽)
themeTab(e){
let num = parseInt(e.currentTarget.dataset.num);
this.setData({theme: num});
app.globalData.theme = num; // 全局更新
this.getTabBar().watchBack(); // (逸曦穆泽)调用自定义 tabbar 的方法
// 保存到本地
wx.setStorage({
key: "themeSkin",
data: num,
})
},3、动态设置tabBar
// (逸曦穆泽)custom-tab-bar/index.js
const app = getApp();
let themeColor = ["#515151","#515151","#000"];
let themeActiveColor = ["#94AC56","#94AC56","#185F95"];
Component({
data: {
theme: 1,
selected: 1,
color: "#515151",
selectedColor: "#94AC56",
list: [{
"pagePath": "/pages/home/home",
"iconPath": "/static/image/nav_home.png",
"selectedIconPath": "/static/image/v1/nav_home.png",
"text": "首页"
},{
"pagePath": "/pages/setup/setup",
"iconPath": "/static/image/nav_setup.png",
"selectedIconPath": "/static/image/v1/nav_setup.png",
"text": "设置"
}],
},
methods: {
// (逸曦穆泽)监听并回调
watchBack() {
var that = this;
let num = app.globalData.theme;
let listUpd = [{
"pagePath": "/pages/home/home",
"iconPath": "/static/image/nav_home.png",
"selectedIconPath": "/static/image/v"+num+"/nav_home.png",
"text": "首页"
},{
"pagePath": "/pages/setup/setup",
"iconPath": "/static/image/nav_setup.png",
"selectedIconPath": "/static/image/v"+num+"/nav_setup.png",
"text": "设置"
}];
that.setData({
theme: num,
color: themeColor[num],
selectedColor: themeActiveColor[num],
list: listUpd,
})
},
switchTab(e){
var that = this;
const data = e.currentTarget.dataset;
const url = data.url;
wx.switchTab({ url });
that.setData({
selected: data.index
});
}
},
})
.wxml
<view class="tabbar theme_v{
{theme}}"> ... </view>说简单呢,其实也很简单,只是使用了主题样式文件中的类层级样式,js 控制了 theme 参数;
难点在于 tabbar 的切换,怎么调用?肯定是调用 custom-tab-bar/index.js 下的方法,获取全局主题参数设置进去;还有一个主题回显的问题,可以自己去实现,这个也很简单,当然,实在需要的话去下面留言即可。
效果图就不贴了,这个涉及公司与客户的东西!
参考文章:
边栏推荐
- Bytebuffer put flip compact clear method demonstration
- Spark基础【介绍、入门WordCount案例】
- Static method to get configuration file data
- Qixi Festival earn badges
- 【8.2】代码源 - 【货币系统】【硬币】【新年的问题(数据加强版)】【三段式】
- Solana NFT开发指南
- 35岁的软件测试工程师,月薪不足2W,辞职又怕找不到工作,该何去何从?
- 1007 Climb Stairs (greedy | C thinking)
- How to discover a valuable GameFi?
- 1007 Climb Stairs (贪心 | C思维)
猜你喜欢

MRTK3 develops Hololens application - gesture drag, rotate, zoom object implementation

A 35-year-old software testing engineer with a monthly salary of less than 2W, resigns and is afraid of not finding a job, what should he do?

Swing有几种常用的事件处理方式?如何监听事件?

多列属性column元素的可见性:display、visibility、opacity、垂直对齐方式:vertical-align、z-index 越大越显示在上层

Detailed and comprehensive postman interface testing practical tutorial

Web3.0 Dapps - the road to the future financial world

This year's Qixi Festival, "love vegetables" are more loving than gifts

Android 面试题——如何徒手写一个非阻塞线程安全队列 ConcurrentLinkedQueue?

YYGH-13-Customer Service Center

【Mysql进阶优化篇02】索引失效的10种情况及原理
随机推荐
Redis1:Redis介绍、Redis基本特性、关系型数据库、非关系型数据库、数据库发展阶段
Bosses, I noticed that a mysql CDC connector parameters scan. The incremental. Sna
Ffmpeg - sources analysis
炎炎夏日教你利用小米智能家居配件+树莓派4接入Apple HomeKit
Haproxy搭建Web群集
The most effective seven performance testing techniques of software testing techniques
UE4 更改组件变量 (以修改第一人称角色模板的最大行走速度和跳跃高度为例)
Bytebuffer put flip compact clear method demonstration
Acid (ACID) Base (BASE) Principles for Database Design
The first performance test practice, there are "100 million" a little nervous
事件解析树Drain3使用方法和解释
Increasing leetcode - a daily topic 1403. The order of the boy sequence (greed)
What is the function of industrial-grade remote wireless transmission device?
Web3.0 Dapps - the road to the future financial world
【8.2】代码源 - 【货币系统】【硬币】【新年的问题(数据加强版)】【三段式】
工业级远距离无线传输装置的功能有哪些?
There are several common event handling methods in Swing?How to listen for events?
四位数显表头设计
[Geek Challenge 2019]FinalSQL
Use Unity to publish APP to Hololens2 without pit tutorial