uni-app 利用自带swiper实现点击tab内容滑动,滑动内容tab切换(不使用插件)

本文介绍了一个基于uni-app的车辆选择组件实现方法,通过使用view和swiper组件结合v-for循环展示不同类型的车辆及其信息,并通过自定义方法实现选项卡切换和swiper滑动同步。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

结构

<view class="ui-car-select">
	<view class="ui-car-name-list">
		<view class="ui-car-name-item" v-for="(item,index) in carTypes" :class="{active:current==index}" :data-current="index" @tap="tabChange">{{item.cartype}}</view>
	</view>
	<view>
		<swiper class="ui-carinfos" @change="swiperChange" :current="current">
			<swiper-item class="ui-carinfo-item" v-for="(item,index) in carinfos">{{item.carinfo}}</swiper-item>
		</swiper>
	</view>
</view>

代码

data() {
			return {
				visible: false,
				current:0,
				carTypes:[{
					cartype:"小面包"
					},{
						cartype:"中面包"
					},{
						cartype:"小货车"
					},{
						cartype:"中货车"
				}],
				carinfos:[{
					carinfo:"小面包"
					},{
						carinfo:"中面包"
					},{
						carinfo:"小货车"
					},{
						carinfo:"中货车"
				}]
			}
		},
methods:{
	tabChange:function(e){
		var index = e.target.dataset.current || e.currentTarget.dataset.current;
		this.current=index;
	},
	swiperChange:function(e) {			
		var index=e.target.current || e.detail.current;
		this.current = index;
	}
}

记得给tab添加.active类名的样式。这里就不写了。

给tab的每一个加data-current,利用e.target.dataset.current赋值index。swiper利用自带current属性,利用e.target.current赋值index。

不知道会不会有兼容问题,或者其他的注意点。有知道的朋友欢迎留言。我也是第一次用,开始没看到这个费了老大劲了。

可以参考hello uniapp template模板里面的tabbar>tabbar.nvue。

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值