uniapp中三种tab切换实战案例

目录

一、比较常见的普通tab选项卡

二、使用scroll-view滑动

 三、scroll-view滑动 + swiper轮播联动


一、比较常见的普通tab选项卡

代码块部分

<template>
	<view class="content">
		<view class="hand">
			<view class="dataInfo">
				<view class="dataList" @click="navIndex = index" v-for="(item,index) in dataInfo" :key='item.titleId'>
					<text :class="navIndex==index?'activite':''">{{item.title}}</text>
				</view>
			</view>
		</view>
		<!-- 内容切换开始 -->
		<view class="main">
			<view class="htmlContent" v-if="navIndex==0">
				<text>html</text>
			</view>
			<view class="jsContent" v-if="navIndex==1">
				<text>javascript</text>
			</view>
			<view class="cssContent" v-if="navIndex==2">
				<text>css</text>
			</view>
			<view class="vueContent" v-if="navIndex==3">
				<text>vue</text>
			</view>
		</view>
		<!-- 内容切换结束 -->
	</view>

</template>

<script>
	export default {
		data() {
			return {
				navIndex: 0,
				dataInfo: [{
						title: 'HTML',
						titleId: 1
					},
					{
						title: 'JavaScript',
						titleId: 2
					},
					{
						title: 'CSS',
						titleId: 3
					},
					{
						title: 'VUE',
						titleId: 4
					}
				]
			}
		}
	}
</script>

<style>
	.content {
		padding: 20rpx;
	}

	.hand .dataInfo {
		display: flex;
		justify-content: space-between;
		text-align: center;
	}

	.hand .dataInfo .dataList {
		flex: 1;
		border-right: 1px solid #bcbcbc;
		border-top: 1px solid #bcbcbc;
	}

	.hand .dataInfo .dataList:first-child {
		border-left: 1px solid #bcbcbc;
	}

	.hand .dataInfo .dataList text {
		font-size: 30rpx;
		color: grey;
		background-color: #e6e6e6;
		display: block;
		line-height: 70rpx;
	}

	.hand .dataInfo .dataList text.activite {
		background-color: grey;
		color: #fff;
	}

	.main {
		border: 1px solid #bcbcbc;
		height: 200px;
	}

	.main .htmlContent,
	.cssContent,
	.jsContent,
	.vueContent text {
		font-size: 40rpx;
		color: #0F254E;
		display: block;
		padding: 20rpx;
	}
</style>

二、使用scroll-view滑动

代码块部分

<template>
	<view class="content">
		<view class="hand">
			<scroll-view scroll-x="true" style="white-space: nowrap;">
				<view class="dataInfo">
					<view class="dataList" v-for="(item,index) in dataInfo" :key='item.titleId' :id="item.titleId"
						@click="navIndex = index">
						<text :class="navIndex==index?'activite':''">{{item.title}}</text>
					</view>
				</view>
			</scroll-view>
		</view>
		<!-- 内容切换开始 -->
		<view class="main">
			<view class="htmlContent" v-if="navIndex==0">
				<text>html</text>
			</view>
			<view class="jsContent" v-if="navIndex==1">
				<text>javascript</text>
			</view>
			<view class="cssContent" v-if="navIndex==2">
				<text>css</text>
			</view>
			<view class="vueContent" v-if="navIndex==3">
				<text>vue</text>
			</view>
			<view class="reactContent" v-if="navIndex==4">
				<text>react</text>
			</view>
		</view>
		<!-- 内容切换结束 -->
	</view>

</template>

<script>
	export default {
		data() {
			return {
				navIndex: 0,
				dataInfo: [{
						title: 'HTML',
						titleId: 'html'
					},
					{
						title: 'JavaScript',
						titleId: 'js'
					},
					{
						title: 'CSS',
						titleId: 'css'
					},
					{
						title: 'VUE',
						titleId: 'vue'
					},
					{
						title: 'React ',
						titleId: 'react '
					}
				]
			}
		}
	}
</script>

