微信小程序自定义tabBar简易实现

这篇具有很好参考价值的文章主要介绍了微信小程序自定义tabBar简易实现。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。


1.app.json设置custom为true开启自定义

微信小程序自定义tabBar简易实现,微信小程序,小程序

2.根目录创建自定义的tab文件

微信小程序自定义tabBar简易实现,微信小程序,小程序

index.wxml


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

index.json

{
  "component": true
}

index.js

Component({
  data: {
    selected: 0,
    color: "#7A7E83",
    selectedColor: "#3cc51f",
    list: [{
        pagePath: "/index/index",
        iconPath: "/image/icon_component.png",
        selectedIconPath: "/image/icon_component_HL.png",
        text: "组件"
      }, {
        pagePath: "/index/index3",
        iconPath: "/image/scan-svgrepo-com.png",
        selectedIconPath: "/image/scan-svgrepo-com.png",
        text: "扫码",
        diyClass: "diy"
      },
      {
        pagePath: "/index/index2",
        iconPath: "/image/icon_API.png",
        selectedIconPath: "/image/icon_API_HL.png",
        text: "接口"
      }
    ]
  },
  attached() {},
  methods: {
    switchTab(e) {
      const data = e.currentTarget.dataset
      const url = data.path
      console.log(data)
      if (data.index == 1) {
        wx.scanCode({
          success: (e) => {
            console.log(e)
          }
        })
        this.setData({
          selected: data.index
        })
      } else {
        wx.switchTab({
          url
        })
      }
    }
  }
})

index.css

/* .tab-bar {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  height: 48px;
  background: white;
  display: flex;
  padding-bottom: env(safe-area-inset-bottom);
}

.tab-bar-border {
  background-color: rgba(0, 0, 0, 0.33);
  position: absolute;
  left: 0;
  top: 0;
  width: 100%;
  height: 1px;
  transform: scaleY(0.5);
}

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

.tab-bar-item image {
  width: 27px;
  height: 27px;
}
mtrj
.tab-bar-item view {
  font-size: 10px;
}
 */





/* ------------ */

/*重新样式*/
.tab-bar {
	position: fixed;
	bottom: 0;
	left: 0;
	right: 0;
	display: flex;
	box-sizing: content-box;
	background-color: transparent;
}
 
.tab-bar-bg {
	width: 100%;
	height: 140rpx;
}
 
.tab-bar-icon {
	display: flex;
	position: absolute;
	justify-content: space-between;
	width: 100%;
}
 
.tab-bar-border {
  background-color: rgba(0, 0, 0, 0.33);
  position: absolute;
	left: 0;
	top: 0;
	width: 100%;
	height: 1px;
	transform: scaleY(0.5);
}
 
.tab-bar-item {
	flex: auto;
	text-align: center;
	display: flex;
	justify-content: center;
	align-items: center;
	flex-direction: column;
	background-color: transparent;
	height: 120rpx;
}
 
.tab-bar-item.bm {
	margin-top: 0 !important;
	background: transparent;
	position: relative;
	flex: inherit;
	width: 134rpx;
}
 
.tab-bar-item image {
	width: 48rpx;
	height: 48rpx;
	overflow: initial;
}
 
.tab-bar-item view {
	font-size: 24rpx;
}
 
.tab-bar-item image.diy {
  position: absolute;
  width: 108rpx;
  height: 114rpx;
  bottom: 50%;
  z-index: 100;
}
 
.tab-bar-item view.diy {
	background: transparent;
	width: 100%;
	height: 100%;
	padding-top: 73rpx;
	z-index: 99;
}

3.app.js全局封装一个设置tabbar选中的方法

App({
  onLaunch: function () {},
  //设置tabbar的选中
  getCurrentTabbar(selected, that) {
    if (typeof that.getTabBar === 'function' &&
      that.getTabBar()) {
      that.getTabBar().setData({
        selected: selected
      })
    }
  },
})

4.在onshow中使用选中方法

// index/index3.js
const app= getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    app.getCurrentTabbar(1,this);
  },

如果是componet的页面就直接再show中自己定义它的selected代表当前的选中态

Component({
  pageLifetimes: {
    show() {
      if (typeof this.getTabBar === 'function' &&
        this.getTabBar()) {
        this.getTabBar().setData({
          selected: 0
        })
      }
    }
  }
})

最终效果预览

微信小程序自定义tabBar简易实现,微信小程序,小程序文章来源地址https://www.toymoban.com/news/detail-769578.html

