微信小程序自定义底部导航栏

效果图
在这里插入图片描述
因为官网 自定义底部导航栏的api需求的基础库版本比较高 以及组建的所以底部导航栏用的模块化封装的
wxml部分

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

js部分

// 获取应用实例
const App = getApp()

let _compData = {
	'_tabbar.color': "#7A7E83",
	'_tabbar.selectedColor': "#FC625D",
	'_tabbar.selected': 0,
	'_tabbar.list': [],
	'_tabbar.pageName': null
}
let _tabbarInstance = {
	initTabbar(e) {
		if (e.initPage) {
			this.setData({ '_tabbar.pageName': e.initPage, '_tabbar.selected': e.selected,})
			if (!this.hasInitTabbar) {
				this.hasInitTabbar = true
			let list = [{
				"iconPath": "/image/icon-home1.png",
				"selectedIconPath": "/image/icon-home2.png",
				"pagePath": "/pages/index/index",
				"text": "首页"
			},
			{
				"iconPath": "/image/icon-classify1.png",
				"selectedIconPath": "/image/icon-classify2.png",
				"pagePath": "/pages/shopDetail/shopDetail",
				"text": "分类"
			},
			{
				"iconPath": "/image/icon-shopCart1.png",
				"selectedIconPath": "/image/icon-shopCart2.png",
				"pagePath": "/pages/shopCart/shopCart",
				"text": "购物车"
			},
			{
				"iconPath": "/image/icon-my1.png",
				"selectedIconPath": "/image/icon-my2.png",
				"pagePath": "/pages/userCenter/userCenter",
				"text": "我的"
			}
			]
			this.setData({
				'_tabbar.list': list
			})
			}
		}
	},
	switchTab(e) {
		const data = e.currentTarget.dataset
		const url = data.path
		wx.switchTab({
			url
		})
	}
}

function Tabbar(PageInstance) {
	Object.assign(PageInstance, _tabbarInstance)
	PageInstance.setData(_compData)
	return this
}
module.exports = {
	Tabbar
}

wxss部分

.tab-bar { position: fixed;bottom: 0;left: 0;right: 0;height: 110rpx;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;position:relative }
.tab-bar-item cover-image { width: 30px;height: 30px; }
.tab-bar-item cover-view { font-size: 10px; }
.shop-cart-bage { position:absolute;left:50%;top:0;transform:translate(-50%);margin-left:40rpx;min-width:40rpx;height:40rpx;line-height:40rpx;border-radius:20rpx;background-color:rgba(252, 98, 93, 1);color:#fff;padding:0 10rpx;box-shadow:0 0 5px rgba(0, 0, 0, 0.5)}

例子 index.wxml页面调用

<!-- 自定义底部导航栏 -->
<import src="../../component/CustomTabbar/CustomTabbar.wxml" /> 
<template is="Tabbar" data="{{ ..._tabbar }}" />

缺点 因为组件是在page里面 第一次加载的时候tabbar闪动 影响用户体验,
由于这个缺点封装完之后 就没再用

对于解决tabbar闪动的方案我是把tabar作为一个页面 然后把每一个导航栏的内容封装成组件 插入tabar页面中

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
微信小程序中,自定义底部导航栏的代码如下: ```html <view class="tab-bar"> <view class="tab-item" wx:for="{{tabs}}" wx:key="{{item.id}}" bindtap="switchTab" data-id="{{item.id}}"> <image class="tab-icon" src="{{item.icon}}" /> <text class="tab-title">{{item.title}}</text> </view> </view> ``` 在上面的代码中,我们使用了一个 `view` 标签作为底部导航栏的容器,并在其中使用了一个 `wx:for` 循环渲染每个导航项。每个导航项都是一个 `view` 标签,并包含了一个图标和一个标题。我们还为每个导航项绑定了一个 `bindtap` 事件,用于在用户点击导航项时切换页面。 接下来,我们需要在页面的 `js` 文件中定义导航项的数据和切换页面的逻辑: ```javascript Page({ data: { tabs: [ { id: 0, title: '首页', icon: '/images/home.png', url: '/pages/index/index' }, { id: 1, title: '消息', icon: '/images/message.png', url: '/pages/message/message' }, { id: 2, title: '我的', icon: '/images/me.png', url: '/pages/me/me' } ], activeTab: 0 }, switchTab: function(e) { const id = e.currentTarget.dataset.id const tab = this.data.tabs.find(item => item.id === id) if (tab) { wx.switchTab({ url: tab.url }) this.setData({ activeTab: id }) } } }) ``` 在上面的代码中,我们使用了一个 `data` 对象来保存导航项的数据和当前选中的导航项。在 `switchTab` 方法中,我们通过点击事件获取到用户点击的导航项的 `id`,然后在导航项数组中查找对应的导航项,并使用 `wx.switchTab` 方法切换到对应的页面。最后,我们使用 `setData` 方法更新当前选中的导航项的 `id`。 最后,我们还需要在 `wxss` 文件中定义导航栏的样式: ```css .tab-bar { display: flex; flex-direction: row; justify-content: space-around; align-items: center; height: 100rpx; background-color: #fff; box-shadow: 0 -1px 5px #ddd; } .tab-item { display: flex; flex-direction: column; align-items: center; } .tab-icon { width: 50rpx; height: 50rpx; } .tab-title { font-size: 24rpx; margin-top: 10rpx; } ``` 在上面的代码中,我们使用了 `flex` 布局来将导航项平均分配到底部导航栏中,并设置了一些基本的样式,如高度、背景色、阴影等。我们还为导航项的图标和标题设置了一些样式,如大小、间距、字体大小等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

刘家军

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值