• uni-app 实现凸起的 tabbar 底部导航栏


    效果图

    在 pages.json 中设置隐藏自带的 tabbar 导航栏

    "custom": true, // 开启自定义tabBar(不填每次原来的tabbar在重新加载时都回闪现)

    新建一个 custom-tabbar.vue 自定义组件页面

    custom-tabbar.vue
    1. <template>
    2. <view class="container">
    3. <view
    4. class="tabbar-item"
    5. :class="[item.centerItem ? ' center-item' : '']"
    6. :style="'width: calc(100% /' + tabbarList.length + ')'"
    7. @click="changeItem(item)"
    8. v-for="(item, i) in tabbarList"
    9. :key="i"
    10. >
    11. <view class="item-top"><image :src="curItem === item.id ? item.selectedIconPath : item.iconPath" />view>
    12. <view class="item-bottom" :class="[curItem === item.id ? 'item-active' : '']">{{ item.text }}view>
    13. view>
    14. view>
    15. template>
    16. <script>
    17. export default {
    18. props: {
    19. /* 当前导航栏 */
    20. currPage: {
    21. type: Number,
    22. default: 0
    23. }
    24. },
    25. data() {
    26. return {
    27. curItem: 0, // 当前所选导航栏
    28. tabbarList: [
    29. {
    30. id: 0,
    31. pagePath: "/pages/public/index",
    32. iconPath: "/static/tab-bar/home.png",
    33. selectedIconPath: "/static/tab-bar/home-active.png",
    34. text: "首页",
    35. centerItem: false
    36. },
    37. {
    38. id: 1,
    39. pagePath: "",
    40. iconPath: "/static/tab-bar/bulge-active.png",
    41. selectedIconPath: "/static/tab-bar/bulge-active.png",
    42. text: "称重",
    43. centerItem: true
    44. },
    45. {
    46. id: 2,
    47. pagePath: "/pages/weight/my",
    48. iconPath: "/static/tab-bar/my.png",
    49. selectedIconPath: "/static/tab-bar/my-active.png",
    50. text: "我的",
    51. centerItem: false
    52. }
    53. ] // 导航栏列表
    54. };
    55. },
    56. mounted() {
    57. this.curItem = this.currPage; // 当前所选导航栏
    58. // #ifdef H5
    59. uni.hideTabBar(); // 隐藏 tabBar 导航栏
    60. // #endif
    61. },
    62. methods: {
    63. /* 导航栏切换 */
    64. changeItem(e) {
    65. // 中间凸起按钮
    66. if (e.id === 1) {
    67. // todo
    68. return;
    69. }
    70. uni.switchTab({ url: e.pagePath }); // 跳转到其他 tab 页面
    71. }
    72. }
    73. };
    74. script>
    75. <style lang="scss" scoped>
    76. $textDefaultColor: #999; // 文字默认颜色
    77. $bottomBg: #fff; // 底部背景
    78. $textSelectedColor: #67c23a; // 文字选中颜色
    79. $centerItemBg: #fff; // 中间凸起按钮背景
    80. .container {
    81. position: fixed;
    82. bottom: 0;
    83. left: 0;
    84. display: flex;
    85. align-items: center;
    86. width: 100%;
    87. height: 110rpx;
    88. color: $textDefaultColor;
    89. padding: 5rpx 0;
    90. background-color: $bottomBg;
    91. box-shadow: 0 0 10rpx #999;
    92. }
    93. .tabbar-item {
    94. display: flex;
    95. flex-direction: column;
    96. justify-content: center;
    97. align-items: center;
    98. text-align: center;
    99. height: 100rpx;
    100. .item-top {
    101. flex-shrink: 0;
    102. width: 65rpx;
    103. height: 65rpx;
    104. padding: 4rpx;
    105. image {
    106. width: 100%;
    107. height: 100%;
    108. }
    109. }
    110. .item-bottom {
    111. width: 100%;
    112. font-size: 28rpx;
    113. }
    114. .item-active {
    115. color: $textSelectedColor;
    116. }
    117. }
    118. .center-item {
    119. position: relative;
    120. .item-top {
    121. position: absolute;
    122. top: -55rpx;
    123. left: 50%;
    124. transform: translateX(-50%);
    125. width: 105rpx;
    126. height: 105rpx;
    127. background-color: $centerItemBg;
    128. border-radius: 50%;
    129. }
    130. .item-bottom {
    131. position: absolute;
    132. bottom: 5rpx;
    133. }
    134. }
    135. style>

    底部安全区域的适配问题可查看:uni-app 苹果手机底部安全区域的适配问题

    在 main.js 中引用组件

    1. // 注册全局组件
    2. import customTabbar from "components/custom-tabbar.vue"
    3. Vue.component('custom-tabbar', customTabbar)

    在要用到的页面中直接调用

    1. <custom-tabbar :curr-page="0" />
  • 相关阅读:
    Java初始化大量数据到Neo4j中(一)
    如何实现MQTT的Java代码
    Centos7安装nginx及网页如何放入nginx
    sql-labs(11-20)
    Android移动应用开发之登录用户密码记住及创建数据库存储查询用户名密码
    8月24笔记
    0825学习笔记(文件)
    部署LVS—NAT模式集群
    elasticsearch启动问题
    【C++】基础知识点回顾 上:命名空间与输入输出
  • 原文地址:https://blog.csdn.net/AdminGuan/article/details/133377293