微信小程序 自定义tabbar实例
在小程序的开发文档中,对tabbar是这样说明的:
如果小程序是一个多 tab 应用(客户端窗口的底部或顶部有 tab 栏可以切换页面),可以通过 tabBar 配置项指定 tab 栏的表现,以及 tab 切换时显示的对应页面。
Tip:
当设置 position 为 top 时,将不会显示 icon
tabBar 中的 list 是一个数组,只能配置最少2个、最多5个 tab,tab 按数组的顺序排序。
在实际开发过程中,小程序自带的tabbar样式并不能满足设计提供的开发需求,所以需要我们自定义一套属于自己的tabbar。
需求如下图:

新建template.wxml

<template name="tabbar">
<view class="tabbar_box" style=" border-top-color:{{tabbar.borderStyle}}; {{tabbar.position == 'top' ? 'top:0' : 'bottom:0'}}">
<block wx:for="{{tabbar.list}}" wx:for-item="item" wx:key="index">
<navigator class="tabbar_nav" url="{{item.pagePath}}" style="width:{{1/tabbar.list.length*100}}%; " open-type="redirect">
<image class="tabbar_icon" src="{{item.selected ? item.selectedIconPath : item.iconPath}}"></image>
<text style="color:{{item.selected ? tabbar.selectedColor : tabbar.color}}">{{item.text}}</text>
</navigator>
</block>
</view>
</template>

app.wxss里定义组件样式

.tabbar_box{
display: flex;
flex-direction: row;
justify-content: space-around;
position: fixed;
bottom: 0;
left: 0;
z-index: 999;
width: 100%;
height: 120rpx;
border-top: 1rpx solid #d7d7d7;
}
.tabbar_nav{
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
font-size: 28rpx;
height: 100%;
}
.tabbar_icon{
width: 50rpx;
height:50rpx;
}
.tabbar_nav:nth-child(2) image{
position:relative;
top:-32rpx;
width:80rpx;
height:80rpx;
}
.tabbar_nav:nth-child(2) text{
position:relative;
top:-32rpx;
}

app.js里面进行tabBar的参数配置;
默认第一个选中;然后封装一个函数,方便需要用到的页面调用

//app.js
App({
tabbar: {
color: "#242424",
selectedColor: "#fa8582",
backgroundColor: "#ffffff",
borderStyle: "#d7d7d7",
list: [
{
pagePath: "/pages/index/index",
text: "首页",
iconPath: "../../assets/images/tab1.png",
selectedIconPath: "../../assets/images/tab1_cur.png",
selected: true
},
{
pagePath: "/pages/fabu/fabu",
text: "发布",
iconPath: "../../assets/images/tab_new.png",
selectedIconPath: "../../assets/images/tab_new.png",
selected: false
},
{
pagePath: "/pages/user/user",
text: "我的",
iconPath: "../../assets/images/tab4.png",
selectedIconPath: "../../assets/images/tab4_cur.png",
selected: false
}
],
position: "bottom"
},
changeTabBar: function () {
var _curPageArr = getCurrentPages();
var _curPage = _curPageArr[_curPageArr.length - 1];
var _pagePath = _curPage.__route__;
if (_pagePath.indexOf('/') != 0) {
_pagePath = '/' + _pagePath;
}
var tabBar = this.tabbar;
for (var i = 0; i < tabBar.list.length; i++) {
console.log(_pagePath + '--' + tabBar.list[i].pagePath)
tabBar.list[i].selected = false;
if (tabBar.list[i].pagePath == _pagePath) {
tabBar.list[i].selected = true;//根据页面地址设置当前页面状态
}
}
_curPage.setData({
tabbar: tabBar
});
},
})

在这里,getCurrentPages()方法,用来获取页面page对象,执行非当前页js里的方法,打印查看一目了然

下面就是调用了,每个配置在tabbar中的页面都要,因为这个是页面跳转了
在wxml引入创建的模板并使用
<import src="../template/template.wxml" />
<template is="tabbar" data="{{tabbar}}"/>
在js中调用函数

const app = getApp();
Page({
data: {
tabbar: {},
},
onLoad: function (options){
//调用app中的函数
app.changeTabBar();
},
})

