uniapp:实现tabs(scroll-view)点击左右滑动

scroll-view

scroll-x					允许横向滚动
scroll-top					设置横向滚动条位置
scroll-with-animation		在设置滚动条位置时使用动画过渡

1、注意需要在data中给.box一个默认的宽度,因为css设置的是display: flex;
2、加上默认宽度后onLoad中可以根据数据tabTopList数据个数,计算出每个itemWidth的宽度,总宽度totalWidth

<view class="tabTop">
	<scroll-view 
		scroll-x="true" 
		:scroll-left="scrollLeft" 
		:scroll-with-animation="true">
		<view class="box" :style="{'width':totalWidth+'px'}">
			<view 
				class="tabTopItem" 
				v-for="(item,index) in 10" 
				:key="index" 
				:class="{'ac':tabTopCurrent == index}" @click="tabTopChange(index)">
				测试{{item.name}}仓
				<image src="../../static/icon/87.png" mode="" class="tips" v-if="index == 0"></image>
			</view>
		</view>
	</scroll-view>
</view>
<script>
	export default {
		data() {
			return {
				itemWidth:0,//每个item的宽度
				totalWidth:10000,//设置一个默认总宽度
				scrollLeft:0,//滑动距离
				tabTopList: [],
				tabTopCurrent: 0,
			}
		}
		onLoad() {
			/*
				计算总宽度和每个item的宽度
				item宽度 = 自身宽度 + 8px右边距
				总宽度 = item个数 * 每个item的宽度
			*/
			this.$nextTick(()=>{
				let dom = uni.createSelectorQuery().select(".tabTopItem");
				dom.boundingClientRect((data)=> {
					let num = this.tabTopList.length;
					this.itemWidth = data.width+8;
					this.totalWidth = num*this.itemWidth;
				}).exec()
			})
		},
		methods: {
			// 点击tab切换高亮,并进行滑动,(index-1)是为了点击项显示在第二栏的位置
			tabTopChange(index){
				this.tabTopCurrent = index;
				this.scrollLeft = this.itemWidth*(index-1);
			},
		}
	}
.tabTop{
	width: 702rpx;
	background: #FFFFFF;
	height: 90rpx;
	margin: auto;
	/* 隐藏滚动条样式 */
	::-webkit-scrollbar {
		width: 0;
		height: 0;
	}
	.box{
		display: flex;
		align-items: center;
		.tabTopItem{
			text-align: center;
			width: 168rpx;
			height: 90rpx;
			line-height: 90rpx;
			background: #F6F7FB;
			font-size: 28rpx;
			color: #8D9199;
			margin-right: 8px;
			position: relative;
			.tips{
				position: absolute;
				right: 0;
				top: 0;
				width: 72rpx;
				height: 28rpx;
			}
		}
		.ac{
			font-size: 30rpx;
			background: #fff;
			color: #17181B;
		}
	}
}

在这里插入图片描述

  • 2
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
可以参考以下代码: ```html <template> <view> <scroll-view class="scroll-view" scroll-x="true"> <view class="tab-item" :class="{active: activeIndex === index}" v-for="(item, index) in tabs" :key="index" @tap="handleTabClick(index)">{{item}}</view> </scroll-view> <view class="list-wrap"> <view class="list" v-show="activeIndex === 0"> <view v-for="(item, index) in list1" :key="index">{{item}}</view> </view> <view class="list" v-show="activeIndex === 1"> <view v-for="(item, index) in list2" :key="index">{{item}}</view> </view> <view class="list" v-show="activeIndex === 2"> <view v-for="(item, index) in list3" :key="index">{{item}}</view> </view> </view> </view> </template> <script> export default { data() { return { tabs: ['列表1', '列表2', '列表3'], activeIndex: 0, list1: ['列表1-1', '列表1-2', '列表1-3'], list2: ['列表2-1', '列表2-2', '列表2-3'], list3: ['列表3-1', '列表3-2', '列表3-3'] } }, methods: { handleTabClick(index) { this.activeIndex = index } } } </script> <style> .scroll-view { display: flex; white-space: nowrap; } .tab-item { padding: 20rpx 30rpx; font-size: 32rpx; color: #999; } .tab-item.active { color: #333; border-bottom: 4rpx solid #333; } .list-wrap { height: calc(100vh - 200rpx); overflow: auto; } .list { padding: 20rpx; font-size: 28rpx; line-height: 1.5; color: #333; } </style> ``` 其中,我们使用了一个 `scroll-view` 实现了多个横向滚动的标签,并用 `v-for` 渲染了多个列表,通过 `v-show` 根据 `activeIndex` 的值来显示或隐藏对应的列表。最后,我们使用了 `@tap` 监听标签的点击事件,并将 `activeIndex` 的值更新为点击的标签的索引,从而实现了多列表切换。
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值