微信小程序自定义tabbar栏【中间突出样式】

news/2024/7/20 1:48:43 标签: 微信小程序, javascript, 小程序

文章目录

  • 前言
  • 一、自定义tabbar栏 配置
  • 二、添加自定义tabbar栏组件
      • 添加组件代码
      • 创建全局字段
      • 在组件中保存重要字段
  • 三、效果展示
  • 总结

前言

昨天主管突然给我说小程序>微信小程序默认的 tabBar 不美观,让我改成中间突出的那种样式。纵然我心里面有千般不情愿,但还是接下了这个任务。查了一下文档 自定义 tabBar 发现有这个方法,有思路了就赶紧搞起来,以下是我的开发经验分享。


一、自定义tabbar栏 配置

  • 在 app.json 文件中的 tabBar 中指定 custom 字段为 true(意思是允许使用自定义 tabBar);
  • 在 app.json 中全局开启使用组件,或者在所有涉及的 tab 页 json 中申明usingComponents 项;
  • 在 app.json 中添加作为 tabBar 栏的页面;

示例代码

  "tabBar": {
    "custom": true,
    "color": "#afafaf",
    "selectedColor": "#0099f6",
    "backgroundColor": "#F7F8F8",
    "list": [
      {
        "pagePath": "pages/index/index",
        "text": "首页"
      },
      {
        "pagePath": "pages/goodIndexCopy/goodIndexCopy",
        "text": "易购商城"
      },
      {
        "pagePath": "pages/release/release",
        "text": "发布"
      },
      {
        "pagePath": "pages/nearby/nearby",
        "text": "本地"
      },
      
      {
        "pagePath": "pages/mine/mine",
        "text": "我的"
      }
    ]
  },
  "usingComponents": {},

pagePath 是自己添加的页面,text 是tabBar上展示的文字。

二、添加自定义tabbar栏组件

在根目录下创建 custom-tab-bar 文件夹,并在该文件夹下新建 Component,或者新建 Page,但是这种创建方式需要自己改动一些代码,在这里我们选用新建 Component 的方式。

添加组件代码

1、完善 wxml 文件代码,tabBar 栏需要展示的页面是一个固定的数组,可以使用 wx:for 循环展示,在这里用到 selected 这个字段,这个字段的作用是帮助展示 tabBar 选中和未选中的样式。

<!--custom-tab-bar/index.wxml-->
<view class="tab-bar">
  <view wx:for="{{list}}" wx:key="index" class="tab-bar-item {{item.bulge?'bulge':''}}" data-path="{{item.pagePath}}" data-index="{{index}}" bindtap="switchTab">
    <view  wx:if="item.bulge" class="tab-bar-bulge"></view>
    <image class="image" src="{{selected == index ? item.selectedIconPath : item.iconPath}}"></image>
    <!-- <view  wx:if="{{item.text}}" style="color: {{selected === index ? selectedColor : color}}" class="tab-bar-view">{{item.text}}</view> -->
    <view  class="tab-bar-view" style="color: {{selected === index ? selectedColor : color}}">{{item.text}}</view>
  </view>
</view>

2、完善 js 文件代码,list 数组就是在 tabBar 栏展示的页面信息,switchTab 方法作用可以出看来是负责跳转页面。其它的字段相信各位都知道,这里就不再描述。

