uniapp 顶部选项卡 TopTabBar
在官网tabBar那里,只能自己手动实现效果图如下1. 项目结构+ home+ mine+ TopBar+ TopBarComponent+ guanzhu.vue+ tuijian.vue2. 实现TopTab的静态UI界面(1)顶部选项卡的布局 --- 当前为TopBar页面<template><!-- 顶部选项卡 --><view class="tabs">
·
在官网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>
更多推荐
已为社区贡献27条内容
所有评论(0)