wx小程序自定义tabbar

news2024/10/6 4:08:17

1.在app.json文件中,添加自定义tabbar配置:"custom": true

  "tabBar": {
    "custom": true,
    "backgroundColor": "#fafafa",
    "borderStyle": "white",
    "selectedColor": "#40ae36",   
    "color": "#666",
    "list": [
      {
        "pagePath": "pages/index/index",
        "iconPath": "static/images/home.png",
        "selectedIconPath": "static/images/home@selected.png",
        "text": "首页"
      },
      {
        "pagePath": "pages/adoptProduct/adoptProduct",
        "iconPath": "static/images/adopt.png",
        "selectedIconPath": "static/images/adopt@selected.png",
        "text": "认养"
      },
      {
        "pagePath": "pages/supermarket/supermarket",
        "iconPath": "static/images/supermarket.png",
        "selectedIconPath": "static/images/supermarket@selected.png",
        "text": "农副超市"
      },     
      {
        "pagePath": "pages/ucenter/index/index",
        "iconPath": "static/images/my.png",
        "selectedIconPath": "static/images/my@selected.png",
        "text": "我的"
      }
    ]
  }

2.新建根目录文件

index.wxml

<view class="tabBar">
  <view class="cont">
    <block wx:for="{{tabBar.list}}" wx:key="index" class="cont-item">
      <view data-path="{{item.pagePath}}" data-index="{{item.pagePath}}" bindtap="switchTab" class="{{item.search?'search':'item'}} {{tabIndex === index ? 'on' : 'off'}}">
        <image src="{{tabIndex === index  ? item.selectedIconPath : item.iconPath}}"></image>
        <view class="txt {{tabIndex === index ? 'selectedColor' : ''}}">{{item.text}}</view>
      </view>
    </block>
  </view>
</view>

index.wxss

.tabBar {
  z-index: 100;
  width: 100%;
  position: fixed;
  bottom: 0;
  font-size: 28rpx;
  background-color: #fff;
  color: #636363;
}
.cont {
  z-index: 0;
  height: calc(100rpx + env(safe-area-inset-bottom) / 2);
  padding-bottom: calc(env(safe-area-inset-bottom) / 2); 
  padding-bottom: 30rpx;
  display: flex;
  justify-content: space-around;
}
.cont .item {
  font-size: 24rpx;
  position: relative;
  width: 15%;
  text-align: center;
  padding: 0;
  display: block;
  height: auto;
  line-height: 1;
  margin: 0;
  background-color: inherit;
  overflow: initial;
  justify-content: center;
  align-items: center;
  padding-top: 20rpx;
}
.cont .item image {
  width: 50rpx !important;
  height: 50rpx !important;
  margin: auto
}
.cont .selectedColor {
  color: #40ae36;
}
.txt{
  font-size: 24rpx;
}

index.js

//获取应用实例
const app = getApp();
Component({
  data: {},
  methods: {
    switchTab(e) { 
      const data = e.currentTarget.dataset
      const url = data.path
      wx.switchTab({url})
    }
  }
})

index.json

{ 
  "component": true,
  "usingComponents": {}
}

3.在utils添加tab-service.js(通过接口请求,去判断显示那个tabbar)

let tabData = {
  tabIndex: 0,//底部按钮高亮下标
  tabBar: {
      custom: true,
      color: "#5F5F5F",
      selectedColor: "#07c160",
      backgroundColor: "#F7F7F7",
      list: []
  }
}

// 更新菜单
const updateRole = (that, type) => {
 if (type === '0') {
    tabData.tabBar.list=[{
      pagePath: "/pages/index/index",
      iconPath: "/static/images/home.png",
      selectedIconPath: "/static/images/home@selected.png",
      text: "首页"
    },
    {
      pagePath: "/pages/adoptProduct/adoptProduct",
      iconPath: "/static/images/adopt.png",
      selectedIconPath: "/static/images/adopt@selected.png",
      text: "认养"
    },
    {
      pagePath: "/pages/supermarket/supermarket",
      iconPath: "/static/images/supermarket.png",
      selectedIconPath: "/static/images/supermarket@selected.png",
      text: "农副超市"
    },     
    {
      pagePath: "/pages/ucenter/index/index",
      iconPath: "/static/images/my.png",
      selectedIconPath: "/static/images/my@selected.png",
      text: "我的"
    }]
  }else if (type === '1'){
    tabData.tabBar.list= [{
      pagePath: "/pages/index/index",
      iconPath: "/static/images/home.png",
      selectedIconPath: "/static/images/home@selected.png",
      text: "首页"
    },
    {
      pagePath: "/pages/supermarket/supermarket",
      iconPath: "/static/images/supermarket.png",
      selectedIconPath: "/static/images/supermarket@selected.png",
      text: "农副超市"
    },     
    {
      pagePath: "/pages/ucenter/index/index",
      iconPath: "/static/images/my.png",
      selectedIconPath: "/static/images/my@selected.png",
      text: "我的"
    }]
  } 
  updateTab(that);
}
 
