zoukankan      html  css  js  c++  java
  • 小程序自带组件自定义tabbar

    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
            })
          }
        }
      },
  • 相关阅读:
    1.BMap(百度地图)第二次加载显示不全
    SpringMVC的拦截器
    装饰者模式
    java产生随机数
    VS 常用快捷键
    给包含compid列且值为null ,表的行数据赋值--
    遍历数据库,删除包含指定列的表的行数据-
    DataTable select根据条件取值
    临时表汇总金额
    Redirect url 路径简单介绍
  • 原文地址:https://www.cnblogs.com/lhl66/p/12935428.html
Copyright © 2011-2022 走看看