uniapp 顶部选项卡 TopTabBar

官网tabBar那里,只能自己手动实现

效果图如下

1. 项目结构

+ home
+ mine
+ TopBar
+ TopBarComponent
        + guanzhu.vue 
        + tuijian.vue

2. 实现TopTab的静态UI界面

(1)顶部选项卡的布局 --- 当前为TopBar页面 

<template>
	<!-- 顶部选项卡 -->
	<view class="tabs">
		<view 
			class="uni-tab-item" 
			v-for="(tab,index) in tabBars"
			:key="tab.id"
		>
			<text 
				class="uni-tab-item-title"
				:class="{tabActive: tabIndex==tab.id}"
				@tap="tarTap(tab)"
			>
				{{tab.name}}
				<span class="tab-item-title-line"></span>
				
			</text>
		</view>
	</view>
</template>

<script>
	export default {
		name: "index-tabbar",
		props:{
			tabBars: Array,
			tabIndex: String
		},
		data(){
			return {
				
			}
		},
		methods:{
			tarTap(item){
				this.$emit("TarTap",item)
			}
		}
	}
</script>

<style>
	.tabs{
		display: flex;
		flex: 1;
		flex-direction: row;
		overflow-x: scroll;
		height: 100%;
	}
	.uni-tab-item{
		width: 100%;
		white-space: nowrap;
		line-height: 100rpx;
		height: 100rpx;
		border-bottom: 1px solid #eee;
	}
	.uni-tab-item-title{
		color: #969696;
		font-weight: bold;
		font-size: 38rpx;
		width: 150rpx;
		display: inline-block;
		text-align: center;
		color: #555;
	}
	.tabActive{
		color: #343434;
	}
	.tabActive .tab-item-title-line{
		display: block;
		border-bottom: 4rpx solid #fede33;
		border-top: 4rpx solid #fede33;
		width: 86rpx;
		margin: 0 auto;
		border-radius: 40rpx;
		margin-top: -10px;
		background-color: #FEDE33;
		box-sizing: border-box;
	}
</style>

(2)在home界面导入 TopTab选项卡组件,为其添加数据

<template>
	<view>
		<index-tabbar :tabBars="tabBars" @TarTap="TarData" :tabIndex="tabIndex" ></index-tabbar>
	</view>
</template>

<script>
	import indexTabbar from './topBar.vue';
	
	export default {
		components:{
			indexTabbar,
		},
		data() {
			return {
				tabIndex: "GuanZhu",
				tabBars:[
					{
						name: "关注",
						id: "GuanZhu"
					},
					{
						name:"推荐",
						id:"TuiJian"
					},
					{
						name: "财经",
						id: "caijing"
					},
					{
						name: "体育",
						id: 'tiyu'
					},
					{
						name: "娱乐",
						id: "yule"
					}
				],
				currentTabComponent: "GuanZhu"
			}
		},
		methods:{
			TarData(item){
				//设置id,来显示选中那个标签,显示下划线
				this.tabIndex = item.id;
				//显示标签对应的组件内容
				this.currentTabComponent = item.id
			}
		}
	}
</script>

<style>
</style>

这样静态的选项卡就出现了

3. 实现点击选项卡“推荐 或 关注”就显示对应的界面内容

该技术点是Vue动态组件,通过点击选项卡获取当前选项id名称,来显示对应组件

在Home界面,添加选项卡内容显示

<template>
	<view>
		<index-tabbar :tabBars="tabBars" @TarTap="TarData" :tabIndex="tabIndex" ></index-tabbar>
		<!-- 每个tab标题对应的具体组件内容 -->
		<component v-bind:is="currentTabComponent"></component>
	</view>
</template>

<script>
	import indexTabbar from './topBar.vue';
    //推荐选项卡  具体内容
	import TuiJian from './topBarComponent/tuijian.vue';
    关注选项卡  具体内容
	import GuanZhu from './topBarComponent/guanzhu.vue';
	export default {
		components:{
			indexTabbar,
			TuiJian,
			GuanZhu
		},
		data() {
			return {
				title: 'Hello',
				tabIndex: "GuanZhu",
				tabBars:[
					{
						name: "关注",
						id: "GuanZhu"
					},
					{
						name:"推荐",
						id:"TuiJian"
					},
					{
						name: "财经",
						id: "caijing"
					},
					{
						name: "体育",
						id: 'tiyu'
					},
					{
						name: "娱乐",
						id: "yule"
					}
				],
				currentTabComponent: "GuanZhu"
			}
		},
		methods:{
			TarData(item){
				//设置id,来显示选中那个标签,显示下划线
				this.tabIndex = item.id;
				//显示标签对应的组件内容
				this.currentTabComponent = item.id
			}
		}
	}
</script>

<style>
</style>

  • 6
    点赞
  • 37
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 9
    评论
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

tengyuxin

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

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

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

打赏作者

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

抵扣说明:

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

余额充值