// 更新底部高亮
const updateIndex = (that, index) => {
  tabData.tabIndex = index;
  updateTab(that);
}
 
// 更新Tab状态
const updateTab = (that) => {
  if (typeof that.getTabBar === 'function' && that.getTabBar()) {
      that.getTabBar().setData(tabData);
  }
}
 
// 将可调用的方法抛出让外面调用
module.exports = {
  updateRole, updateTab, updateIndex,tabBar:tabData.tabBar.list
}

4.在tabbar对应界面添加

tabService.updateRole(this,'0') // 显示所有tabbar4个
tabService.updateRole(this,'1') // 显示tabbar中的3个
tabService.updateIndex(this, 0)  // tabbar高亮

首页中的index.js

 onShow(){ 
    this.chooseMenu()
    tabService.updateIndex(this, 0)  // tabbar高亮
  }
 chooseMenu(){
        util.request(api.chooseMenu, {
        }, 'GET').then(res => {
          if (res.errno === 0) {
            if(res.data == 1 ) {
              tabService.updateRole(this,'1')
            }else {
              tabService.updateRole(this,'0')
            }
          }
          else{
            util.showErrorToast(res.errmsg);
          }
        })
      }

我的页面中的index.js

 onShow(){ 
    this.chooseMenu()
    tabService.updateIndex(this, 0)  // tabbar高亮
  }
 chooseMenu(){
        util.request(api.chooseMenu, {
        }, 'GET').then(res => {
          if (res.errno === 0) {
            if(res.data == 1 ) {
               tabService.updateRole(this,'1')
               tabService.updateIndex(this, 2)  //由于tabService.updateRole(this,'1')为1,所有少一个认养tabbar,所有updateIndex应传2高亮
            }else {
               tabService.updateRole(this,'0')
               tabService.updateIndex(this, 3) 
            }
          }
          else{
            util.showErrorToast(res.errmsg);
          }
        })
      }

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1800926.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

高精度加法的实现

这是C算法基础-基础算法专栏的第七篇文章&#xff0c;专栏详情请见此处。 引入 在C语言中&#xff0c;int的可存储数据范围是-2147483648~2147483647&#xff0c;long long的可存储数据范围是-9223372036854775808~9223372036854775807&#xff0c;但是如果一些数据比long long…

2021JSP普及组第二题:插入排序

2021JSP普及组第二题 题目&#xff1a; 思路&#xff1a; 题目要求排序后根据操作进行对应操作。 操作一需要显示某位置数据排序后的位置&#xff0c;所以需要定义结构体数组储存原数据的位置和数据本身排序后所得数据要根据原位置输出排序后的位置&#xff0c;所以建立一个新…

Linux lvm卷扩容之SSM

介绍 SSM&#xff08;System Storage Manager&#xff09;是系统存储管理器&#xff0c;它是一种统一的命令行界面&#xff0c;用于管理各种存储设备。通过SSM&#xff0c;用户可以方便地管理、配置和监控存储系统。检查关于可用硬驱和LVM卷的信息。显示关于现有磁盘存储设备、…

新能源汽车内卷真相

导语&#xff1a;2025年&#xff0c;我国新能源汽车总产能预计可达3661万辆&#xff0c;如此产能如何消化&#xff1f; 文 | 胡安 “这样卷下去不是办法&#xff0c;企业目的是什么&#xff1f;是盈利&#xff0c;为国家作贡献&#xff0c;为社会作贡献。我们应该有大格局&…

Stable-Diffusion的WebUI部署

1、环境准备及安装 1.1、linux环境 # 首先&#xff0c;已经预先安装好了anaconda&#xff0c;在这里新建一个环境 conda create -n sdwebui python3.10 # 安装完毕后&#xff0c;激活该环境 conda activate sdwebui# 安装 # 下载stable-diffusion-webui代码 git clone https:…

2024年安全现状报告

2024 年安全现状报告有些矛盾。尽管安全专业人员的道路困难重重&#xff0c;比如说严格的合规要求、不断升级的地缘政治紧张局势和更复杂的威胁环境&#xff0c;但整个行业还是在取得进展。 许多组织表示&#xff0c;与前几年相比&#xff0c;网络安全变得更容易管理。组织之间…

经典文献阅读之--MGS-SLAM(单目稀疏跟踪和高斯映射与深度平滑正则化)

Tip: 如果你在进行深度学习、自动驾驶、模型推理、微调或AI绘画出图等任务&#xff0c;并且需要GPU资源&#xff0c;可以考虑使用UCloud云计算旗下的Compshare的GPU算力云平台。他们提供高性价比的4090 GPU&#xff0c;按时收费每卡2.6元&#xff0c;月卡只需要1.7元每小时&…

线性代数|机器学习-P9向量和矩阵范数

文章目录 1. 向量范数2. 对称矩阵S的v范数3. 最小二乘法4. 矩阵范数 1. 向量范数 范数存在的意义是为了实现比较距离&#xff0c;比如&#xff0c;在一维实数集合中&#xff0c;我们随便取两个点4和9&#xff0c;我们知道9比4大&#xff0c;但是到了二维实数空间中&#xff0c…

