当前位置:   article > 正文

uniapp里面tabbar自定义的方法_uniapp 自定义tabbar

uniapp 自定义tabbar

第一种不封装自定义tabbar,需要的页面tabbar

需要的步骤

第一步,pages.json需要

  1. "tabBar": {
  2. "custom": true,//自定义
  3. "color": "#666666",
  4. "selectedColor": "#DF5458",
  5. "list": [{
  6. "pagePath": "pages/index1"
  7. },
  8. {
  9. "pagePath": "pages/index2"
  10. },
  11. {
  12. "pagePath": "pages/index3"
  13. },
  14. {
  15. "pagePath": "pages/index4"
  16. }
  17. ]
  18. },

第二步对应的页面

  1. <u-tabbar class="tabbar" :value="value1" @change="change1" :fixed="true" activeColor="red" inactiveColor="#000" :placeholder="false" :safeAreaInsetBottom="false">
  2. <u-tabbar-item text="首页">
  3. <image class="u-page__item__slot-icon" slot="active-icon"
  4. src="https://cdn.uviewui.com/uview/common/bell-selected.png" style="height:20px;width: 20px;"></image>
  5. <image class="u-page__item__slot-icon" slot="inactive-icon"
  6. src="https://cdn.uviewui.com/uview/common/bell.png" style="height:20px;width: 20px;"></image>
  7. </u-tabbar-item>
  8. <u-tabbar-item text="放映厅">
  9. <image class="u-page__item__slot-icon" slot="active-icon"
  10. src="https://cdn.uviewui.com/uview/common/bell-selected.png" style="height:20px;width: 20px;"></image>
  11. <image class="u-page__item__slot-icon" slot="inactive-icon"
  12. src="https://cdn.uviewui.com/uview/common/bell.png" style="height:20px;width: 20px;"></image>
  13. </u-tabbar-item>
  14. <u-tabbar-item text="直播">
  15. <image class="u-page__item__slot-icon" slot="active-icon"
  16. src="https://cdn.uviewui.com/uview/common/bell-selected.png" style="height:20px;width: 20px;"></image>
  17. <image class="u-page__item__slot-icon" slot="inactive-icon"
  18. src="https://cdn.uviewui.com/uview/common/bell.png" style="height:20px;width: 20px;"></image>
  19. </u-tabbar-item>
  20. <u-tabbar-item text="我的">
  21. <image class="u-page__item__slot-icon" slot="active-icon"
  22. src="https://cdn.uviewui.com/uview/common/bell-selected.png" style="height:20px;width: 20px;"></image>
  23. <image class="u-page__item__slot-icon" slot="inactive-icon"
  24. src="https://cdn.uviewui.com/uview/common/bell.png" style="height:20px;width: 20px;"></image>
  25. </u-tabbar-item>
  26. </u-tabbar>

第二种封装的tabbar

引入组件,注册组件,给组件传当前页面数

组件

  1. <template>
  2. <u-tabbar class="tabbar" :value="val" @change="change1" :fixed="true" :border='true' activeColor="red" inactiveColor="#000"
  3. :placeholder="false" :safeAreaInsetBottom="false">
  4. <u-tabbar-item text="首页">
  5. <image class="u-page__item__slot-icon" slot="active-icon"
  6. src="https://cdn.uviewui.com/uview/common/bell-selected.png" style="height:20px;width: 20px;"></image>
  7. <image class="u-page__item__slot-icon" slot="inactive-icon"
  8. src="https://cdn.uviewui.com/uview/common/bell.png" style="height:20px;width: 20px;"></image>
  9. </u-tabbar-item>
  10. <u-tabbar-item text="放映厅">
  11. <image class="u-page__item__slot-icon" slot="active-icon"
  12. src="https://cdn.uviewui.com/uview/common/bell-selected.png" style="height:20px;width: 20px;"></image>
  13. <image class="u-page__item__slot-icon" slot="inactive-icon"
  14. src="https://cdn.uviewui.com/uview/common/bell.png" style="height:20px;width: 20px;"></image>
  15. </u-tabbar-item>
  16. <u-tabbar-item text="直播">
  17. <image class="u-page__item__slot-icon" slot="active-icon"
  18. src="https://cdn.uviewui.com/uview/common/bell-selected.png" style="height:20px;width: 20px;"></image>
  19. <image class="u-page__item__slot-icon" slot="inactive-icon"
  20. src="https://cdn.uviewui.com/uview/common/bell.png" style="height:20px;width: 20px;"></image>
  21. </u-tabbar-item>
  22. <u-tabbar-item text="我的">
  23. <image class="u-page__item__slot-icon" slot="active-icon"
  24. src="https://cdn.uviewui.com/uview/common/bell-selected.png" style="height:20px;width: 20px;"></image>
  25. <image class="u-page__item__slot-icon" slot="inactive-icon"
  26. src="https://cdn.uviewui.com/uview/common/bell.png" style="height:20px;width: 20px;"></image>
  27. </u-tabbar-item>
  28. </u-tabbar>
  29. </template>
  30. <script>
  31. export default {
  32. props: {
  33. val: {
  34. type: Number,
  35. default: 0
  36. }
  37. },
  38. data() {
  39. return {
  40. list: [{
  41. path: "pages/index1"
  42. },
  43. {
  44. path: "pages/index2"
  45. },
  46. {
  47. path: "pages/index3"
  48. },
  49. {
  50. path: 'pages/index4'
  51. },
  52. ]
  53. };
  54. },
  55. methods: {
  56. change1(e) {
  57. uni.switchTab({
  58. url: '/' + this.list[e].path,
  59. })
  60. },
  61. }
  62. }
  63. </script>
  64. <style lang="scss">
  65. </style>