到了这里,关于微信小程序自定义tabBar简易实现的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 微信小程序自定义tabBar

    首页 分类 留言 我的 /components/index-tabbar/index.js Component({ properties: { active: { type: String, value: ‘index’ }, }, methods: { onChange(event) { wx.redirectTo({ url: /pages/${event.detail}/index , }) } } }) 模拟的 tabbar 页面写法如下: /pages/home/index.json { “usingComponents”: { “index-tabbar”: “/components/index-ta

    2024年04月24日
    浏览(30)
  • 微信小程序——自定义底部tabBar

    目录  实现步骤 1、配置信息 2、添加代码文件。  3、在该目录下编写代码即可。 二、在app.json里面添加tabBar配置 三.、在custom-tab-bar添加配置 1. 在custom-tab-bar创建如下目录 2.给index.wxml添加tabBar的结构代码   3. 给index.js 添加数据配置 和 事件方法 4. 给index.wxss 添加样式 四、

    2024年02月16日
    浏览(38)
  • 微信小程序自定义tabBar使用

    自定义使用tabbar步骤 文章目录 一、为什么要使用自定义tabBar? 二、使用步骤 总结 微信小程序官方默认的tabbar有很多局限性,比如无法调整图片和文字大小、不能动态调整个数等。 小程序开发版本:RC Build (1.06.2206271) 在小程序开发文档中找到指南--》基础能力--》自定义tab

    2024年02月09日
    浏览(43)
  • 微信小程序自定义tabbar闪烁问题

    闪烁问题原因:超过两个tabbar页不要单纯的使用官方说的show时getTabBar().setData设置选中态,自定义tabbar是多个实例的,那样只会改变当前tabbar实例的选中态,其他页面的tabbar实例并没有改变选中态。 解决tabbar闪烁问题: 1.在app.js中设置 globalData 2.在 custom-tab-bar/index.js 中设置

    2024年02月10日
    浏览(32)
  • 微信小程序自定义tabBar(边框圆角)

    先看看自定义tabBar的效果     可能图片效果不是很明显,我用红框框出来了,这样看起来明显一点。 接下来就是具体步骤了  先在pages里建两个文件夹,我现在做的项目tabBar只有两个,所以我建了两个文件夹,如果大于两个用这个方法也可以,但是不能多于五个。 app.json中

    2024年02月08日
    浏览(36)
  • 【微信小程序】-- 案例 - 自定义 tabBar(四十六)

    💌 所属专栏:【微信小程序开发教程】 😀 作  者:我是夜阑的狗🐶 🚀 个人简介:一个正在努力学技术的CV工程师,专注基础和实战分享 ,欢迎咨询! 💖 欢迎大家:这里是CSDN,我总结知识的地方,喜欢的话请三连,有问题请私信 😘 😘 😘   大家好,又见面了,

    2024年02月03日
    浏览(34)
  • 微信小程序自定义tabbar以及闪烁问题

    微信小程序的标题栏,具体描述不需要太多解释,但是很多时候,不满足于默认的配置的时候,需要我们手动的去绘制一个tabbar。比如一般社交平台软件会要求中间有一个突出的发布按钮,此时就需要重新定制下tabbar了 假设我们现在有这么一个需求,tabbar中的子项是根据ap

    2024年02月09日
    浏览(36)
  • 微信小程序子页面自定义tabbar组件

    有时候微信小程序会遇到代码合并,就比如把B小程序代码迁移到A小程序,要使得B作为A小程序的一个子页面子功能。因为本身小程序都有tabbar,原来B也有,这时候就要给B子功能自定义一个tabbar底部导航栏。(注意,这个不是微信小程序自定义tabBar,不需要app.json中设置一个

    2024年02月08日
    浏览(34)
  • 微信小程序自定义 底部 tabbar (中间凸起)

    在与 pages 文件夹同级的地方新建 custom-tab-bar 文件夹,并新建 index.wxml 、index.wxss 、index.js 、index.json 四个文件夹 注意路径!!! 复制后会报错,把图片和页面路径改掉就好了!!! 提示: 不要无脑复制,复制到自己的项目中后记得更改图片、页面路径!!! 如需自定义 头

    2024年02月20日
    浏览(31)
  • 微信小程序自定义tabbar【中间凸起样式】

    效果预览 微信开发文档:自定义tabBar 一、配置信息 在 app.json 中的 tabBar 中指定 custom 字段为 true【允许使用自定义 tabBar】 在所有 tab 页 json 中申明usingComponents 项,或者在 app.json 中全局开启 在 list 中指定自己需要 tab 示例 二、添加 tabBar 代码文件 在代码根目录下添加custom-

    2024年02月10日
    浏览(33)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包