认识Spring 中的BeanPostProcessor

关于BeanPostProcessor和BeanFactoryPostProcessors&#xff0c;将分2篇文章来写&#xff0c;这篇文章是对Spring 中BeanPostProcessor进行了总结 先看下大模型对这个类的介绍&#xff0c;随后再看下这两个类的示例&#xff0c;最后看下这两个类的实现。 这两个类从名字看都很类…

堆盘子00

题目链接 堆盘子 题目描述 注意点 SetOfStacks应该由多个栈组成&#xff0c;并且在前一个栈填满时新建一个栈 解答思路 将多个栈存储到一个List中&#xff0c;当入栈时&#xff0c;如果List中最后一个栈容量已经达到cap&#xff0c;则需要新建一个栈&#xff0c;将元素推到…

压缩视频在线压缩网站,压缩视频在线压缩工具软件

在数字化时代&#xff0c;视频成为了人们记录和分享生活的重要载体。然而&#xff0c;视频文件一般都非常大&#xff0c;这不仅占据了大量的存储空间&#xff0c;也给视频的传输和分享带来了不便。因此&#xff0c;压缩视频成为了许多人必须掌握的技能。本文将详细介绍如何压缩…

Golang | Leetcode Golang题解之第138题随机链表的复制

题目&#xff1a; 题解&#xff1a; func copyRandomList(head *Node) *Node {if head nil {return nil}for node : head; node ! nil; node node.Next.Next {node.Next &Node{Val: node.Val, Next: node.Next}}for node : head; node ! nil; node node.Next.Next {if…

【一百零九】【算法分析与设计】树状数组求解前缀最大值,673. 最长递增子序列的个数,树状数组求前缀区间最大值

树状数组求解前缀最大值 树状数组可以求解和前缀区间有关的问题,例如前缀和,前缀区间最值. 可以利用 l o g n log_n logn​的时间复杂度快速查找前缀信息. 利用树状数组查询前缀区间中最大值问题. 树状数组下标1位置存储arr数组下标1位置的最大值. 树状数组2位置存储arr数组1,…

树的重心-java

主要通过深度优先搜索来完成树的重心&#xff0c;其中关于树的重心的定义可以结合文字多加理解。 文章目录 前言☀ 一、树的重心☀ 二、算法思路☀ 1.图用邻接表存储 2.图的遍历 3.算法思路 二、代码如下☀ 1.代码如下&#xff1a; 2.读入数据 3,代码运行结果 总结 前言☀ 主…

《PyTorch 实战宝典》重磅发布!

Pytorch 是目前常用的深度学习框架之一&#xff0c;比起 TF 的框架环境配置不兼容&#xff0c;和 Keras 由于高度封装造成的不灵活&#xff0c;PyTorch 无论是在学术圈还是工业界&#xff0c;都相当占优势。 不夸张地说&#xff0c;掌握了 PyTorch &#xff0c;就相当于走上了…

Cloudpods 强大的多云管理平台部署

简介 Cloudpods 是一款简单、可靠的企业IaaS资源管理软件。帮助未云化企业全面云化IDC物理资源&#xff0c;提升企业IT管理效率。 Cloudpods 帮助客户在一个地方管理所有云计算资源。统一管理异构IT基础设施资源&#xff0c;极大简化多云架构复杂度和难度&#xff0c;帮助企业…

[ue5]建模场景学习笔记(5)——必修内容可交互的地形,交互沙(2)

1需求分析&#xff1a; 继续制作可交互沙子内容&#xff0c;前面我们已经让角色在指定区域留下痕迹&#xff0c;那么能否让区域移动起来&#xff0c;这样才能逐步满足角色走到哪里都能产生交互痕迹&#xff0c;满足更大的地图。 2.操作实现&#xff1a; 1.首先建立角色能产生…

12、SpringBoot 源码分析 - 自动配置深度分析五

SpringBoot 源码分析 - 自动配置深度分析五 refresh和自动配置大致流程OnClassCondition的createOutcomesResolver创建结果解析器StandardOutcomesResolver的resolveOutcomes解析结果StandardOutcomesResolver的getOutcomeClassNameFilter的MISSING判断是否没有 ThreadedOutcom…

【YOLOv5/v7改进系列】改进池化层为SPP、SPPF、SPPCSPC

一、导言 池化层&#xff08;Pooling Layer&#xff09;是卷积神经网络&#xff08;Convolutional Neural Networks, CNNs&#xff09;中的一个重要组成部分&#xff0c;主要用于减少输入数据的空间尺寸&#xff08;例如&#xff0c;图像的宽度和高度&#xff09;&#xff0c;…

3D打印随形透气钢:技术革新引领模具制造新潮流

在模具制造领域&#xff0c;透气钢一直扮演着重要角色&#xff0c;它能够有效解决模具困气问题&#xff0c;提高注塑成型的效率和质量。然而&#xff0c;传统的透气钢制造方法受限于工艺和材料&#xff0c;难以满足复杂模具的需求。随着3D打印技术的飞速发展&#xff0c;3D打印…