zoukankan      html  css  js  c++  java
  • 小程序自定义tabBar

    在page目录下新建一个tabbar文件夹

    在tabbar.wxml中:

    <template name="tabBar">
      <view class="tab-bar" style="color: {{tabBar.color}}; background: {{tarBar.backgroundColor}}; {{tabBar.position=='top'? 'top: 0' : 'bottom: 0'}}; {{tabBar.borderStyle? (tabBar.position=='top'? 'border-bottom: solid 1px '+tabBar.borderStyle + ';' : 'border-top: solid 1px '+tabBar.borderStyle + ';') : ''}}">
      <block wx:for="{{tabBar.list}}" wx:key="index">
        <navigator url="{{item.pagePath}}" open-type="switchTab" class="{{item.clas}}" style="{{item.active? 'color: '+(item.selectedColor? item.selectedColor : tabBar.selectedColor) : ''}}">
          <image src="{{item.selectedIconPath}}" wx:if="{{item.active}}" class="img"></image>
          <image src="{{item.iconPath}}" wx:else="{{!item.active}}" class="img"></image>
          <text class="iconText">{{item.text}}</text>
        </navigator>
        </block>
      </view>
    </template>

    在tabbar.json中:

    {
      "component": true,
      "usingComponents": {}
    }

    在app.js中:(图标自己保存所需图片以及注意图片路径)

    App({
      onLaunch: function () {
        wx.hideTabBar(); //隐藏小程序自带tabBar
      },
      //第一种底部  
      editTabBar: function () {
        //使用getCurrentPages可以获取当前加载中所有的页面对象的一个数组,数组最后一个就是当前页面。
        var curPageArr = getCurrentPages();    //获取加载的页面
        var curPage = curPageArr[curPageArr.length - 1];    //获取当前页面的对象
        var pagePath = curPage.route;    //当前页面url
        if (pagePath.indexOf('/') != 0) {
          pagePath = '/' + pagePath;
        }
        var tabBar = this.globalData.tabBar;
        for (var i = 0; i < tabBar.list.length; i++) {
          if (tabBar.list[i].pagePath == pagePath) {
            tabBar.list[i].active = true;    //根据页面地址设置当前页面状态    
          }else{
            tabBar.list[i].active = false;
          }
        }
        curPage.setData({
          tabBar: tabBar
        });
      },
      //第二种底部,原理同上
      editTabBar1: function () {
        //使用getCurrentPages可以获取当前加载中所有的页面对象的一个数组,数组最后一个就是当前页面。
    
        var curPageArr = getCurrentPages();    //获取加载的页面
        var curPage = curPageArr[curPageArr.length - 1];    //获取当前页面的对象
        var pagePath = curPage.route;    //当前页面url
        if (pagePath.indexOf('/') != 0) {
          pagePath = '/' + pagePath;
        }
        var tabBar = this.globalData.tabBar1;
        for (var i = 0; i < tabBar.list.length; i++) {
          if (tabBar.list[i].pagePath == pagePath) {
            tabBar.list[i].active = true;    //根据页面地址设置当前页面状态    
          } else {
            tabBar.list[i].active = false;
          }
        }
        curPage.setData({
          tabBar: tabBar
        });
      },
      globalData: {
        //第一种底部导航栏显示
        tabBar: {
          "color": "#b2b2b2",
          "selectedColor": "#11415F",
          "backgroundColor": "#fff",
          "borderStyle": "#eee",
          "list": [
            {
              "pagePath": "/pages/home/index/index",
              "text": "首页",
              "iconPath": "../../../images/homeIcon1.png",
              "selectedIconPath": "../../../images/homeIcon.png",
              "clas": "menu-item",
              active: true
            },
            {
              "pagePath": "/pages/my/index/index",
              "text": "我的",
              "iconPath": "../../../images/myIcon1.png",
              "selectedIconPath": "../../../images/myIcon.png",
              "clas": "menu-item",
              active: false
            }
          ],
          "position": "bottom"
        },
        //第二种底部导航栏显示
        tabBar1: {
          "color": "#b2b2b2",
          "selectedColor": "#11415F",
          "backgroundColor": "#fff",
          "borderStyle": "#eee",
          "list": [
            {
              "pagePath": "/pages/home/index/index",
              "text": "首页",
              "iconPath": "../../../images/homeIcon1.png",
              "selectedIconPath": "../../../images/homeIcon.png",
              "clas": "menu-item1",
              active: true
            },
            {
              "pagePath": "pages/invite/index/index",
              "text": "邀约",
              "iconPath": "images/trust.png",
              "selectedIconPath": "images/trust-fill.png",
              "clas": "menu-item1",
              active: false
            },
            {
              "pagePath": "/pages/my/index/index",
              "text": "我的",
              "iconPath": "../../../images/myIcon1.png",
              "selectedIconPath": "../../../images/myIcon.png",
              "clas": "menu-item1",
              active: false
            }
          ],
          "position": "bottom"
        }
      }
    })

    在app.wxss中写上tabbar全局样式:

    .menu-item{  
       50%;
      height: 97rpx;  
      float: left;
      display: flex;
      align-items: center;
      justify-content: center;
      flex-direction: column;
    }  
    .menu-item1{  
       33.3333%;
      height: 97rpx;  
      float: left;
      display: flex;
      align-items: center;
      justify-content: center;
      flex-direction: column;
    }  
    .img{ 
       48rpx;
      height: 48rpx;  
      display: block;
    }
    .tab-bar{  
      position: fixed;  
       100%;
      height: 97rpx;
      z-index: 1;
    }
    .iconText {
      font-size:20rpx;
      font-family:PingFang SC;
      font-weight:500;
    }

    在app.json中tabBar对象中:

    "tabBar": {
        "custom": true,
        "list": [
          {
            "pagePath": "pages/home/index/index"
          },
           {
            "pagePath": "pages/invite/index/index"
          },
          {
            "pagePath": "pages/my/index/index"
          }
        ]
      },

    在所需要使用tabbar的页面中引用:(如index页面:在index.js文件中):

    var app = getApp(); //引用app
    Page({
    
      /**
       * 页面的初始数据
       */
      data: {
       
      },/**
       * 生命周期函数--监听页面加载
       */
      onLoad: function () {
        
      },
    
      /**
       * 生命周期函数--监听页面初次渲染完成
       */
      onReady: function () {
    
      },
    
      /**
       * 生命周期函数--监听页面显示
       */
      onShow: function () {
        app.editTabBar(); //在onShow中使用所需的哪一种tabBar
      },
    
      /**
       * 生命周期函数--监听页面隐藏
       */
      onHide: function () {
    
      },
    
      /**
       * 生命周期函数--监听页面卸载
       */
      onUnload: function () {
    
      },
    
      /**
       * 页面相关事件处理函数--监听用户下拉动作
       */
      onPullDownRefresh: function () {
    
      },
    
      /**
       * 页面上拉触底事件的处理函数
       */
      onReachBottom: function () {
    
      },
    
      /**
       * 用户点击右上角分享
       */
      onShareAppMessage: function () {
    
      }
    })
  • 相关阅读:
    LeetCode:删除有序数组中的重复项
    ABAP新语法之内联声明
    SAL实战练习-全选及按钮事件等
    固定资产创建BAPI_FIXEDASSET_CREATE--含扩展结构字段EXTENSIONIN
    SAP-采购订单-数据输入校验
    外围系统传SAP---OUT2SAP接口测试
    SAP2OUT异步接口测试
    SAP2OUT同步接口测试
    SAP-批量创建货源清单
    BDC-用户锁定及有效期设置程序
  • 原文地址:https://www.cnblogs.com/xiaofang234/p/12652464.html
Copyright © 2011-2022 走看看