• 小程序自定义tabBar——原生


    目录

    一、微信官方文档:

    二、效果动图

    三、示例代码下载及配置

    四、遇到的问题

    五、新增自定义样式

    六、完整代码


     笔记:

    一、微信官方文档:

    自定义 tabBar 可以让开发者更加灵活地设置 tabBar 样式,以满足更多个性化的场景。

    微信官方文档: 自定义 tabBar | 微信开放文档微信开发者平台文档https://developers.weixin.qq.com/miniprogram/dev/framework/ability/custom-tabbar.html

    • 在 app.json 中的 tabBar 项指定 custom 字段,同时其余 tabBar 相关配置也补充完整。
    • 所有 tab 页的 json 里需声明 usingComponents 项,也可以在 app.json 全局开启。

    二、效果动图

    简单效果:

    三、示例代码下载及配置

     为了方便,可以直接下载demo

     

    打开后,我新增一个页面,如下结构:

     在app,json中 加上   "custom": true,   后,后面的页面配置就会失效。但还是要配置,list不能为空。

     由于是直接下载微信官方文档提供的示例,对于我们想要的效果,仅需改动或新增即可。

    四、遇到的问题

    但也是遇到一些问题:

    •  小程序自定义导航栏点击时会闪一下  将 cover-view 换成 view ,将 cover-image 换成 iamge 即可
    • 报错:Some selectors are not allowed in component wxss, including tag name selectors, ID selectors, and attribute selectors。class="cover-image" 原因是:在组件 即Component的wxss中使用了不推荐的选择器, 用clas 选择器 即可   

    五、新增自定义样式

    我的自定义tabbar:

    custom-tab-bar组件中:

    • js
    1. Component({
    2. data: {
    3. selected: 0,
    4. color: "#7A7E83",
    5. selectedColor: "#3cc51f",
    6. list: [{
    7. pagePath: "/index/index",
    8. iconPath: "/image/icon_component.png",
    9. selectedIconPath: "/image/icon_component_HL.png",
    10. text: "组件"
    11. },
    12. {
    13. pagePath: "/index/index3",
    14. iconPath: "/image/add.png",
    15. selectedIconPath: "/image/add.png",
    16. text: "发布"
    17. },
    18. {
    19. pagePath: "/index/index2",
    20. iconPath: "/image/icon_API.png",
    21. selectedIconPath: "/image/icon_API_HL.png",
    22. text: "接口"
    23. }
    24. ]
    25. },
    26. attached() {},
    27. methods: {
    28. switchTab(e) {
    29. const data = e.currentTarget.dataset
    30. const url = data.path
    31. wx.switchTab({
    32. url
    33. })
    34. this.setData({
    35. selected: data.index
    36. })
    37. },
    38. },
    39. })
    • json
    1. {
    2. "component": true
    3. }
    • wxml
    1. <view class="tab-bar">
    2. <view wx:for="{{list}}" wx:key="index" class="tab-bar-item" data-path="{{item.pagePath}}" wx:for-index="index" data-index="{{index}}" bindtap="switchTab">
    3. <image class=" {{selected == index?'active':''}}" src="{{selected===index?item.selectedIconPath:item.iconPath}}">image>
    4. <view class="{{ item.tuqi }}" wx:if="{{ selected===index }}" class="cover-view" style="color: {{selected === index ? selectedColor : color}} ">{{item.text}}view>
    5. view>
    6. view>
    • 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. border-top: 1px solid #ccc;
    11. border-top-left-radius: 25rpx;
    12. border-top-right-radius: 25rpx;
    13. }
    14. .tab-bar-item {
    15. flex: 1;
    16. text-align: center;
    17. display: flex;
    18. justify-content: center;
    19. align-items: center;
    20. flex-direction: column;
    21. }
    22. .tab-bar-item image {
    23. transition: all .25s ease;
    24. width: 27px;
    25. height: 27px;
    26. }
    27. .tab-bar-item .cover-view {
    28. font-size: 20rpx;
    29. margin-top: 5rpx;
    30. animation-name: wipe-in-up; /*动画的名称 */
    31. animation-duration: 1000ms; /*动画从开始到结束的时间*/
    32. }
    33. .active {
    34. transition: property duration timing-function delay;
    35. transform: scale(1.2);
    36. border-radius: 50rpx;
    37. margin-top: -50rpx;
    38. transform: translateY(-10rpx);
    39. animation-name: wipe-in-up; /*动画的名称 */
    40. animation-duration: 1000ms; /*动画从开始到结束的时间*/
    41. }
    42. @keyframes wipe-in-up {
    43. from {
    44. clip-path: inset(100% 0 0 0);
    45. }
    46. to {
    47. clip-path: inset(0 0 0 0);
    48. }
    49. }
    50. [transition-style="in:wipe:up"] {
    51. animation: 2.5s cubic-bezier(.25, 1, .30, 1) wipe-in-up both;
    52. }
    53. .tq {
    54. margin-top: -50rpx;
    55. }

    六、完整代码

    完整代码:GitHub - YXHLHM/-TabBar-: 微信小程序 自定义 tabBar

    拜~~

  • 相关阅读:
    个推支持海外消息推送,助力APP扬帆出海
    C++多线程--std::thread
    Java的一些常见类【万字介绍】
    MySQL如何对SQL做prepare预处理(解决IN查询SQL预处理仅能查询出一条记录的问题)
    Word文档设置表格可跨页
    vue路由切换时,若是一个路径但是参数不同,页面不会变化
    我上半年深陷泥淖的往事
    力扣:125. 验证回文串(Python3)
    一个命令就可启用的微信机器人WhoChat
    访问 GitHub 方法
  • 原文地址:https://blog.csdn.net/qq_61122628/article/details/127453014