微信小程序自定义tabbar【中间凸起样式

简介: 微信小程序自定义tabbar【中间凸起样式

微信小程序自定义tabBar样式,选中后中间凸起

效果预览

+20.png
一、配置信息

在 app.json 中的 tabBar 中指定 custom 字段为 true【允许使用自定义 tabBar】


在所有 tab 页 json 中申明usingComponents 项,或者在 app.json 中全局开启


在 list 中指定自己需要 tab


示例

"tabBar": {
    "custom": true,
    "color": "#515151",
    "selectedColor": "#DAA520",
    "backgroundColor": "#000000",
    "list": [
        {
            "pagePath": "pages/index/index",
            "text": "首页"
        },
        {
            "pagePath": "pages/hospital/hospital",
            "text": "医院"
        },
        {
            "pagePath": "pages/publish/publish",
            "text": "item3"
        },
        {
            "pagePath": "pages/popularization/popularization",
            "text": "科普"
        },
        {
            "pagePath": "pages/me/me",
            "text": "我的"
        }
    ]
},
"usingComponents": {},

二、添加 tabBar 代码文件

在代码根目录下添加custom-tab-bar文件夹,并在该文件夹下新建 page,文件结构如下


|-- cusotm-tab-bar

   |-- index.js

   |-- index.json

   |-- index.wxml

   |-- index.wxss


三、编写 tabBar 代码

wxml 代码

<!--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>

js 代码

// custom-tab-bar/index.js
Component({
  data: {
    color: "#515151",
    selectedColor: "#DAA520",
    backgroundColor: "#ffffff",
    list: [
      {
        pagePath: "/pages/index/index",
        text: "首页",
        iconPath: "/images/tabbar/index.png",
        selectedIconPath: "/images/tabbar/index-selected.png"
      },
      {
        pagePath: "/pages/hospital/hospital",
        text: "医院",
        iconPath: "/images/tabbar/hospital.png",
        selectedIconPath: "/images/tabbar/hospital-selected.png"
      },
      {
        pagePath: "/pages/publish/publish",
        bulge:true,
        text: "发布",
        iconPath: "/images/tabbar/dog.png",
        selectedIconPath: "/images/tabbar/dog-selected.png"
      },
      {
        pagePath: "/pages/popularization/popularization",
        text: "科普",
        iconPath: "/images/tabbar/popularization.png",
        selectedIconPath: "/images/tabbar/popularization-selected.png"
      },
      {
        pagePath: "/pages/me/me",
        text: "我的",
        iconPath: "/images/tabbar/me.png",
        selectedIconPath: "/images/tabbar/me-selected.png"
      },
    ]
  },
  attached() {
  },
  methods: {
    switchTab(e) {
      const data = e.currentTarget.dataset
      const url = data.path
      wx.switchTab({url}) 
    }
  }
})

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: -16px;
}
.bulge .tab-bar-view{
  position: relative;
  bottom: -16px;
  margin-top: 4px;
}
.tab-bar-item .tab-bar-view {
  font-size: 12px;
  margin-top: 4px;
}

json 代码

{
  "component": true
}

四、配置 tab 页

在每一个 tab 页的onShow函数中写入下面的代码,其中 selected 的值为每个 tab 的索引

onShow: function () {
    if (typeof this.getTabBar === 'function' && this.getTabBar()) {
        this.getTabBar().setData({
            // 首页为 0
            selected: 0
        })
    }
},


相关文章
|
9天前
|
小程序
Taro@3.x+Vue@3.x+TS开发微信小程序,根据系统主题展示不同样式(darkMode)
本文介绍如何在Taro项目中配置深色模式。通过在`src/app.config.ts`设置`darkmode`选项和在`theme.json`中定义主题变量,可以实现跟随系统主题的界面风格切换。
Taro@3.x+Vue@3.x+TS开发微信小程序,根据系统主题展示不同样式(darkMode)
|
9天前
|
小程序 开发者
Taro@3.x+Vue@3.x+TS开发微信小程序,使用自定义tabBar
本文介绍了如何在Taro项目中实现自定义tabBar。首先,在`app.config.ts`中设置`custom: true`并配置`tabBar`。
Taro@3.x+Vue@3.x+TS开发微信小程序,使用自定义tabBar
|
2月前
|
小程序
跨端技术问题之页面或组件样式在小程序、小程序插件和小程序分包中有什么差异
跨端技术问题之页面或组件样式在小程序、小程序插件和小程序分包中有什么差异
|
2月前
|
小程序 前端开发
【微信小程序-原生开发】添加自定义图标(以使用阿里图标库为例)
【微信小程序-原生开发】添加自定义图标(以使用阿里图标库为例)
50 0
|
2月前
|
JSON 小程序 数据库
【微信小程序-原生开发】实用教程14 - 列表的分页加载,触底加载更多(含无更多数据的提醒和显示,自定义组件)
【微信小程序-原生开发】实用教程14 - 列表的分页加载,触底加载更多(含无更多数据的提醒和显示,自定义组件)
67 0
|
8天前
|
小程序 前端开发 Java
SpringBoot+uniapp+uview打造H5+小程序+APP入门学习的聊天小项目
JavaDog Chat v1.0.0 是一款基于 SpringBoot、MybatisPlus 和 uniapp 的简易聊天软件,兼容 H5、小程序和 APP,提供丰富的注释和简洁代码,适合初学者。主要功能包括登录注册、消息发送、好友管理及群组交流。
23 0
SpringBoot+uniapp+uview打造H5+小程序+APP入门学习的聊天小项目
|
8天前
|
小程序 前端开发 JavaScript
【项目实战】SpringBoot+uniapp+uview2打造一个企业黑红名单吐槽小程序
【避坑宝】是一款企业黑红名单吐槽小程序,旨在帮助打工人群体辨别企业优劣。该平台采用SpringBoot+MybatisPlus+uniapp+uview2等技术栈构建,具备丰富的注释与简洁的代码结构,非常适合实战练习与学习。通过小程序搜索“避坑宝”即可体验。
22 0
【项目实战】SpringBoot+uniapp+uview2打造一个企业黑红名单吐槽小程序
|
25天前
|
存储 小程序 JavaScript
|
25天前
|
小程序 前端开发 安全
|
2月前
|
JavaScript Java 测试技术
基于SpringBoot+Vue+uniapp的汉服交易小程序的详细设计和实现(源码+lw+部署文档+讲解等)
基于SpringBoot+Vue+uniapp的汉服交易小程序的详细设计和实现(源码+lw+部署文档+讲解等)
35 7
下一篇
DDNS