javascript">  /**
   * 组件的初始数据
   */
  data: {
    selected: 0,
    color: "#afafaf",
    selectedColor: "#0099f6",
    backgroundColor: "#F7F8F8",
    list: [
      {
        pagePath: "/pages/index/index",
        iconPath: "/images/icon/wtc/icon_zhuye2.png",
        selectedIconPath: "/images/icon/wtc/icon_zhuye2_d.png",
        text: "首页",
      },
      {
        pagePath: "/pages/goodIndexCopy/goodIndexCopy",
        iconPath: "/images/icon/wtc/icon_pintuan2.png",
        selectedIconPath: "/images/icon/wtc/icon_pintuan2_d.png",
        text: "易购商城"
      },
      {
        pagePath: "/pages/release/release",
        bulge:true,
        iconPath: "/images/add.png",
        selectedIconPath: "/images/add-active.png",
        text: "发布"
      },
      {
        pagePath: "/pages/nearby/nearby",
        iconPath: "/images/icon/wtc/icon_pintuan3.png",
        selectedIconPath: "/images/icon/wtc/icon_pintuan3_d.png",
        text: "本地",
      },
      {
        pagePath: "/pages/mine/mine",
        iconPath: "/images/icon/wtc/icon_wode3.png",
        selectedIconPath: "/images/icon/wtc/icon_wode3_d.png",
        text: "我的"
      },
    ]
  },
    /**
   * 组件的方法列表
   */
  methods: {
    switchTab(e) {
      // console.log(e);
      const data = e.currentTarget.dataset;
      const url = data.path;
      wx.switchTab({url})
    }
  }

3、完善 wxss 文件代码。

.tab-bar {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  height: 50px;
  background: #FFF;
  display: flex;
  line-height: 1.2;
  padding-bottom: env(safe-area-inset-bottom);
  border-top: 1px solid #e6e6e6;
}

.tab-bar-item {
  flex: 1;
  text-align: center;
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
}

.tab-bar-item .image {
  width: 26px;
  height: 26px;
}
.bulge {
  background-color: #FFF;
}
.bulge .tab-bar-bulge{
  position: absolute;
  z-index: -1;
  width: 64px;
  height: 64px;
  top: -24px;
  border-radius: 50%;
  border-top: 1px solid #e6e6e6;
  background-color: #FFF;
}
.bulge .image{
  position: absolute; 
  width: 50px;
  height: 50px;
  top: -20px;
}
.bulge .tab-bar-view{
  position: relative;
  bottom: -16px;
  margin-top: 4px;
}

.tab-bar-item .tab-bar-view {
  font-size: 12px;
  margin-top: 4px;
}

创建全局字段

做完以上工作之后,我们可以就可以看一下效果了,是不是就以为这样就可以了呢?但是事与愿违,会发现这里存在 bug,在我们点击 tabBar 栏进行跳转的时候会发现页面跳转过去了,但是对应页面的 tabBar 没有改变颜色。为了解决这个 bug,需要添加全局字段。在 app.js 文件中创建该字段。

javascript">  globalData: {
    selected: 0
  },

在组件中保存重要字段

全局字段创建完成之后,我们需要在组件 js 文件中使用该字段,在 ready 函数中保存这个字段,在点击 tabBar 栏时,把相应的index 赋值给这个全局字段。

javascript">// 引入全局函数
const app = getApp()
Component({
  /**
   * 组件的初始数据
   */
  data: {
    selected: 0,
    color: "#afafaf",
    selectedColor: "#0099f6",
    backgroundColor: "#F7F8F8",
    list: [
      {
        pagePath: "/pages/index/index",
        iconPath: "/images/icon/wtc/icon_zhuye2.png",
        selectedIconPath: "/images/icon/wtc/icon_zhuye2_d.png",
        text: "首页",
      },
      {
        pagePath: "/pages/goodIndexCopy/goodIndexCopy",
        iconPath: "/images/icon/wtc/icon_pintuan2.png",
        selectedIconPath: "/images/icon/wtc/icon_pintuan2_d.png",
        text: "易购商城"
      },
      {
        pagePath: "/pages/release/release",
        bulge:true,
        iconPath: "/images/add.png",
        selectedIconPath: "/images/add-active.png",
        text: "发布"
      },
      {
        pagePath: "/pages/nearby/nearby",
        iconPath: "/images/icon/wtc/icon_pintuan3.png",
        selectedIconPath: "/images/icon/wtc/icon_pintuan3_d.png",
        text: "本地",
      },
      {
        pagePath: "/pages/mine/mine",
        iconPath: "/images/icon/wtc/icon_wode3.png",
        selectedIconPath: "/images/icon/wtc/icon_wode3_d.png",
        text: "我的"
      },
    ]
  },
  ready: function() {
    this.setData({
      selected: app.globalData.selected
    })
  },
  /**
   * 组件的方法列表
   */
  methods: {
    switchTab(e) {
      // console.log(e);
      const data = e.currentTarget.dataset;
      const url = data.path;
      app.globalData.selected = data.index;
      wx.switchTab({url})
    }
  }
})

