微信小程序自定義tabBar組件開發(fā)

博客地址:http://blog.mambaxin.com

1.新建template文件夾用于保存tabBar模板,template/template.wxml

<template name="tabBar">
  <view class="tabBar">
    <block wx:for="{{tabBar}}" wx:for-item="item" wx:key="tabBar">
        <view class="tabBar-item">
            <navigator open-type="reLaunch" url="{{item.pagePath}}">
                <view><image class="icon" src='{{item.iconPath}}'></image></view>
                <view class="{{item.current== 1 ? 'tabBartext' :''}}">{{item.text}}</view>
            </navigator>
        </view>
    </block>
 </view>
</template>

2.創(chuàng)建template.wxss

.icon{
  width:54rpx;
  height: 54rpx;
}
.tabBar{
  width:100%;
  position: fixed;
  bottom:0;
  padding:10rpx;
  margin-left:-4rpx;
  background:#F7F7FA;
  font-size:24rpx;
  color:#8A8A8A;
  box-shadow: 3rpx 3rpx 3rpx 3rpx #aaa; 
  z-index: 9999;
}

 .tabBar-item{
  float:left;
  width:20%;
  text-align: center;
  overflow: hidden;
}
/*當(dāng)前字體顏色*/
.tabBartext{
  color:red;
}
.navigator-hover{
  background-color: rgba(0, 0, 0, 0);
}

3.創(chuàng)建template.js,初始化數(shù)據(jù)

//初始化數(shù)據(jù)
function tabbarinit() {
  return [
    {
      "current": 0,
      "pagePath": "/pages/dashboard/index",
      "iconPath": "/images/goback.png",
      "text": "返回商城"
    },
    {
      "current": 0,
      "pagePath": "/pages/collage/index",
      "iconPath": "/images/collage1.png",
      "selectedIconPath": "/images/collage.png",
      "text": "拼團(tuán)首頁"
    },
    {
      "current": 0,
      "selectedIconPath": "/images/list.png",
      "iconPath": "/images/list1.png",
      "pagePath": "/pages/collage-list/index",
      "text": "活動列表"  

    },
    {
      "current": 0,
      "selectedIconPath": "/images/collage-order.png",
      "iconPath": "/images/collage-order1.png",
      "pagePath": "/pages/collage-order/index",
      "text": "我的訂單"  
    },
    {
      "current": 0,
      "selectedIconPath": "/images/group.png",
      "iconPath": "/images/group1.png",
      "pagePath": "/pages/group/index",
      "text": "我的團(tuán)"
    }
  ]

}
//tabbar 主入口
function tabbarmain(bindName = "tabdata", id, target) {
  var that = target;
  var bindData = {};
  var otabbar = tabbarinit();
  otabbar[id]['iconPath'] = otabbar[id]['selectedIconPath']//換當(dāng)前的icon
  otabbar[id]['current'] = 1;
  bindData[bindName] = otabbar
  that.setData({ bindData });
}

module.exports = {
  tabbar: tabbarmain
}

4.使用方法

  • 先把樣式文件載入app.wxss
@import "/template/template.wxss";
  • 新建一個頁面,比如index.wxml,引入模板
<import src="../../template/template.wxml"/>
<template is="tabBar" data="{{tabBar:bindData.tabBar}}"/>

  • index.js 初始化數(shù)據(jù)
var template = require('../../template/template.js');

Page({

  onLoad: function () {
    template.tabbar("tabBar", 0, this)//0表示第一個tabbar
    this.getData();
  },
})
  • 其它新建頁面也跟index.wxml一樣,初始化數(shù)據(jù)。

效果如圖

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容