当前位置:   article > 正文

自定义tabBar

自定义tabbar

第一步:在page.json修改tabbar的配置

只保留页面路径,

Markup

  1. "tabBar": {
  2. "color": "#7A7E83",
  3. "selectedColor": "#0A50DF",
  4. "borderStyle": "white",
  5. "backgroundColor": "red",
  6. "height":"0px",
  7. "custom": true,
  8. "list": [{
  9. "pagePath": "pages/home/home"
  10. // "iconPath": "static/icon/tab_home_default.png",
  11. // "selectedIconPath": "static/icon/tab_home_active.png",
  12. // "text": "首页"
  13. },
  14. {
  15. "pagePath": "pages/check/check"
  16. // "iconPath": "static/icon/tab_check_default.png",
  17. // "selectedIconPath": "static/icon/tab_check_active.png",
  18. // "text": "出车前检测"
  19. },
  20. {
  21. "pagePath": "pages/EmergRescue/EmergRescue"
  22. // "iconPath": "static/icon/tab_transport_default.png",
  23. // "selectedIconPath": "static/icon/tab__transport_active.png",
  24. // "text": "应急救援"
  25. },
  26. {
  27. "pagePath": "pages/drill/drill"
  28. // "iconPath": "static/icon/tab_service_default.png",
  29. // "selectedIconPath": "static/icon/tab_service_active.png",
  30. // "text": "救援演练"
  31. },{
  32. "pagePath": "pages/my/my"
  33. // "iconPath": "static/icon/tab_my_active.png",
  34. // "selectedIconPath": "static/icon/tab_my_default.png",
  35. // "text": "我的"
  36. }]

第二部:自定义的tabbar组件全部代码

Markup

  1. <template>
  2. <cover-view>
  3. <cover-image src="https://tyb-whp.oss-cn-hangzhou.aliyuncs.com/applet/bcimg.png"></cover-image>
  4. <cover-view :style="{'padding-bottom': paddingBottomHeight + 'rpx'}">
  5. <cover-view :class="[ index == 2 ? 'tabbar-item2' : `tabbar-item`]"
  6. v-for="(item, index) in list"
  7. :key="index"
  8. @click="tabbarChange(item.path)"
  9. >
  10. <cover-image :src="item.icon_a" v-if="current == index"></cover-image>
  11. <cover-image :src="item.icon" v-else></cover-image>
  12. <cover-view :class="{'tabbarActive': current == index}" v-if="item.text">{{item.text}}</cover-view>
  13. </cover-view>
  14. </cover-view>
  15. </cover-view>
  16. </template>
  17. <script>
  18. export default {
  19. props: {
  20. current: String
  21. },
  22. data() {
  23. return {
  24. paddingBottomHeight: 0, //苹果X以上手机底部适配高度
  25. list: [{
  26. text: '首页',
  27. icon: '../../static/icon/tab_home_default.png', //未选中图标
  28. icon_a: '../../static/icon/tab_home_active.png', //选中图片
  29. path: "/pages/home/home", //页面路径
  30. },{
  31. text: '行业服务',
  32. icon: '../../static/icon/tab_service_default.png',
  33. icon_a: '../../static/icon/tab_service_active.png',
  34. path: '/pages/check/check',
  35. },
  36. {
  37. text: '应急救援',
  38. icon: '../../static/icon/check/red-center0.png',
  39. icon_a: '../../static/icon/check/red-center0.png',
  40. path: "/pages/EmergRescue/EmergRescue",
  41. },
  42. {
  43. text: '运输管理',
  44. icon: '../../static/icon/tab_transport_default.png',
  45. icon_a: '../../static/icon/tab__transport_active.png',
  46. path: "/pages/drill/drill",
  47. },{
  48. text: '我的',
  49. icon: '../../static/icon/tab_my_active.png',
  50. icon_a: '../../static/icon/tab_my_default.png',
  51. path: "/pages/my/my",
  52. },
  53. ]
  54. };
  55. },
  56. created() {
  57. let that = this;
  58. uni.getSystemInfo({
  59. success: function (res) {
  60. let model = ['X', 'XR', 'XS', '11', '12', '13', '14', '15'];
  61. model.forEach(item => {
  62. //适配iphoneX以上的底部,给tabbar一定高度的padding-bottom
  63. if(res.model.indexOf(item) != -1 && res.model.indexOf('iPhone') != -1) {
  64. that.paddingBottomHeight = 40;
  65. }
  66. })
  67. }
  68. });
  69. },
  70. watch: {
  71. },
  72. methods: {
  73. tabbarChange(path) {
  74. console.log('222');
  75. uni.switchTab({
  76. url: path
  77. })
  78. }
  79. }
  80. };
  81. </script>
  82. <style scoped>
  83. .extend{
  84. position: fixed;
  85. bottom: 0;
  86. left: 0;
  87. right: 0;
  88. width: 750rpx;
  89. height: 200rpx;
  90. }
  91. .tabbarActive{
  92. color: #0A50DF !important;
  93. }
  94. .tabbar{
  95. display: flex;
  96. justify-content: space-around;
  97. align-items: center;
  98. width: 750rpx;
  99. height: 180rpx;
  100. .tabbar-item{
  101. display: flex;
  102. flex-direction: column;
  103. align-items: center;
  104. justify-content: center;
  105. height: 100rpx;
  106. margin-top: 90rpx;
  107. .item-img{
  108. margin-bottom: 20rpx;
  109. width: 46rpx;
  110. height: 46rpx;
  111. }
  112. .item-name{
  113. font-size: 26rpx;
  114. color: #A3A3A3;
  115. }
  116. }
  117. }
  118. .tabbar-item2{
  119. display: flex;
  120. flex-direction: column;
  121. align-items: center;
  122. justify-content: center;
  123. height: auto;
  124. margin-top: 40rpx;
  125. margin-left: -10rpx;
  126. .item-img{
  127. // margin-bottom: 10rpx;
  128. width: 115rpx;
  129. height: 115rpx;
  130. }
  131. .item-name{
  132. font-size: 26rpx;
  133. color: #A3A3A3;
  134. }
  135. }
  136. .background{
  137. position: absolute;
  138. top: 0;
  139. left: 0;
  140. bottom: 0;
  141. right: 0;
  142. z-index: -999;
  143. width: 750rpx;
  144. height: 150rpx;
  145. }
  146. </style>

第三步:引入

 

注意:

在引入的页面:

加入这个方法

Markup

  1. onShow() {
  2. uni.hideTabBar({
  3. animation: false
  4. })
  5. },

 

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

闽ICP备14008679号