&._微信小程序底部选项,选中之后图片不变">
当前位置:   article > 正文

微信小程序自定义tabBar(超详细简单以及tabBar选中效果不改变解决办法)_微信小程序底部选项,选中之后图片不变

微信小程序底部选项,选中之后图片不变

我们可以先去了解一下官方文档: https://developers.weixin.qq.com/miniprogram/dev/framework/ability/custom-tabbar.html

老规矩先看一下效果图:

项目结构:

custom-tab-bar/index.wxml

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

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 cover-image {
  29. width: 27px;
  30. height: 27px;
  31. }
  32. .tab-bar-item cover-view {
  33. font-size: 10px;
  34. }

custom-tab-bar/index.js

  1. Component({
  2. data: {
  3. selected: 0,
  4. color: "#999999",
  5. selectedColor: "#F7393F",
  6. list: [{
  7. pagePath: "/pages/index/index",
  8. iconPath: "/img/base/icon-home.png",
  9. selectedIconPath: "/img/base/icon-home-red.png",
  10. text: "首页"
  11. }, {
  12. pagePath: "/pages/my/my-chat/index",
  13. iconPath: "/img/base/icon-chat.png",
  14. selectedIconPath: "/img/base/icon-chat-red.png",
  15. text: "聊天"
  16. }, {
  17. pagePath: "/pages/live-streaming/index",
  18. iconPath: "/img/base/icon-live-streaming.png",
  19. selectedIconPath: "/img/base/icon-live-streaming-red.png",
  20. text: "直播"
  21. }, {
  22. pagePath: "/pages/my/my-shopping-trolley/index",
  23. iconPath: "/img/base/icon-shopping-trolley.png",
  24. selectedIconPath: "/img/base/icon-shopping-trolley-red.png",
  25. text: "购物车"
  26. }, {
  27. pagePath: "/pages/my/my-personal-center/index",
  28. iconPath: "/img/base/icon-user.png",
  29. selectedIconPath: "/img/base/icon-user-red.png",
  30. text: "个人中心"
  31. }]
  32. },
  33. attached() {
  34. },
  35. methods: {
  36. switchTab(e) {
  37. const data = e.currentTarget.dataset
  38. const url = data.path
  39. wx.switchTab({url})
  40. this.setData({
  41. selected: data.pages
  42. })
  43. }
  44. }
  45. })

custom-tab-bar/index.json

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

以上就是所以自定义tabBar代码

接下来看看怎么引入:

全局配置app.json

  1. "tabBar": {
  2. "custom": true,
  3. "color": "#999999",
  4. "selectedColor": "#F7393F",
  5. "borderStyle": "black",
  6. "list": [
  7. {
  8. "pagePath": "pages/index/index",
  9. "iconPath": "img/base/icon-home.png",
  10. "selectedIconPath": "img/base/icon-home-red.png",
  11. "text": "首页"
  12. },
  13. {
  14. "pagePath": "pages/my/my-chat/index",
  15. "iconPath": "img/base/icon-chat.png",
  16. "selectedIconPath": "img/base/icon-chat-red.png",
  17. "text": "聊天"
  18. },
  19. {
  20. "pagePath": "pages/live-streaming/index",
  21. "iconPath": "img/base/icon-live-streaming.png",
  22. "selectedIconPath": "img/base/icon-live-streaming-red.png",
  23. "text": "直播"
  24. },
  25. {
  26. "pagePath": "pages/my/my-shopping-trolley/index",
  27. "iconPath": "img/base/icon-shopping-trolley.png",
  28. "selectedIconPath": "img/base/icon-shopping-trolley-red.png",
  29. "text": "购物车"
  30. },
  31. {
  32. "pagePath": "pages/my/my-personal-center/index",
  33. "iconPath": "img/base/icon-user.png",
  34. "selectedIconPath": "img/base/icon-user-red.png",
  35. "text": "个人中心"
  36. }
  37. ]
  38. },

主要一行代码"custom": true,

点选选中效果不改变或者点击两次才改变解决办法:

在每个页面js中onShow方法里面加入

  1. onShow: function() {
  2. if (typeof this.getTabBar === 'function' && this.getTabBar()) {
  3. this.getTabBar().setData({
  4. selected: 0 //这个数字是当前页面在tabBar中list数组的索引
  5. })
  6. }
  7. }

问题就解决了

 

如果对你有帮助,请关注一下博主的小程序支持一下, 在此谢谢了

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