首页 > 编程语言 >小程序自定义底部导航 custom-tab-bar

小程序自定义底部导航 custom-tab-bar

时间:2022-12-05 09:35:17浏览次数:34  
标签:bar tab 自定义 custom USERTYPE tabbar images png

1. app.json  

 

 

 

2. 将 custom-tab-bar 放到根目录下(pages同级)

3. custom-tab-bar  代码

{
  "component": true
}

Component({
  data: {
    USERTYPE:'customer',
    selected: 0,
    color: "#7A7E83",
    selectedColor: "#3cc51f",
    listAll: [{
      type:'customer',
      pagePath: "/pages/c_home/index",
      iconPath: "/images/tabbar/home.png",
      selectedIconPath: "/images/tabbar/home-a.png",
      text: "首页"
    }, {
      type:'customer',
      pagePath: "/pages/c_mine/index",
      iconPath: "/images/tabbar/mine.png",
      selectedIconPath: "/images/tabbar/mine-a.png",
      text: "我的"
    },
    {
      type:'business',
      pagePath: "/pages/b_home/index",
      iconPath: "/images/tabbar/b_home.png",
      selectedIconPath: "/images/tabbar/b_home-a.png",
      text: "主页"
    }, {
      type:'business',
      pagePath: "/pages/b_mine/index",
      iconPath: "/images/tabbar/b_mine.png",
      selectedIconPath: "/images/tabbar/b_mine-a.png",
      text: "我的"
    }],
    list:[], 
  },
  observers:{
     'USERTYPE': function(newVal, oldVal){   //监听systemId的数据变化 
      let listAll = this.data.listAll 
      let list = [] 
      if (newVal === 'business') {
        list = listAll.slice(2,4) 
      } else {
        list = listAll.slice(0,2) 
      }
      this.setData({
        list 
      })
    }
  },  
  methods: { 
    switchTab(e) { 
      const data = e.currentTarget.dataset
      const url = data.path 
      wx.switchTab({url}) 
    }
  }
})

 
<view class="tab-bar">
  <view class="tab-bar-border"></view>
  <view wx:for="{{list}}" wx:key="index" class="tab-bar-item" data-path="{{item.pagePath}}" data-index="{{index}}" bindtap="switchTab"> 
    <image src="{{selected === index ? item.selectedIconPath : item.iconPath}}"></image>
    <view style="color: {{selected === index ? selectedColor : color}}">{{item.text}}</view>   
  </view>
</view> 

 .tab-bar {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  height: 96rpx;
  background: white;
  display: flex;
  padding-bottom: env(safe-area-inset-bottom); 
}

.tab-bar-border {
  background-color: rgba(250,250,250 0.33);
  position: absolute;
  left: 0;
  top: 0;
  width: 100%;
  height: 1px;
  transform: scaleY(0.5);
}

.tab-bar-item {
  flex: 1;
  text-align: center;
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
}

.tab-bar-item image {
  width: 27px;
  height: 27px;
}

.tab-bar-item view {
  font-size: 10px;
}

4. 登录成功本地存用户类型

  wx.setStorageSync('USERTYPE', 'business')  or         wx.setStorageSync('USERTYPE', 'customer') 5.tabbar 页面 onShow 
    if (typeof this.getTabBar === 'function' &&
      this.getTabBar()) {
      this.getTabBar().setData({
        selected: 0,
        USERTYPE:wx.getStorageSync('USERTYPE')
      })
    }

6.注意 custom-tab-bar js 代码 custom-tab-bar  USERTYPE 要用 

observers 否则页面跳转会混乱(踩过坑),之前那块的逻辑写到了 created 里面
    

 

标签:bar,tab,自定义,custom,USERTYPE,tabbar,images,png
From: https://www.cnblogs.com/daifuchao/p/16951458.html

相关文章

  • spring源码 自定义beanDefinition的添加逻辑
      首先我自定义了两个beandefinition,第一个会产生第二个,并且都是特殊的实现,能够避开前面的循环读取,所以,spring在最后用了一个while循环,一个flag就将,beandefinition新增......
  • 自定义RBAC(1)
    您好,我是湘王,这是我的51CTO博客,欢迎您来,欢迎您再来~在对Spring Security稍做了解之后,可以知道,SpringSecurity其实只是一个实现认证授权的框架,封装了很多实现细节。但也有一......
  • 自定义RBAC(1)
    您好,我是湘王,这是我的博客园,欢迎您来,欢迎您再来~ 在对Spring Security稍做了解之后,可以知道,SpringSecurity其实只是一个实现认证授权的框架,封装了很多实现细节。但也有......
  • 【C语言】自定义一个模仿strlen()函数功能的函数
    自定义一个模仿strlen()函数功能的函数//my_strlen()函数声明,返回字符数组中字符的个数//参数为字符数组的地址intmy_strlen(char*str);intmain(void){ charch[......
  • Springboot之additional-spring-configuration-metadata.json自定义提示
    【3】@ConfigurationProperties注入属性https://blog.csdn.net/qq_25614773/article/details/124788923 https://docs.spring.io/spring-boot/docs/2.4.7/reference/ht......
  • 15.【C语言进阶】自定义类型
    结构体的声明常规的结构的声明太过简单常见,不再过多阐述。特殊声明在声明结构体的时候可以不完全的声明struct{ inti; doubled; charc;}x;struct{ inti;......
  • el-upload 自定义上传图片(修改文件名,统一上传)
    当前遇到的需求是:从本地选取图片之后,要支持用户自己修改文件名,再统一上传oss第一步:覆盖默认上传行为 代码为:第二步:在图片未上传之前得到图片的“伪协议地址”,循环展......
  • WPF 读写自定义配置文件
    程序集如下usingSystem.Windows;namespaceWpfApp1{///<summary>///MainWindow.xaml的交互逻辑///</summary>publicpartialclassMainW......
  • 【web】自定义协议Protocol URL
    URLProtocol(自定义协议)可以让web页面调用本地exe程序,这个神奇的功能是怎么实现的呢?URLProtocolURLProtocol,没错就是标题里所说的自定义协议。这玩意儿大家最常见的比如......
  • zabbix监控项中的自定义时间
    在zabbix监控项里面可以设置自定义时间,格式如下zabbix自定义时间间隔wd1-7h5-22m/5周一到周日,每天5点到22点,每隔5分钟执行一次h9,10m10-40/30每天9点......