• 本网豪情赞助商

  • login failed for display 0 ubuntuallowed_u
  • css设定文本超出一行或多行就隐藏并显示省略
  • css设定文本超出一行或多行就隐藏并显示省略
  • 微信小程序如何把接口调用成功的回调函数返回
  • CodeIgniter - 数据库的增删改查
  • php生成excel的三种方式
  • 小程序UI库推荐
  • 多个句子竖向排列
  • 美团,大众点评,58城市行政区域和商圈数据实
  • java.security.InvalidKeyException: Illega
  • h3>

    微信小程序中动态设置TabBar并解决闪动问题


    文章摘要: 微信小程序中动态设置TabBar并解决闪动问题


    文章TAG:

    https://blog.csdn.net/qq_41213166/article/details/90903704

    微信小程序中动态设置TabBar并解决闪动问题

    2019年06月05日 16:11:33 magic_engineer 阅读数 327
     

    最近接到一个需求用户在登录微信小程序时需要根据角色显示不同的tabbar内容,解决思路大致如下:

    • 创建自定义TabBar的模板tabbar.wxml,本例放置在pages目录下创建的template文件夹中
    
    		
    1.  
      <template name="tabBar">
    2.  
      <view class="tabbar" style="color: {{tabBar.color}}; background: {{tabBar.backgroundColor}}; {{tabBar.position=='top'? 'top: 0' : 'bottom: 0'}}; {{tabBar.borderStyle? (tabBar.position=='top'? 'border-bottom: solid 1px '+tabBar.borderStyle + ';' : 'border-top: solid 1rpx '+tabBar.borderStyle + ';') : ''}}">
    3.  
      <block wx:for="{{tabBar.list}}" wx:key="pagePath">
    4.  
      <navigator url="{{item.pagePath}}" open-type="switchTab" class="{{item.clas}}" style="{{item.active? 'color: '+(item.selectedColor? item.selectedColor : tabBar.selectedColor) : ''}}">
    5.  
      <image src="{{item.selectedIconPath}}" wx:if="{{item.active}}" class="tabbar-icon"></image>
    6.  
      <image src="{{item.iconPath}}" wx:if="{{!item.active}}" class="tabbar-icon"></image>
    7.  
      <text class='tabbar-text'>{{item.text}}</text>
    8.  
      </navigator>
    9.  
      </block>
    10.  
      </view>
    11.  
      </template>
    • 在app.js中添加如下两项
    
    		
    1.  
      // 自定义显示tabbar
    2.  
      onTabBar: function(key) {
    3.  
      var _curPageArr = getCurrentPages();
    4.  
      var _curPage = _curPageArr[_curPageArr.length - 1];
    5.  
      var _pagePath = _curPage.__route__;
    6.  
      if (_pagePath.indexOf('/') != 0) {
    7.  
      _pagePath = '/' + _pagePath;
    8.  
      }
    9.  
      var tabBar = this.tabBarData[key];
    10.  
      for (var i = 0; i < tabBar.list.length; i++) {
    11.  
      tabBar.list[i].active = false;
    12.  
      if (tabBar.list[i].pagePath == _pagePath) {
    13.  
      tabBar.list[i].active = true; // 根据页面地址设置当前页面状态
    14.  
      }
    15.  
      }
    16.  
      _curPage.setData({
    17.  
      tabBar: tabBar
    18.  
      });
    19.  
      },
    20.  
      tabBarData: {
    21.  
      userInfo: null,
    22.  
      pop: 2,
    23.  
      num: 0,
    24.  
      user: {
    25.  
      "color": "#dbdbdb",
    26.  
      "selectedColor": "#1296db",
    27.  
      "backgroundColor": "white",
    28.  
      "borderStyle": "white",
    29.  
      "position": "bottom",
    30.  
      "list": [
    31.  
      {
    32.  
      "pagePath": "/pages/message/message",
    33.  
      "text": "消息",
    34.  
      "iconPath": "/pages/images/icon/static/inform.png",
    35.  
      "selectedIconPath": "/pages/images/icon/active/inform.png",
    36.  
      "clas": "tabbar-item",
    37.  
      "active": true
    38.  
      },
    39.  
      {
    40.  
      "pagePath": "/pages/list/list",
    41.  
      "text": "列表",
    42.  
      "iconPath": "/pages/images/icon/static/list.png",
    43.  
      "selectedIconPath": "/pages/images/icon/active/list.png",
    44.  
      "clas": "tabbar-item",
    45.  
      "active": false
    46.  
      },
    47.  
      {
    48.  
      "pagePath": "/pages/user/user",
    49.  
      "text": "我的",
    50.  
      "iconPath": "/pages/images/icon/static/user.png",
    51.  
      "selectedIconPath": "/pages/images/icon/active/user.png",
    52.  
      "clas": "tabbar-item",
    53.  
      "active": false
    54.  
      }
    55.  
      ]
    56.  
      },
    57.  
      staff: {
    58.  
      "color": "#dbdbdb",
    59.  
      "selectedColor": "#1296db",
    60.  
      "backgroundColor": "white",
    61.  
      "borderStyle": "white",
    62.  
      "position": "bottom",
    63.  
      "list": [
    64.  
      {
    65.  
      "pagePath": "/pages/message/message",
    66.  
      "text": "消息",
    67.  
      "iconPath": "/pages/images/icon/static/inform.png",
    68.  
      "selectedIconPath": "/pages/images/icon/active/inform.png",
    69.  
      "clas": "tabbar-item",
    70.  
      "active": true
    71.  
      },
    72.  
      {
    73.  
      "pagePath": "/pages/user/user",
    74.  
      "text": "我的",
    75.  
      "iconPath": "/pages/images/icon/static/user.png",
    76.  
      "selectedIconPath": "/pages/images/icon/active/user.png",
    77.  
      "clas": "tabbar-item",
    78.  
      "active": false
    79.  
      }
    80.  
      ]
    81.  
      }
    82.  
      }
    • 在app.wxss中添加自定义TabBar的样式
    
    		
    1.  
      /* 自定义tabbar样式*/
    2.  
       
    3.  
      .tabbar {
    4.  
      display: flex;
    5.  
      flex-direction: row;
    6.  
      position: fixed;
    7.  
      width: 100%;
    8.  
      }
    9.  
       
    10.  
      .tabbar-item {
    11.  
      flex-grow: 1;
    12.  
      padding: 6rpx 0;
    13.  
      text-align: center;
    14.  
      }
    15.  
       
    16.  
      .tabbar-icon {
    17.  
      width: 46rpx;
    18.  
      height: 46rpx;
    19.  
      display: block;
    20.  
      margin: 0 auto;
    21.  
      }
    22.  
       
    23.  
      .tabbar-text {
    24.  
      font-size: 24rpx;
    25.  
      }

    至此基本的代码搬运工作已经基本完成,为了解决闪动问题,我并没有放弃使用微信自带的tabbar,所以在示例的app.json中需要配置所有的tabbar页面。

    
    		
    1.  
      "tabBar": {
    2.  
      "color": "#dbdbdb",
    3.  
      "selectedColor": "#1296db",
    4.  
      "backgroundColor": "white",
    5.  
      "borderStyle": "white",
    6.  
      "position": "bottom",
    7.  
      "list": [
    8.  
      {
    9.  
      "pagePath": "pages/message/message",
    10.  
      "text": "消息",
    11.  
      "iconPath": "/pages/images/icon/static/inform.png",
    12.  
      "selectedIconPath": "/pages/images/icon/active/inform.png"
    13.  
      },
    14.  
      {
    15.  
      "pagePath": "pages/list/list",
    16.  
      "text": "列表",
    17.  
      "iconPath": "/pages/images/icon/static/list.png",
    18.  
      "selectedIconPath": "/pages/images/icon/active/list.png"
    19.  
      },
    20.  
      {
    21.  
      "pagePath": "pages/user/user",
    22.  
      "text": "我的",
    23.  
      "iconPath": "/pages/images/icon/static/user.png",
    24.  
      "selectedIconPath": "/pages/images/icon/active/user.png"
    25.  
      }
    26.  
      ]
    27.  
      },
    • 在具有tabbar的页面中我们可以在页面的onShow事件中添加如下代码
    
    		
    1.  
      wx.hideTabBar({
    2.  
      success: function() {
    3.  
      app.onTabBar('user');
    4.  
      }
    5.  
      })

    注意:这里的传入的参数user往往由登录时的角色判断获取,本例如果改为staff则呈现不同效果,app由const app = getApp()获取

    • 在相应的wxml页面添加
    
    		
    1.  
      <import src="../template/tabbar.wxml"/>
    2.  
      ......页面内容
    3.  
      <template is="tabBar" data="{{tabBar:tabBar}}"></template>

    点击我们的自定义TabBar时,实际还是调用的微信自带的tabbar的跳转,只是将它自带的tabbar隐藏而已,闪动问题自然就没有了!