当前位置:   article > 正文

微信小程序开发自定义tabBar_微信小程序 自定义tabbar

微信小程序 自定义tabbar

1.app.json配置tabBar,并且custom:true

        

2.与pages文件夹同级新建custom-tab-bar文件夹,名字一定不能错

        

3.

        custom-tab-bar/index.wxml

        注意路径:data-path="{{'/'+item.pagePath}}",

  1. <!--custom-tab-bar/custom-tab-bar.wxml-->
  2. <view class="tab-bar">
  3. <view class="tab-bar-border"></view>
  4. <view wx:for="{{list}}" wx:key="index" class="tab-bar-item" data-path="{{'/'+item.pagePath}}" data-index="{{index}}" bindtap="switchTab">
  5. <image src="{{selected === index ? item.selectedIconPath : item.iconPath}}"></image>{{selected }} -- {{index}}
  6. <view style="color: {{selected === index ? selectedColor : color}}">{{item.text}}</view>{{selected }} -- {{index}}
  7. </view>
  8. </view>

    custom-tab-bar/index.js,这里配置上需要展示的tabBar页面

  1. Component({
  2. data: {
  3. selected: "0",
  4. "color": "#000",
  5. "selectedColor": "#1296db",
  6. "list": [
  7. {
  8. "text": "首页",
  9. "pagePath": "pages/index/index",
  10. "iconPath": "../images/home.png",
  11. "selectedIconPath": "../images/home-fill.png"
  12. },
  13. {
  14. "text": "服务",
  15. "pagePath": "pages/serve/serve",
  16. "iconPath": "../images/serve.png",
  17. "selectedIconPath": "../images/serve-fill.png"
  18. },
  19. {
  20. "text": "购物",
  21. "pagePath": "pages/cart/cart",
  22. "iconPath": "../images/cart.png",
  23. "selectedIconPath": "../images/cart_fill.png"
  24. },
  25. {
  26. "text": "健康",
  27. "pagePath": "pages/health/health",
  28. "iconPath": "../images/health.png",
  29. "selectedIconPath": "../images/health-fill.png"
  30. },
  31. {
  32. "text": "我的",
  33. "pagePath": "pages/mine/mine",
  34. "iconPath": "../images/user.png",
  35. "selectedIconPath": "../images/user-fill.png"
  36. }
  37. ]
  38. },
  39. attached() {
  40. },
  41. methods: {
  42. switchTab(event) {
  43. // data为接受到的参数
  44. const data = event.currentTarget.dataset;
  45. console.log('data',data.index);
  46. // 取出参数中的path作为路由跳转的目标地址
  47. const url = data.path
  48. wx.switchTab({url})
  49. this.setData({
  50. selected: data.index
  51. })
  52. console.log('selected',this.data.selected);
  53. },
  54. }
  55. })

custom-tab-bar/index.json

  1. {
  2. "component": true
  3. }

custom-tab-bar/index.wxss

  1. .tab-bar {
  2. position: fixed;
  3. bottom: 0;
  4. left: 0;
  5. right: 0;
  6. height: 48px;
  7. background: white;
  8. display: flex;
  9. padding-bottom: env(safe-area-inset-bottom);
  10. }
  11. .tab-bar-border {
  12. background-color: rgba(0, 0, 0, 0.33);
  13. position: absolute;
  14. left: 0;
  15. top: 0;
  16. width: 100%;
  17. height: 1px;
  18. transform: scaleY(0.5);
  19. }
  20. .tab-bar-item {
  21. flex: 1;
  22. text-align: center;
  23. display: flex;
  24. justify-content: center;
  25. align-items: center;
  26. flex-direction: column;
  27. }
  28. .tab-bar-item image {
  29. width: 27px;
  30. height: 27px;
  31. }
  32. .tab-bar-item view {
  33. font-size: 10px;
  34. }

4.此时tabBar已经存在了,但是存在切换延迟问题,需要点击两次icon才能正确选中

解决方法:在跳转的页面的onShow生命周期里面加上,selected是custom-tab-bar里面当前设置选中下标的参数名,而值是对应tabbar数组里面对应的下标 ,相应的修改 selected的值

  1. onShow(){
  2. if (typeof this.getTabBar === 'function' && this.getTabBar()) {
  3. this.getTabBar().setData({
  4. selected: 2,
  5. active: getApp().globalData.selected
  6. })
  7. console.log(this.getTabBar().__data__.selected)
  8. this.setData({
  9. active:this.getTabBar().__data__.selected
  10. })
  11. }
  12. }

如果此时设置完,存在切换闪烁的问题,可以把custom-tab-ba/index.js中this.setData({selected: data.index})注释掉

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

闽ICP备14008679号