当前位置:   article > 正文

uniApp自定义tabBar底部导航栏+实现tabBar中间凸起自定义样式_uniapp自定义底部导航栏

uniapp自定义底部导航栏

一.配置信息

pages.js 添加 "custom": true 属性

二. 定义TabBar组件

  1. <template>
  2. <view class="tabbar-container">
  3. <view v-for="(item,index) in tabbarList" :class="[item.centerItem && 'center-item' ,'tabbar-item']"
  4. @click="changeItem(item)" :key="item.id">
  5. <view class="item-top">
  6. <image v-if="item.id!==2" :src="activeTab===item.path?item.selectIcon:item.icon"
  7. :class="[activeTab===item.path&&'active']"></image>
  8. <text v-else class="iconfont icon-add"></text>
  9. </view>
  10. <view :class="[activeTab===item.path ? 'item-active' : '','item-bottom']">
  11. <text class="text">{{item.text}}</text>
  12. </view>
  13. </view>
  14. <u-action-sheet class="action-sheet" round="20" :show="sheetShow" title="添加" @close="handleClose">
  15. <view class="gutter">
  16. </view>
  17. </u-action-sheet>
  18. </view>
  19. </template>
  20. <script>
  21. import {
  22. mapGetters
  23. } from "vuex"
  24. export default {
  25. data() {
  26. return {
  27. currentItem: 0,
  28. tabbarList: [{
  29. id: 0,
  30. path: "/pages/home/index",
  31. icon: "/static/img/home.png",
  32. selectIcon: "/static/img/home_fill.png",
  33. text: "首页",
  34. centerItem: false
  35. }, {
  36. id: 1,
  37. path: "/pages/monitor/index",
  38. icon: "/static/img/playon.png",
  39. selectIcon: "/static/img/playon_fill.png",
  40. text: "监控",
  41. centerItem: false
  42. }, {
  43. id: 2,
  44. icon: "/static/img/playon.png",
  45. selectIcon: "/static/img/playon_fill.png",
  46. text: "",
  47. centerItem: true
  48. }, {
  49. id: 3,
  50. path: "/pages/notice/index",
  51. icon: "/static/img/message.png",
  52. selectIcon: "/static/img/message_fill.png",
  53. text: "通知",
  54. centerItem: false
  55. }, {
  56. id: 4,
  57. path: "/pages/member/index",
  58. icon: "/static/img/mine.png",
  59. selectIcon: "/static/img/mine_fill.png",
  60. text: "我的",
  61. centerItem: false
  62. }],
  63. sheetShow: false,
  64. };
  65. },
  66. computed: mapGetters(['activeTab']),
  67. created() {
  68. let routes = getCurrentPages(); // 获取当前打开过的页面路由数组
  69. let curRoute = routes[routes.length - 1].route //获取当前页面路由
  70. this.$store.commit('setActiveTab', '/' + curRoute);
  71. },
  72. mounted() {
  73. uni.hideTabBar();
  74. },
  75. methods: {
  76. changeItem(item) {
  77. if (item?.path) {
  78. var pages = getCurrentPages();
  79. var page = pages[pages.length - 1].route;
  80. if (item.path == '/' + page) return
  81. this.$store.commit('setActiveTab', item.path);
  82. uni.switchTab({
  83. url: item.path,
  84. })
  85. } else {
  86. this.sheetShow = !this.sheetShow;
  87. }
  88. },
  89. //关闭快捷操作
  90. handleClose() {
  91. this.sheetShow = false;
  92. },
  93. },
  94. }
  95. </script>
  96. <style lang="scss" scoped>
  97. .tabbar-container {
  98. position: fixed;
  99. bottom: 0;
  100. left: 0;
  101. width: 100%;
  102. height: 100rpx;
  103. // box-shadow: 0px 3px 20px rgba(0, 0, 0, 0.16);
  104. border-top: 4rpx solid #e4e4e4;
  105. display: flex;
  106. align-items: center;
  107. color: #999999;
  108. z-index: 200;
  109. background-color: #fff;
  110. .tabbar-item {
  111. width: 20%;
  112. height: 100%;
  113. display: flex;
  114. flex-direction: column;
  115. justify-content: space-around;
  116. align-items: center;
  117. text-align: center;
  118. .item-top {
  119. width: 50rpx;
  120. height: 50rpx;
  121. >image {
  122. width: 100%;
  123. height: 100%;
  124. }
  125. .active {
  126. animation: mymove 1s 1;
  127. }
  128. @keyframes mymove {
  129. 0% {
  130. transform: scale(1);
  131. /*开始为原始大小*/
  132. }
  133. 10% {
  134. transform: scale(0.8);
  135. }
  136. 30% {
  137. transform: scale(1.1);
  138. /*放大1.1*/
  139. }
  140. 50% {
  141. transform: scale(0.9);
  142. /*放大1.1*/
  143. }
  144. 70% {
  145. transform: scale(1.05);
  146. }
  147. 90% {
  148. transform: scale(1);
  149. }
  150. }
  151. }
  152. .item-bottom {
  153. font-size: 26rpx;
  154. width: 100%;
  155. }
  156. }
  157. .center-item {
  158. display: block;
  159. position: relative;
  160. .item-top {
  161. flex-shrink: 0;
  162. width: 100rpx;
  163. height: 100rpx;
  164. position: absolute;
  165. top: -50rpx;
  166. left: calc(50% - 50rpx);
  167. border-radius: 50%;
  168. box-shadow: 0px 3px 20px rgba(0, 0, 0, 0.16);
  169. background-color: #ff6a00;
  170. .iconfont {
  171. position: absolute;
  172. left: 50%;
  173. top: 50%;
  174. transform: translate(-50%, -50%);
  175. font-size: 70rpx;
  176. font-weight: 500;
  177. color: #fffefd;
  178. }
  179. }
  180. .item-bottom {
  181. position: absolute;
  182. bottom: 5rpx;
  183. }
  184. }
  185. .item-active {
  186. color: #ff6a00;
  187. }
  188. .action-sheet {
  189. .gutter {
  190. display: flex;
  191. margin-top: 20rpx;
  192. margin-bottom: 40rpx;
  193. .item {
  194. flex: 1;
  195. &:last-child {
  196. margin-right: 0;
  197. }
  198. .iconfont {
  199. font-size: 56rpx;
  200. }
  201. .yellow {
  202. color: #F1C40F;
  203. }
  204. .purple {
  205. color: #9B59B6;
  206. }
  207. .turquoise {
  208. color: #1ABC9C;
  209. }
  210. .disabled {
  211. color: #ccc;
  212. }
  213. }
  214. }
  215. }
  216. }
  217. </style>

三. 把tabbar url地址存在vuex

  1. export default {
  2. state: {
  3. activeTab: "", // 底部tabbar url地址
  4. },
  5. mutations: {
  6. setActiveTab(state, url) {
  7. state.activeTab = url;
  8. },
  9. },
  10. getters: {
  11. activeTab: state => state.activeTab,
  12. }
  13. }

四. 在页面引入

  1. <template>
  2. <view>
  3. 我的
  4. <TabBar />
  5. </view>
  6. </template>
  7. <script>
  8. import TabBar from '@/components/Tabbar/Tabbar.vue'
  9. export default {
  10. components: {
  11. TabBar,
  12. },
  13. }
  14. </script>

效果图

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号