当前位置:   article > 正文

uniapp实现导航栏中间图标凸起_uniapp底部导航栏中间凸起

uniapp底部导航栏中间凸起

 1、使用uniapp官方提供的属性midButton

使用时,list数组须为偶数

pages.json

  1. "tabBar": {
  2. "color": "#000",
  3. "selectedColor": "red",
  4. "list": [{
  5. "text": "首页",
  6. "pagePath": "pages/index/index",
  7. "iconPath": "static/logo.png",
  8. "selectedIconPath": "static/logo.png"
  9. },
  10. {
  11. "text": "商城",
  12. "pagePath": "pages/a/a",
  13. "iconPath": "static/logo.png",
  14. "selectedIconPath": "static/logo.png"
  15. },
  16. // {
  17. // "text": "会员",
  18. // "pagePath": "pages/b/b",
  19. // "iconPath": "static/logo.png",
  20. // "selectedIconPath": "static/logo.png"
  21. // },
  22. {
  23. "text": "任务",
  24. "pagePath": "pages/c/c",
  25. "iconPath": "static/logo.png",
  26. "selectedIconPath": "static/logo.png"
  27. },
  28. {
  29. "text": "我的",
  30. "pagePath": "pages/d/d",
  31. "iconPath": "static/logo.png",
  32. "selectedIconPath": "static/logo.png"
  33. }
  34. ],
  35. "midButton": {
  36. "pagePath": "pages/b/b",
  37. "iconPath": "static/logo.png",
  38. "width": "60px",
  39. "height": "60px",
  40. "iconWidth": "40px",
  41. "iconheight": "40px",
  42. "text": "会员"
  43. }
  44. },

App.vue

  1. <script>
  2. export default {
  3. onLaunch: function() {
  4. console.log('App Launch')
  5. //监听凸起页面
  6. uni.onTabBarMidButtonTap(()=>{
  7. uni.navigateTo({
  8. url:'/pages/b/b'
  9. })
  10. })
  11. },
  12. onShow: function() {
  13. console.log('App Show')
  14. },
  15. onHide: function() {
  16. console.log('App Hide')
  17. }
  18. }
  19. </script>
  20. <style>
  21. /*每个页面公共css */
  22. </style>

2、自定义tabBar组件

把pages.json里的tabbar都删掉

新建组件tabBar.vue