就这两种

  1. <template>
  2. <view>
  3. <view :style="'height: '+height+'px;'"></view>
  4. <u-tabbar v-model="val" @change="tabbarChange" :fixed="true" :placeholder="false" :border='false'
  5. activeColor="#d81e06" inactiveColor="#666666" :safeAreaInsetBottom="false">
  6. <view class="tab">
  7. <u-tabbar-item text="商城">
  8. <image class="icon" slot="inactive-icon" src="@/static/image/farem1.png"></image>
  9. <image class="icon" slot="active-icon" src="@/static/image/farem5.png"></image>
  10. </u-tabbar-item>
  11. <u-tabbar-item text="分类">
  12. <image class="icon" slot="inactive-icon" src="@/static/image/farem2.png"></image>
  13. <image class="icon" slot="active-icon" src="@/static/image/farem6.png"></image>
  14. </u-tabbar-item>
  15. <u-tabbar-item>
  16. <image slot="inactive-icon" src="@/static/image/farem9.png" style="width: 120rpx;height: 120rpx;margin-top: -116rpx;"></image>
  17. <image slot="active-icon" src="@/static/image/farem9.png" style="width: 120rpx;height: 120rpx;margin-top: -116rpx;"></image>
  18. </u-tabbar-item>
  19. <u-tabbar-item text="购物车">
  20. <image class="icon" slot="inactive-icon" src="@/static/image/farem3.png"></image>
  21. <image class="icon" slot="active-icon" src="@/static/image/farem8.png"></image>
  22. </u-tabbar-item>
  23. <u-tabbar-item text="我的">
  24. <image class="icon" slot="inactive-icon" src="@/static/image/farem4.png"></image>
  25. <image class="icon" slot="active-icon" src="@/static/image/farem7.png"></image>
  26. </u-tabbar-item>
  27. </view>
  28. </u-tabbar>
  29. </view>
  30. </template>
  31. <script>
  32. export default {
  33. props: {
  34. val: {
  35. type: Number,
  36. default: 0
  37. }
  38. },
  39. // options: {
  40. // styleIsolation: 'shared'
  41. // },
  42. created() {
  43. const info = uni.getSystemInfoSync()
  44. const scale = 750 / info.windowWidth;
  45. this.height = 156 / scale
  46. },
  47. data() {
  48. return {
  49. height: '',
  50. value4: 0,
  51. list: [{
  52. path: "pages/index1"
  53. },
  54. {
  55. path: "pages/classify/classify"
  56. },
  57. {
  58. path: "pages/shop/index"
  59. },
  60. {
  61. path: "pages/shopping/shopping"
  62. },
  63. {
  64. path: 'pages/index/index'
  65. },
  66. ]
  67. }
  68. },
  69. // props: {
  70. // current: Number,
  71. // },
  72. onLoad() {},
  73. onShow() {
  74. },
  75. methods: {
  76. tabbarChange(e) {
  77. if (e == 2) {
  78. uni.scanCode({
  79. scanType: ["qrCode"],
  80. success(e) {
  81. uni.reLaunch({
  82. url: "/packageA/pages/index?q=" + encodeURIComponent(e.result)
  83. })
  84. }
  85. })
  86. } else {
  87. uni.switchTab({
  88. url: '/' + this.list[e].path,
  89. })
  90. }
  91. }
  92. }
  93. }
  94. </script>
  95. <style lang="scss">
  96. .icon {
  97. width: 50rpx;
  98. height: 50rpx;
  99. }
  100. .icon2 {
  101. width: 156rpx;
  102. height: 156rpx;
  103. }
  104. .tab {
  105. height: 156rpx;
  106. background-image: url(@/static/image/farem55.png);
  107. background-repeat: no-repeat;
  108. background-size: 750rpx;
  109. background-position: top center;
  110. display: flex;
  111. width: 100%;
  112. }
  113. ::v-deep.u-tabbar-item__text {
  114. font-size: 10px !important;
  115. }
  116. </style>
  1. <template>
  2. <view>
  3. <view :style="'height: '+height+'px;'"></view>
  4. <u-tabbar v-model="val" @change="tabbarChange" :fixed="true" :placeholder="false" :border='false'
  5. activeColor="#d81e06" inactiveColor="#666666" :safeAreaInsetBottom="false">
  6. <view class="tab">
  7. <u-tabbar-item text="商城">
  8. <image class="icon" slot="inactive-icon" src="@/static/image/farem1.png"></image>
  9. <image class="icon" slot="active-icon" src="@/static/image/farem5.png"></image>
  10. </u-tabbar-item>
  11. <u-tabbar-item text="分类">
  12. <image class="icon" slot="inactive-icon" src="@/static/image/farem2.png"></image>
  13. <image class="icon" slot="active-icon" src="@/static/image/farem6.png"></image>
  14. </u-tabbar-item>
  15. <u-tabbar-item>
  16. <image slot="inactive-icon" src="@/static/image/farem9.png" style="width: 120rpx;height: 120rpx;margin-top: -116rpx;"></image>
  17. <image slot="active-icon" src="@/static/image/farem9.png" style="width: 120rpx;height: 120rpx;margin-top: -116rpx;"></image>
  18. </u-tabbar-item>
  19. <u-tabbar-item text="购物车">
  20. <image class="icon" slot="inactive-icon" src="@/static/image/farem3.png"></image>
  21. <image class="icon" slot="active-icon" src="@/static/image/farem8.png"></image>
  22. </u-tabbar-item>
  23. <u-tabbar-item text="我的">
  24. <image class="icon" slot="inactive-icon" src="@/static/image/farem4.png"></image>
  25. <image class="icon" slot="active-icon" src="@/static/image/farem7.png"></image>
  26. </u-tabbar-item>
  27. </view>
  28. </u-tabbar>
  29. </view>
  30. </template>
  31. <script>
  32. export default {
  33. props: {
  34. val: {
  35. type: Number,
  36. default: 0
  37. }
  38. },
  39. created() {
  40. const info = uni.getSystemInfoSync()
  41. const scale = 750 / info.windowWidth;
  42. this.height = 156 / scale
  43. },
  44. data() {
  45. return {
  46. height: '',
  47. value4: 0,
  48. list: [{
  49. path: "pages/index1"
  50. },
  51. {
  52. path: "pages/classify/classify"
  53. },
  54. {
  55. path: "pages/shop/index"
  56. },
  57. {
  58. path: "pages/shopping/shopping"
  59. },
  60. {
  61. path: 'pages/index/index'
  62. },
  63. ]
  64. }
  65. },
  66. onLoad() {},
  67. onShow() {
  68. },
  69. methods: {
  70. tabbarChange(e) {
  71. if (e == 2) {
  72. uni.scanCode({
  73. scanType: ["qrCode"],
  74. success(e) {
  75. uni.reLaunch({
  76. url: "/packageA/pages/index?q=" + encodeURIComponent(e.result)
  77. })
  78. }
  79. })
  80. } else {
  81. uni.switchTab({
  82. url: '/' + this.list[e].path,
  83. })
  84. }
  85. }
  86. }
  87. }
  88. </script>
  89. <style lang="scss">
  90. .icon {
  91. width: 50rpx;
  92. height: 50rpx;
  93. }
  94. .icon2 {
  95. width: 156rpx;
  96. height: 156rpx;
  97. }
  98. .tab {
  99. height: 156rpx;
  100. background-image: url(@/static/image/farem55.png);
  101. background-repeat: no-repeat;
  102. background-size: 750rpx;
  103. background-position: top center;
  104. display: flex;
  105. width: 100%;
  106. }
  107. ::v-deep.u-tabbar-item__text {
  108. font-size: 10px !important;
  109. }
  110. </style>

引入组件的页面样式加入

要不然不透

还有就是tabbar不定高度,就按照自己高度定

如果定了高度就按照

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

闽ICP备14008679号