<style>
	.content {
		padding: 20rpx;
	}

	.hand .dataInfo {
		display: flex;
		text-align: center;
		width: 100%;
	}

	.hand .dataInfo .dataList {
		border-right: 1px solid #bcbcbc;
		border-top: 1px solid #bcbcbc;
	}

	.hand .dataInfo .dataList:first-child {
		border-left: 1px solid #bcbcbc;
	}

	.hand .dataInfo .dataList text {
		font-size: 30rpx;
		color: grey;
		background-color: #e6e6e6;
		display: block;
		line-height: 70rpx;
		width: 200rpx;
	}

	.hand .dataInfo .dataList text.activite {
		background-color: grey;
		color: #fff;
	}

	.main {
		border: 1px solid #bcbcbc;
		height: 200px;
	}

	.main .htmlContent,
	.cssContent,
	.jsContent,
	.reactContent,
	.vueContent text {
		font-size: 40rpx;
		color: #0F254E;
		display: block;
		padding: 20rpx;
	}
</style>

 三、scroll-view滑动 + swiper轮播联动

代码块部分

<template>
	<view class="content">
		<view class="hand">
			<scroll-view scroll-x="true" style="white-space: nowrap;">
				<view class="dataInfo">
					<view class="dataList" v-for="(item,index) in dataInfo" :key='item.titleId' :id="item.titleId"
						@click="navIndex = index">
						<text :class="navIndex==index?'activite':''">{{item.title}}</text>
					</view>
				</view>
			</scroll-view>
		</view>
		<!-- 内容切换开始 -->
		<view class="main">
			<swiper :current="navIndex" @change="tabChange">
				<swiper-item>
					<view class="htmlContent" v-if="navIndex==0">
						<text>html</text>
					</view>
				</swiper-item>
				<swiper-item>
					<view class="jsContent" v-if="navIndex==1">
						<text>javascript</text>
					</view>
				</swiper-item>
				<swiper-item>
					<view class="cssContent" v-if="navIndex==2">
						<text>css</text>
					</view>
				</swiper-item>
				<swiper-item>
					<view class="vueContent" v-if="navIndex==3">
						<text>vue</text>
					</view>
				</swiper-item>
				<swiper-item>
					<view class="reactContent" v-if="navIndex==4">
						<text>react</text>
					</view>
				</swiper-item>
			</swiper>
		</view>
		<!-- 内容切换结束 -->
	</view>

</template>

<script>
	export default {
		data() {
			return {
				navIndex: 0,
				dataInfo: [{
						title: 'HTML',
						titleId: 'html'
					},
					{
						title: 'JavaScript',
						titleId: 'js'
					},
					{
						title: 'CSS',
						titleId: 'css'
					},
					{
						title: 'VUE',
						titleId: 'vue'
					},
					{
						title: 'React ',
						titleId: 'react '
					}
				]
			}
		},
		methods: {
			tabChange(e) {
				this.navIndex = e.detail.current
			},
		}
	}
</script>

<style>
	.content {
		padding: 20rpx;
	}

	.hand .dataInfo {
		display: flex;
		text-align: center;
		width: 100%;
	}

	.hand .dataInfo .dataList {
		border-right: 1px solid #bcbcbc;
		border-top: 1px solid #bcbcbc;
	}

	.hand .dataInfo .dataList:first-child {
		border-left: 1px solid #bcbcbc;
	}

	.hand .dataInfo .dataList text {
		font-size: 30rpx;
		color: grey;
		background-color: #e6e6e6;
		display: block;
		line-height: 70rpx;
		width: 200rpx;
	}

	.hand .dataInfo .dataList text.activite {
		background-color: grey;
		color: #fff;
	}

	.main {
		border: 1px solid #bcbcbc;
		height: 200px;
	}

	.main .htmlContent,
	.cssContent,
	.jsContent,
	.reactContent,
	.vueContent text {
		font-size: 40rpx;
		color: #0F254E;
		display: block;
		padding: 20rpx;
	}
</style>
  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值