tabBar.vue 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <template>
  2. <view class="uni-tabbar">
  3. <view v-for="(item, index) in list" :key="index" @click="onTabBarClick(item.current)"
  4. :class="['uni-tabbar-item', { 'uni-tabbar-item-active': index === activeIndex }]">
  5. <view class="uni-tabbar-item-icon">
  6. <img :src="index === activeIndex ? item.selectedIconPath : item.iconPath" />
  7. </view>
  8. <view class="uni-tabbar-item-text">{{ item.text }}</view>
  9. </view>
  10. </view>
  11. </template>
  12. <script>
  13. export default {
  14. name: "tabBar",
  15. props:{
  16. activeIndex: Number, // 下标
  17. },
  18. data() {
  19. return {
  20. list: [{
  21. current: 0,
  22. "pagePath": "/pages/index/index",
  23. "text": "首页",
  24. "iconPath": "/static/dibu/boa1.png",
  25. "selectedIconPath": "/static/dibu/bob1.png"
  26. },
  27. {
  28. current: 1,
  29. "pagePath": "/pages/lecturer/wdzx",
  30. "text": "咨询",
  31. "iconPath": "/static/dibu/boa2.png",
  32. "selectedIconPath": "/static/dibu/bob2.png"
  33. },
  34. {
  35. current: 2,
  36. "pagePath": "/pages/index/jiehuo",
  37. "text": "解惑",
  38. "iconPath": "/static/dibu/boa3.png",
  39. "selectedIconPath": "/static/dibu/bob3.png"
  40. },
  41. {
  42. current: 3,
  43. "pagePath": "/pages/lecturer/my",
  44. "text": "我的",
  45. "iconPath": "/static/dibu/boa4.png",
  46. "selectedIconPath": "/static/dibu/bob4.png"
  47. }]
  48. };
  49. },
  50. methods:{
  51. onTabBarClick(i){
  52. this.$emit("change", i);
  53. if(this.activeIndex != i){
  54. uni.switchTab({
  55. url: this.list[i].pagePath
  56. })
  57. }
  58. }
  59. }
  60. }
  61. </script>
  62. <style lang="less">
  63. /* 修改图标的大小和位置 */
  64. .uni-tabbar-item-icon {
  65. position: relative;
  66. img {
  67. width: 48rpx; //图标宽度
  68. height: 48rpx; //图标高度
  69. }
  70. }
  71. /* 修改文字大小和颜色 */
  72. .uni-tabbar-item-text {
  73. font-size: 12px; //文字大小
  74. color: #999; //文字颜色
  75. }
  76. .uni-tabbar-item-active .uni-tabbar-item-text {
  77. color: #374B6E; //选中状态下文字颜色
  78. }
  79. // 添加背景色和阴影
  80. .uni-tabbar {
  81. position: fixed;
  82. left: 0;
  83. bottom: 0;
  84. display: flex;
  85. width: 100%;
  86. height: 100rpx; //tabbar高度
  87. background-color: #fff; //背景色
  88. box-shadow: 0 -1px 3px rgba(0, 0, 0, 0.1); //阴影
  89. z-index: 100;
  90. }
  91. .uni-tabbar-item {
  92. flex: 1;
  93. display: flex;
  94. flex-direction: column;
  95. align-items: center;
  96. justify-content: center;
  97. }
  98. </style>