欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

微信小程序tabbar底部导航

程序员文章站 2023-11-13 18:27:52
微信小程序重写tabbar底部导航,供大家参考,具体内容如下 1.app.js代码如下: edittabbar: function() { var...

微信小程序重写tabbar底部导航,供大家参考,具体内容如下

1.app.js代码如下:

 edittabbar: function() {
    var e = this.globaldata.tabbar, a = getcurrentpages(), t = a[a.length - 1], s = t.__route__;
    0 != s.indexof("/") && (s = "/" + s);
    for (var n in e.list) e.list[n].selected = !1, e.list[n].pagepath == s && (e.list[n].selected = !0);
    t.setdata({
      tabbar: e
    });
  },
  tabbar: {
    color: "#123",
    selectedcolor: "#1ba9ba",
    borderstyle: "#1ba9ba",
    backgroundcolor: "#fff",
    list: [ {
      pagepath: "/we7/pages/index/index",
      iconpath: "/we7/resource/icon/home.png",
      selectediconpath: "/we7/resource/icon/homeselect.png",
      text: "首页"
    }, {
      pagepath: "/we7/pages/user/index/index",
      iconpath: "/we7/resource/icon/user.png",
      selectediconpath: "/we7/resource/icon/userselect.png",
      text: "我的"
    } ]
  },
  globaldata: {
    userinfo: null,
    tabbar: {
      color: "#333",
      selectedcolor: "#d0501f",
      backgroundcolor: "#ffffff",
      borderstyle: "#d5d5d5",
      list: [ {
        pagepath: "/pages/seller/gzt",
        text: "工作台",
        iconpath: "/pages/images/gzt@3x.png",
        selectediconpath: "/pages/images/gztxz@3x.png",
        selected: !0
      }, {
        pagepath: "/pages/seller/cp/cplb",
        text: "商品",
        iconpath: "/pages/images/dbdc.png",
        selectediconpath: "/pages/images/dbdcxz.png",
        selected: !1
      }, 
      position: "bottom"
    }
  },

template.wxml

<template name="tabbar">
  <view class="tabbar_box" style="background-color:{{tabbar.backgroundcolor}}; border-top-color:{{tabbar.borderstyle}}; {{tabbar.position=='top'?'top:0':'bottom:0'}}">
    <navigator class="tabbar_nav" opentype="redirect" style="width:{{1/tabbar.list.length*100}}%; color:{{item.selected?tabbar.selectedcolor:tabbar.color}}" url="{{item.pagepath}}" wx:for="{{tabbar.list}}" wx:key="index">
      <image class="tabbar_icon" src="{{item.selected?item.selectediconpath:item.iconpath}}"></image>
      <text>{{item.text}}</text>
    </navigator>
  </view>
</template>

3.template.wxss

.tabbar_box {
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  -webkit-box-orient: horizontal;
  -webkit-box-direction: normal;
  -ms-flex-direction: row;
  flex-direction: row;
  -ms-flex-pack: distribute;
  justify-content: space-around;
  position: fixed;
  bottom: 0;
  left: 0;
  z-index: 999;
  width: 100%;
  height: 100rpx;
  border-top: 0.5rpx solid #d5d5d5;
}

.tabbar_nav {
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  -webkit-box-orient: vertical;
  -webkit-box-direction: normal;
  -ms-flex-direction: column;
  flex-direction: column;
  -webkit-box-pack: center;
  -ms-flex-pack: center;
  justify-content: center;
  -webkit-box-align: center;
  -ms-flex-align: center;
  align-items: center;
  font-size: 25rpx;
  height: 100%;
}

.tabbar_icon {
  width: 40rpx;
  height: 40rpx;
}

在需要用的页面(wxml)添加如下代码

<import src="template.wxml"></import>
<template is="tabbar" data="{{tabbar:tabbar}}"></template>

在需要的样式(wxss)添加如下代码

@import "template.wxss";

在需要的js页面添加如下代码:

var dsq, app = getapp();添加在顶部
tabbar: {},在data中添加
app.edittabbar();在onload中添加

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。