当前位置:   article > 正文

微信小程序 自定义GIF tabbar 底部导航栏

微信小程序 自定义GIF tabbar 底部导航栏

1.新建tabbar组件

2. wxml组件代码

  1. <view class="tabbar">
  2. <view class="tabbar-item" wx:for="{{ tabs }}" wx:key="{{index}}" bindtap="switchTab" data-index="{{index}}">
  3. <image class="tabbar-item-icon" style="width: 50px;height: 40px;" mode="aspectFit" src="{{item.selected ? item.iconPath : item.selectedIconPath }}" class="{{item.selected ? 'selected' : ''}}"></image>
  4. <view wx:if="{{item.text != ''}}" class="tabbar-item-text" class="{{item.selected ? 'selected' : ''}}">{{item.text}}</view>
  5. </view>
  6. </view>

注意:因为GIF展示会有缓存影响,需要发GIF放到服务器上 并且在后缀加上随机数或者时间戳进行展示

3.js 代码

  1. // components/tabbar/tabbar.js
  2. Component({
  3. /**
  4. * 组件的属性列表
  5. */
  6. properties: {
  7. },
  8. /**
  9. * 组件的初始数据
  10. */
  11. data: {
  12. tabs: [
  13. {
  14. iconPath: 'https://football.web1995.top/images/0-1.gif',
  15. selectedIconPath: 'https://football.web1995.top/images/0-2.gif',
  16. text: '',
  17. selected: true,
  18. pagePath: '/pages/home/index'
  19. },
  20. {
  21. iconPath: 'https://football.web1995.top/images/1-1.gif',
  22. selectedIconPath: 'https://football.web1995.top/images/1-2.gif',
  23. text: '',
  24. selected: false,
  25. pagePath: '/pages/message/index'
  26. },
  27. {
  28. iconPath: 'https://football.web1995.top/images/2-1.gif',
  29. selectedIconPath: 'https://football.web1995.top/images/2-2.gif',
  30. text: '',
  31. selected: false,
  32. pagePath: '/pages/user/index'
  33. },
  34. {
  35. iconPath: 'https://football.web1995.top/images/3-1.gif',
  36. selectedIconPath: 'https://football.web1995.top/images/3-2.gif',
  37. text: '',
  38. selected: false,
  39. pagePath: '/pages/user/index'
  40. },
  41. {
  42. iconPath: 'https://football.web1995.top/images/4-1.gif',
  43. selectedIconPath: 'https://football.web1995.top/images/4-2.gif',
  44. text: '',
  45. selected: false,
  46. pagePath: '/pages/user/index'
  47. }
  48. ],
  49. oldTabs: [
  50. {
  51. iconPath: 'https://football.web1995.top/images/0-1.gif',
  52. selectedIconPath: 'https://football.web1995.top/images/0-2.gif',
  53. text: '',
  54. selected: true,
  55. pagePath: '/pages/home/index'
  56. },
  57. {
  58. iconPath: 'https://football.web1995.top/images/1-1.gif',
  59. selectedIconPath: 'https://football.web1995.top/images/1-2.gif',
  60. text: '',
  61. selected: false,
  62. pagePath: '/pages/message/index'
  63. },
  64. {
  65. iconPath: 'https://football.web1995.top/images/2-1.gif',
  66. selectedIconPath: 'https://football.web1995.top/images/2-2.gif',
  67. text: '',
  68. selected: false,
  69. pagePath: '/pages/user/index'
  70. },
  71. {
  72. iconPath: 'https://football.web1995.top/images/3-1.gif',
  73. selectedIconPath: 'https://football.web1995.top/images/3-2.gif',
  74. text: '',
  75. selected: false,
  76. pagePath: '/pages/user/index'
  77. },
  78. {
  79. iconPath: 'https://football.web1995.top/images/4-1.gif',
  80. selectedIconPath: 'https://football.web1995.top/images/4-2.gif',
  81. text: '',
  82. selected: false,
  83. pagePath: '/pages/user/index'
  84. }
  85. ],
  86. },
  87. /**
  88. * 组件的方法列表
  89. */
  90. methods: {
  91. switchTab(e) {
  92. const { index } = e.currentTarget.dataset;
  93. const { tabs, oldTabs } = this.data;
  94. tabs.forEach((item, idx) => {
  95. if (index === idx) {
  96. item.iconPath = oldTabs[idx].iconPath + '?time=' + Math.random();
  97. item.selectedIconPath = oldTabs[idx].selectedIconPath + '?time=' + Math.random();
  98. item.selected = true
  99. } else {
  100. item.selected = false
  101. }
  102. })
  103. this.setData({
  104. tabs
  105. })
  106. }
  107. }
  108. })

有开发问题可以咨询我喔  wx: ai_002 

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

闽ICP备14008679号