当前位置:   article > 正文

微信小程序中动态设置TabBar并解决闪动问题_微信小程序开发自定义底部导航栏跳转时闪一下

微信小程序开发自定义底部导航栏跳转时闪一下

最近接到一个需求用户在登录微信小程序时需要根据角色显示不同的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. .tabbar {
  3. display: flex;
  4. flex-direction: row;
  5. position: fixed;
  6. width: 100%;
  7. }
  8. .tabbar-item {
  9. flex-grow: 1;
  10. padding: 6rpx 0;
  11. text-align: center;
  12. }
  13. .tabbar-icon {
  14. width: 46rpx;
  15. height: 46rpx;
  16. display: block;
  17. margin: 0 auto;
  18. }
  19. .tabbar-text {
  20. font-size: 24rpx;
  21. }

至此基本的代码搬运工作已经基本完成,为了解决闪动问题,我并没有放弃使用微信自带的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隐藏而已,闪动问题自然就没有了!

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/726975
推荐阅读
相关标签