ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

小程序自带组件自定义tabbar

2020-05-22 10:08:43  阅读:486  来源: 互联网

标签:good 自定义 text image tabbar home 自带 png icon


1.首先建立 custom-tab-bar 文件夹  包含 index.js  index.json index.wxml

// index.js
Component({
  data: {
    selected: 0,
    color: "#7A7E83",
    selectedColor: "#3cc51f",
    list: [{
      pagePath: "/pages/tabBar/home/home",
      iconPath: "/image/icon_home_no.png",
      selectedIconPath: "/image/icon_home.png",
      text: "首页"
    }, {
      pagePath: "/pages/tabBar/good/good",
      iconPath: "/image/icon_good_no.png",
      selectedIconPath: "/image/icon_good.png",
      text: "商品中心"
    }, {
      pagePath: "/pages/tabBar/myPage/myPage",
      iconPath: "/image/icon_my_no.png",
      selectedIconPath: "/image/icon_my.png",
      text: "个人中心"
    }]
  },
  attached() {
  },
  methods: {
    switchTab(e) {
      const data = e.currentTarget.dataset
      const url = data.path
      wx.switchTab({url})
      this.setData({
        selected: data.index
      })
    }
  }
})
// index.json
{
  "component": true,
  "usingComponents":{}
}
<!--miniprogram/custom-tab-bar/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>
/* index.wxss */
.tab-bar {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  height: 48px;
  background: white;
  display: flex;
  padding-bottom: env(safe-area-inset-bottom);
}

.tab-bar-border {
  background-color: rgba(0, 0, 0, 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 cover-image {
  width: 27px;
  height: 27px;
}

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

app.json文件配置

  "tabBar": {
    "custom": true,
    "color": "#7A7E83",
    "selectedColor": "#3cc51f",
    "borderStyle": "black",
    "backgroundColor": "#ffffff",
    "list": [
      {
        "pagePath": "pages/tabBar/home/home",
        "iconPath": "image/icon_home_no.png",
        "selectedIconPath": "image/icon_home.png",
        "text": "首页"
      },
      {
        "pagePath": "pages/tabBar/good/good",
        "iconPath": "image/icon_good_no.png",
        "selectedIconPath": "image/icon_good.png",
        "text": "商品中心"
      },
      {
        "pagePath": "pages/tabBar/myPage/myPage",
        "iconPath": "image/icon_my_no.png",
        "selectedIconPath": "image/icon_my.png",
        "text": "个人中心"
      }
    ]
  },

每个tabbar页面写成组件形式判断

  pageLifetimes: {
    show() {
      if (typeof this.getTabBar === 'function' &&
        this.getTabBar()) {
        this.getTabBar().setData({
          selected: 2
        })
      }
    }
  },

 

标签:good,自定义,text,image,tabbar,home,自带,png,icon
来源: https://www.cnblogs.com/lhl66/p/12935428.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有