微信小程序使用tabbar

微信小程序的原生tabbar样式很多地方无法修改,导致无法满足实际需求,此时自定义tabbar成为了最好的选择官方文档

1.根目录下创建custom-tab-bar(自定义文件夹)
在这里插入图片描述
2.编写自定义导航代码
①:index.js

const {myhicky}=require('../utils/api')
Component({
  data: {
    selected: null,
    color: "#7A7E83",	//未选中文字颜色
    selectedColor: "#1296db",	//选中文字颜色
    time:'',
   list: [						//底部list
      {
        pagePath: "/pages/index/index",
        text: "首页",
        iconPath: "/state/image/tab1.png",
        selectedIconPath: "/state/image/tabbar1.png"
      },
      {
        pagePath: "/pages/outlet/outlet",
        text: "查询",
        iconPath: "/state/image/tab2.png",
        selectedIconPath: "/state/image/tabbar2.png"
      },
      {
        pagePath: "/pages/indentList/indentList",
        text: "订单",
        iconPath: "/state/image/tab3.png",
        selectedIconPath: "/state/image/tabbar3.png"
      },
      {
        pagePath: "/pages/personal/personal",
        text: "我的",
        iconPath: "/state/image/tab4.png",
        selectedIconPath: "/state/image/tabbar4.png"
      }
    ]
  },
  attached() {
  },
  methods: {
    switchTab(e) {	//点击跳转时间
      if(this.data.time!=''){	//利用异步判断点击事件只执行一次
        clearTimeout(this.data.time)
        this.setData({
          time:''
        })
      }
      var time=setTimeout(()=>{
        
        const data = e.currentTarget.dataset
        const url = data.path
        wx.switchTab({url})
        console.log('執行')
      },300)
      this.setData({
        time,
      })
    }
 
  }
})

app.json对应tabbarapp.json对应tabbar

②:index.wxml

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

③:tabbar页面的js

const app = getApp()
const {myhicky}=require('../../utils/api')
// const login = require('../../utils/login');
Component({
  data: {
    current: 'tab1',	//顺序
    value:'',
    background: [],
  },
  pageLifetimes: {
    show() {
      if (typeof this.getTabBar === 'function' &&
        this.getTabBar()) {
        this.getTabBar().setData({
          selected: 0	//tabbar顺序
        })
      }
    },
  },
  methods: {
    onLoad() {
      if (wx.getUserProfile) {
        this.setData({
          canIUseGetUserProfile: true
        })
      }
      this.getlist()
      this.slideshow()
    },
    touch(){
      wx.navigateTo({
        url: '../Provinces/Provinces',
      })
    },
    ship(){
      
    },
  }
})
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值