原文地址:https://www.cnblogs.com/xiao-ling-zi/p/9329612.html
微信小程序 自定义tabbar实例的更多相关文章
- 微信小程序自定义 tabbar
一定的需求情况下,无法使用小程序原生的 tabbar 的时候,需要自行实现一个和 tabbar 功能一模一样的自制组件. 查阅了海量的博客和文档之后,亲自踩坑.总结了三种在不使用微信小程序原生 tab ...
- 微信小程序自定义tabbar的实现
微信小程序自定义tabbar的实现 目的:当采用微信的自定义tabbar组件的时候,切换的时候会出现闪屏的效果:当使用微信默认的tabbar的时候,限制了tabbar的数量以及灵活配置. 方案:自己动 ...
- 微信小程序自定义tabbar的问题
个人感觉小程序的tab样式自定义的能力有所欠缺,不够美观,于是今天自己diy了一个tab 测试的时候发现,无论是使用navigator跳转(会出现点击的效果)还是用bindtap(触摸),因为没有定义 ...
- 微信小程序自定义TabBar
项目中需要根据用户角色控制TabBar中各Item的显示和隐藏,然而小程序自带TabBar没有提供隐藏某个item的功能,只好自己动手写了一个. 1.wxml文件 <view class=&qu ...
- 微信小程序 - 自定义tabbar
更新: 2019-1-18:自定义tabbar组件已发布 各种奇葩的需求,造就了我们 wxml <view class="nav-tabs"> <view cla ...
- 微信小程序 - 自定义tabbar(组件)
配置项(关于使用组件) index.wxml <!-- tabBar:tabBar配置 activeIndex: 激活页面下标 slots: 多插槽配置(需与页面一致) --> <t ...
- 微信小程序——自定义导航栏
微信头部导航栏可能通过json配置: 但是有时候我们项目需求可能需要自定义头部导航栏,如下图所示: 现在具体说一下实现步骤及方法: 步骤: 1.在 app.json 里面把 "navigat ...
- 微信小程序自定义弹窗wcPop插件|仿微信弹窗样式
微信小程序自定义组件弹窗wcPop|小程序消息提示框|toast自定义模板弹窗 平时在开发小程序的时候,弹窗应用场景还是蛮广泛的,但是微信官方提供的弹窗比较有局限性,不能自定义修改.这个时候首先想到的 ...
- 微信小程序-自定义底部导航
代码地址如下:http://www.demodashi.com/demo/14258.html 一.前期准备工作 软件环境:微信开发者工具 官方下载地址:https://mp.weixin.qq.co ...
随机推荐
- jar - 操作jar包的工具
jar - Manipulates Java Archive (JAR) files. jar命令是一种通用的存档和压缩工具,基于ZIP和ZLIB压缩格式. 常用格式: * 创建jar文件 jar c ...
- Pods
Pods Pod概念 Pod是kubernetes集群应用中的创建和部署的最小.最简单的kubernetes对象模型单元的基本执行单元.Pod表示在集群中运行的进程. Pod 封装了应用程序的容器(一 ...
- spring cloud (四) 请求熔断 feign
1 pom.xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns="h ...
- typeScript学习随笔(一)
TypeScript学习随笔(一) 这么久了还不没好好学习哈这么火的ts,边学边练边记吧! 啥子是TypeScript TypeScript 是 JavaScript 的一个超集,支持 es6 标准 ...
- lca:异象石(set+dfs序)
题目:https://loj.ac/problem/10132 #include<bits/stdc++.h> using namespace std; ,N,k=,head[]; str ...
- Alpha冲刺笔记十:第十天
课程名称:软件工程1916|W(福州大学) 作业要求:项目Alpha冲刺(十天冲刺) 团队名称:葫芦娃队 作业目标:在十天冲刺里对每天的任务进行总结. 随笔汇总:https://www.cnblogs ...
- spark中的scalaAPI之RDDAPI常用操作
package com.XXX import org.apache.spark.storage.StorageLevel import org.apache.spark.{SparkConf, Spa ...
- [51Nod 1220] - 约数之和 (杜教筛)
题面 令d(n)d(n)d(n)表示nnn的约数之和求 ∑i=1n∑j=1nd(ij)\large\sum_{i=1}^n\sum_{j=1}^nd(ij)i=1∑nj=1∑nd(ij) 题目分析 ...
- 解决<c:if>无else的问题
之前发了一个jstl的if标签博客,说是jsp没有提供<c:else>标签.于是有大佬评论,说<c:choose></c:choose>可以解决,通过查资料和敲代码 ...
- 02-线性结构3 Reversing Linked List (25 分)
Given a constant K and a singly linked list L, you are supposed to reverse the links of every K elem ...