tabBar.vue

  1. <template>
  2. <view class="tab-block">
  3. <ul
  4. class='tab-list flex flex-center'
  5. :class="showMiddleButton === true?'tab-list-middle':'tab-list-default'"
  6. >
  7. <li
  8. v-for="(item, index) in tabList"
  9. :class="'list-item flex flex-column flex-middle ' + item.middleClass"
  10. @click="handlePush(item, index)"
  11. :key="index"
  12. >
  13. <view class="item-img-box">
  14. <image
  15. class="item-img"
  16. :src="tabIndex == index ? item.selectedIconPath : item.iconPath"
  17. />
  18. </view>
  19. <view class="item-text font-20 color-black">
  20. {{item.text}}
  21. </view>
  22. </li>
  23. </ul>
  24. <view class="tab-bar" v-show="showTabBar === true"></view>
  25. </view>
  26. </template>
  27. <script>
  28. export default {
  29. props: {
  30. tabIndex: { //当前选中的tab项
  31. type: Number,
  32. default: 0
  33. }
  34. },
  35. data() {
  36. return {
  37. /*
  38. * iconPath: 默认icon图片路径
  39. * selectedIconPath: 选中icon图片路径
  40. * text: tab按钮文字
  41. * pagePath:页面路径
  42. * middleClass:中间按钮样式类名
  43. * tabList最小两项,最多五项
  44. * tabList长度为奇数时,中间按钮才会突出显示
  45. *
  46. */
  47. tabList: [{
  48. iconPath: '../../static/logo.png',
  49. selectedIconPath: '../../static/logo.png',
  50. text: '首页',
  51. pagePath: '/pages/index/index',
  52. middleClass: ''
  53. },
  54. {
  55. iconPath: '../../static/logo.png',
  56. selectedIconPath: '../../static/logo.png',
  57. text: '商城',
  58. pagePath: '/pages/a/a',
  59. middleClass: ''
  60. },
  61. {
  62. iconPath: '../../static/logo.png',
  63. selectedIconPath: '../../static/logo.png',
  64. text: '会员',
  65. pagePath: '/pages/b/b',
  66. middleClass: ''
  67. },
  68. {
  69. iconPath: '../../static/logo.png',
  70. selectedIconPath:'../../static/logo.png',
  71. text: '任务',
  72. pagePath: '/pages/c/c'
  73. },
  74. {
  75. iconPath: '../../static/logo.png',
  76. selectedIconPath: '../../static/logo.png',
  77. text: '我的',
  78. pagePath: '/pages/d/d',
  79. middleClass: ''
  80. }
  81. ],
  82. showTabBar: false,
  83. showMiddleButton: false,
  84. };
  85. },
  86. computed: {
  87. getHeight() {
  88. return Number(this.height);
  89. },
  90. },
  91. mounted() {
  92. this.getSystemInfo()
  93. this.setTabBar()
  94. },
  95. methods: {
  96. //判断中间按钮是否突出显示
  97. setTabBar(){
  98. let tabLength = this.tabList.length
  99. if (tabLength % 2) {
  100. this.showMiddleButton = true
  101. let middleIndex = Math.floor(tabLength / 2)
  102. this.tabList[middleIndex].middleClass = 'mid-button'
  103. }
  104. },
  105. //点击按钮
  106. handlePush(item, index) {
  107. if (this.tabIndex !== index) {
  108. uni.reLaunch({
  109. url: `${item.pagePath}?tabIndex=${index}`,
  110. })
  111. }
  112. },
  113. //兼容iPhoneX以上底部黑线条的显示
  114. getSystemInfo() {
  115. uni.getSystemInfo({
  116. success: (res) => {
  117. // X及以上的异形屏top为44,非异形屏为20
  118. if (res.safeArea.top > 20) {
  119. this.showTabBar = true
  120. }
  121. }
  122. });
  123. },
  124. }
  125. }
  126. </script>
  127. <style lang="scss">
  128. .flex {
  129. display: flex;
  130. flex-flow: row wrap;
  131. }
  132. .flex-center {
  133. align-items: center;
  134. justify-content: center;
  135. }
  136. .flex-column {
  137. flex-direction: column;
  138. }
  139. .flex-middle {
  140. align-items: center;
  141. }
  142. .font-20 {
  143. font-size: 20rpx;
  144. }
  145. .tab-block {
  146. position: fixed;
  147. bottom: 0;
  148. left: 0;
  149. z-index: 999;
  150. background-size: contain;
  151. width: 100vw;
  152. .tab-list{
  153. height:100rpx;
  154. padding: 0;//解决偏移
  155. }
  156. .tab-list-default{
  157. background-color: #FFFFFF;
  158. border-top: 1px #dddddd solid;
  159. }
  160. .tab-list-middle {
  161. position: relative;
  162. // background: url('https://res.paquapp.com/popmartvip/home/nav_bar_bg_2x.png') no-repeat center center;
  163. background-size: cover;
  164. }
  165. .list-item {
  166. flex: 1;
  167. .item-img-box {
  168. width: 44rpx;
  169. height: 42rpx;
  170. margin-bottom: 9rpx;
  171. position: relative;
  172. }
  173. .item-img {
  174. width: 44rpx;
  175. height: 42rpx;
  176. }
  177. }
  178. .mid-button {
  179. position: relative;
  180. .item-img-box {
  181. width: 106rpx;
  182. height: 106rpx;
  183. margin-bottom: 9rpx;
  184. position: absolute;
  185. z-index: 1002;
  186. top: -104rpx;
  187. }
  188. .item-img {
  189. width: 106rpx;
  190. height: 106rpx;
  191. }
  192. .item-text {
  193. font-size: 20rpx;
  194. position: absolute;
  195. z-index: 1002;
  196. bottom: -40rpx;
  197. }
  198. }
  199. .tab-bar {
  200. height: 30rpx;
  201. background-color: #FFFFFF;
  202. }
  203. }
  204. </style>

index.vue

  1. <template>
  2. <view class="content">
  3. <view class="text-area">
  4. <text class="title">{{title}}</text>
  5. </view>
  6. <TabBar tabIndex=0></TabBar>
  7. </view>
  8. </template>
  9. <script>
  10. import TabBar from '../../components/tabBar/tabBar.vue'
  11. export default {
  12. components: {
  13. TabBar
  14. },
  15. data() {
  16. return {
  17. title: '我是首页'
  18. }
  19. },
  20. onLoad() {
  21. },
  22. methods: {
  23. }
  24. }
  25. </script>
  26. <style>
  27. </style>

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

闽ICP备14008679号