添加完成后,可以测试一下效果,发现刚才的 bug 已经解决。perfect!!

三、效果展示

在这里插入图片描述

总结

经过了不断探索终于完成了这个功能,事后想了一下还是自己的本事不到家,以后还是需要不断提升自己的能力 ~ ~ ~


http://www.niftyadmin.cn/n/26196.html

相关文章

字节序,主机字节序与网络字节序

&#x1f4cc;————本章重点————&#x1f4cc; &#x1f517;了解字节序的概念; &#x1f517;了解大小端的概念;&#x1f517;了解主机字节序和网络字节序的区别;&#x1f517;学习主机字节序和网络字节序相机转化的接口; ✨————————————✨字节序 概念&am…

【图像处理OpenCV(C++版)】——4.1 对比度增强之灰度直方图

前言&#xff1a; &#x1f60a;&#x1f60a;&#x1f60a;欢迎来到本博客&#x1f60a;&#x1f60a;&#x1f60a; &#x1f31f;&#x1f31f;&#x1f31f; 本专栏主要结合OpenCV和C来实现一些基本的图像处理算法并详细解释各参数含义&#xff0c;适用于平时学习、工作快…

ESP32基于Arduino框架,SD卡+MAX98357模块+MP3播放器

ESP32基于Arduino框架&#xff0c;SD卡MAX98357模块MP3播放器&#x1f3ac;原创作者的制作教程讲解以及源码&#xff1a; 35 ESP32之简单的完整功能SD卡MP3播放器的制作讲解&#xff08;ESP32-audioI2S库介绍&#xff09;- 基于Arduino链接&#xff1a;https://pan.baidu.com/s…

ArcGIS基础实验操作100例--实验84查找面到直线的最近点位置

本实验专栏参考自汤国安教授《地理信息系统基础实验操作100例》一书 实验平台&#xff1a;ArcGIS 10.6 实验数据&#xff1a;请访问实验1&#xff08;传送门&#xff09; 高级编辑篇--实验84 查找面到直线的最近点位置 目录 一、实验背景 二、实验数据 三、实验步骤 &#…

推荐两个好用的虚拟机、SSH 终端开源工具(Virtual Box、WindTerm)

笔者最近因一些变故&#xff0c;加上阳了&#xff0c;停更了一段时间&#xff0c;并提前回老家过年了。因并没有带笔记本电脑回去&#xff0c;故在折腾了一番老家电脑后&#xff0c;选择拥抱开源&#xff0c;使用一些开源的工具&#xff0c;而非习惯的 VMware Workstation 和 S…

seo的基本知识(概述网站内部优化和外部优化)

了解网站外部优化的4大重点 网站优化的时候都会重视网站的外部优化&#xff0c;所以网站外部优化的4大重点&#xff01;今天就来和大家说一说&#xff01; 1.高质量的内容和外链 未来的SEO道路高质量的有价值的内容是非常重要的&#xff0c;还有就是高质量的外链也是重要之…

从mmdetection的dataLoader所生成的data字典(包含DataContainer对象)里面获取tensor以及shape的方法

这篇文章是讲述从mmdetection的dataLoader所生成的data字典(包含DataContainer对象)里面获取tensor以及shape的方法 需求: 如果我的batchsize是8,而我有50个数据,那么前6个batch都包含8个数据,最后一个batch有2个. 我需要每个batch都进行一次FPS计算&#xff0c;就要知道一个…

不求星光灿烂,但愿岁月静好

作者&#xff1a;非妃是公主 专栏&#xff1a;《程序人生》 个性签&#xff1a;顺境不惰&#xff0c;逆境不馁&#xff0c;以心制境&#xff0c;万事可成。——曾国藩 文章目录不求星光灿烂&#xff0c;但愿岁月静好说一说这一年的自己的收获吧2022年的追求自我学会拒绝尝试表达…