当前位置:   article > 正文

微信小程序自定义tabBar详细教程,且自适应尺寸和实现高斯模糊版_微信小程序tabbar 图片大小怎么调整

微信小程序tabbar 图片大小怎么调整

进入下面小程序可以体验效果

 

1、需要在app.json配置

custom 必须是true

  1. "tabBar": {
  2. "custom": true,
  3. "selectedColor": "#33a3dc",
  4. "list": [
  5. {
  6. "iconPath": "asset/imge/hu.png",
  7. "selectedIconPath": "asset/imge/hu-fill.png",
  8. "pagePath": "pages/index/index",
  9. "text": "心动"
  10. },
  11. {
  12. "iconPath": "asset/imge/per.png",
  13. "selectedIconPath": "asset/imge/per-fill.png",
  14. "pagePath": "pages/my/my",
  15. "text": "我的"
  16. }
  17. ]
  18. },

 2、根目录新建文件夹

必须是 “custom-tab-bar” 这个名字

custom-tab-bar/index ,必须是index

然后该文件夹目录下的代码

WXML代码:

  1. <view class="tabbar">
  2. <view style="{{select==index&&index==0?'color: #ef3166;':''}}"
  3. class="tabbar-item {{ select === index ? 'tabbar-select' : '' }}" wx:for="{{ list }}"
  4. wx:key="index"
  5. data-page="{{ item.pagePath }}"
  6. data-index="{{ index }}"
  7. bindtap="selectPage"
  8. >
  9. <block>
  10. <image class="tabbar-item-image"src="{{ select === index ? item.selectedIconPath : item.iconPath }}"></image>
  11. <text class="tabbar-item-text">{{ item.text }}</text>
  12. </block>
  13. </view>
  14. </view>

 js代码:

  1. Component({
  2. data: {
  3. select: 0,
  4. list: [
  5. {
  6. iconPath: "/asset/imge/hu.png",
  7. selectedIconPath: "/asset/imge/hu-fill.png",
  8. pagePath: "/pages/index/index",
  9. text: "心动"
  10. },
  11. {
  12. iconPath: "/asset/imge/per.png",
  13. selectedIconPath: "/asset/imge/per-fill.png",
  14. pagePath: "/pages/my/my",
  15. text: "我的"
  16. }
  17. ]
  18. },
  19. methods: {
  20. selectPage(e) {
  21. const { page } = e.currentTarget.dataset;
  22. wx.switchTab({
  23. url: page,
  24. })
  25. }
  26. }
  27. })

wxss代码:

高斯模糊代码

backdrop-filter: blur(10px);

background-color: rgb(0 0 0 / .10);

  1. .tabbar {
  2. width: 100%;
  3. display: flex;
  4. backdrop-filter: blur(10px);
  5. background-color: rgb(0 0 0 / .10);
  6. position: fixed;
  7. bottom: 0;
  8. padding-top: 10rpx;
  9. z-index: 9999;
  10. box-shadow: 0rpx -2rpx 20rpx 2rpx rgba(165,165,165,0.34);
  11. background-color: rgba(238, 238, 238, 0.89);
  12. padding-bottom:calc(10rpx + env(safe-area-inset-bottom) / 2);
  13. }
  14. .tabbar-item {
  15. flex: 1;
  16. display: flex;
  17. flex-direction: column;
  18. justify-content: center;
  19. align-items: center;
  20. color: #7c7c7c;
  21. }
  22. .tabbar-item-image {
  23. width: 50rpx;
  24. height: 50rpx;
  25. }
  26. .tabbar-item-text {
  27. font-size: 28rpx;
  28. margin-top: 1rpx;
  29. }
  30. .tabbar-select {
  31. color: #0a993a;
  32. }

page 索引页面代码(必须)

例如:我的

在需要跳转的页面当作加入下面代码,否则tabBar不生效。微信自定义tabBar必须的方式

js代码中需要加入该函数,select 根据索引属性填数值就行

  1. /**
  2. * 生命周期函数--监听页面显示
  3. */
  4. onShow(){
  5. if (typeof this.getTabBar === 'function' &&
  6. this.getTabBar()) {
  7. this.getTabBar().setData({
  8. // 根据tab的索引顺序是1
  9. select: 1
  10. })
  11. }
  12. },

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