当前位置:   article > 正文

微信小程序自定义tabbar导航栏,中间凸出样式_微信小程序底部tab栏特殊样式

微信小程序底部tab栏特殊样式

这种样式的底部导航栏

使用微信小程序的自定义tabBar:微信小程序官方说明

uni.app=>在 page.json 中的 tabBar 项指定 custom 字段为true:

  1. "tabBar": {
  2. "custom": true,
  3. "color": "rgb(51, 51, 51)",
  4. "selectedColor": "rgb(249, 48, 43)",
  5. "backgroundColor": "rgb(196, 196, 196)",
  6. "list": [{
  7. "pagePath": "pages/index/index",
  8. "text": "首页",
  9. "iconPath": "/static/tabBar/taber_pictrue/home.png",
  10. "selectedIconPath": "/static/tabBar/taber_pictrue_selected/home.png"
  11. }, {
  12. "pagePath": "pages/course/index",
  13. "text": "课程",
  14. "iconPath": "/static/tabBar/taber_pictrue/course.png",
  15. "selectedIconPath": "/static/tabBar/taber_pictrue_selected/course.png"
  16. }, {
  17. "pagePath": "pages/bm/index",
  18. "text": "报名",
  19. "iconPath": "/static/tabBar/taber_pictrue/bm.png",
  20. "selectedIconPath": "/static/tabBar/taber_pictrue_selected/bm.png"
  21. }, {
  22. "pagePath": "pages/data/index",
  23. "text": "资料",
  24. "iconPath": "/static/tabBar/taber_pictrue/data.png",
  25. "selectedIconPath": "/static/tabBar/taber_pictrue_selected/data.png"
  26. }, {
  27. "pagePath": "pages/myCenter/index",
  28. "text": "我的",
  29. "iconPath": "/static/tabBar/taber_pictrue/mycenter.png",
  30. "selectedIconPath": "/static/tabBar/taber_pictrue_selected/mycenter.png"
  31. }]
  32. },

在根目录创建custom-tab-bar目录,注意一定要完全匹配,不要输错

  1. custom-tab-bar/index.js
  2. custom-tab-bar/index.json
  3. custom-tab-bar/index.wxml
  4. custom-tab-bar/index.wxss

 index.js代码:注意这里的中间需要凸出项设置一个class

  1. Component({
  2. data: {
  3. selected: 0, //当前选中的tab下标
  4. color: "#1E1E1E",
  5. selectedColor: "#646464", //tabbar选中字体颜色
  6. "list": [{
  7. "pagePath": "pages/index/index",
  8. "text": "首页",
  9. "iconPath": "/static/tabBar/taber_pictrue/home.png",
  10. "selectedIconPath": "/static/tabBar/taber_pictrue_selected/home.png"
  11. }, {
  12. "pagePath": "pages/course/index",
  13. "text": "课程",
  14. "iconPath": "/static/tabBar/taber_pictrue/course.png",
  15. "selectedIconPath": "/static/tabBar/taber_pictrue_selected/course.png"
  16. }, {
  17. "pagePath": "pages/bm/index",
  18. "text": "报名",
  19. "iconPath": "/static/tabBar/taber_pictrue/bm-2.png",
  20. "selectedIconPath": "/static/tabBar/taber_pictrue_selected/bm.png",
  21. bmClass: "bm"
  22. }, {
  23. "pagePath": "pages/data/index",
  24. "text": "资料",
  25. "iconPath": "/static/tabBar/taber_pictrue/data.png",
  26. "selectedIconPath": "/static/tabBar/taber_pictrue_selected/data.png"
  27. }, {
  28. "pagePath": "pages/myCenter/index",
  29. "text": "我的",
  30. "iconPath": "/static/tabBar/taber_pictrue/mycenter.png",
  31. "selectedIconPath": "/static/tabBar/taber_pictrue_selected/mycenter.png"
  32. }]
  33. },
  34. attached() {},
  35. methods: {
  36. toggleTabbar(e) {
  37. const data = e.currentTarget.dataset;
  38. const url ='/' + data.path
  39. // this.setData({
  40. // selected: data.index
  41. // })
  42. wx.switchTab({url})
  43. }
  44. },
  45. })

index.json代码:

  1. {
  2. "component": true,
  3. "usingComponents": {}
  4. }

index.wxml代码:动态设置中间凸出项的class,css样式可根据自己项目要求进行更改:

  1. <view>
  2. <view class="tab-bar" >
  3. <image class="tab-bar-bg" src="../static/tabBar/taber_pictrue/tabbar.png"></image>
  4. <view class="tab-bar-icon tab-bar">
  5. <view wx:for="{{list}}" wx:key="index" class="tab-bar-item {{item.diyClass}}" data-path="{{item.pagePath}}" data-index="{{index}}" bindtap="toggleTabbar">
  6. <image src="{{selected === index ? item.selectedIconPath : item.iconPath}}" class="{{item.bmClass}}" mode="aspectFit"/>
  7. <view style="color: {{selected === index ? selectedColor : color}}" class="{{item.bmClass}}">{{item.text}}</view>
  8. </view>
  9. </view>
  10. </view>
  11. </view>

index.wxss代码:

  1. /*重新样式*/
  2. .tab-bar {
  3. position: fixed;
  4. bottom: 0;
  5. left: 0;
  6. right: 0;
  7. display: flex;
  8. box-sizing: content-box;
  9. background-color: transparent;
  10. }
  11. .tab-bar-bg {
  12. width: 100%;
  13. height: 140rpx;
  14. }
  15. .tab-bar-icon {
  16. display: flex;
  17. position: absolute;
  18. justify-content: space-between;
  19. width: 100%;
  20. }
  21. .tab-bar-border {
  22. background-color: rgba(0, 0, 0, 0.33);
  23. position: absolute;
  24. left: 0;
  25. top: 0;
  26. width: 100%;
  27. height: 1px;
  28. transform: scaleY(0.5);
  29. }
  30. .tab-bar-item {
  31. flex: auto;
  32. text-align: center;
  33. display: flex;
  34. justify-content: center;
  35. align-items: center;
  36. flex-direction: column;
  37. background-color: transparent;
  38. height: 120rpx;
  39. }
  40. .tab-bar-item.bm {
  41. margin-top: 0 !important;
  42. background: transparent;
  43. position: relative;
  44. flex: inherit;
  45. width: 134rpx;
  46. }
  47. .tab-bar-item image {
  48. width: 48rpx;
  49. height: 48rpx;
  50. overflow: initial;
  51. }
  52. .tab-bar-item view {
  53. font-size: 24rpx;
  54. }
  55. .tab-bar-item image.bm {
  56. position: absolute;
  57. width: 108rpx;
  58. height: 106rpx;
  59. bottom: 80%;
  60. z-index: 100;
  61. }
  62. .tab-bar-item view.bm {
  63. background: transparent;
  64. width: 100%;
  65. height: 100%;
  66. padding-top: 73rpx;
  67. z-index: 99;
  68. }

有用可以点赞收藏,嘿嘿~

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

闽ICP备14008679号