uniapp实现tabs标签页滑动,使用scroll-view 和swiper

<template>
	<view class="body-view">
		<!-- 使用scroll-view实现tabs滑动切换 -->
		<scroll-view class="top-menu-view" scroll-x="true" :scroll-into-view="tabCurrent">
			<view class="menu-topic-view" v-for="item in tabs" :id="'tabNum'+item.id" :key="(item.id - 1)"
				@click="swichMenu(item.id - 1)">
				<view :class="currentTab==(item.id - 1) ? 'menu-topic-act' : 'menu-topic'">
					<text class="menu-topic-text">{{item.name}}</text>
					<view class="menu-topic-bottom">
						<view class="menu-topic-bottom-color"></view>
					</view>
				</view>
			</view>
		</scroll-view>
		<!-- 内容 -->
		<swiper class="swiper-box-list" :current="currentTab" @change="swiperChange">
			<swiper-item class="swiper-topic-list" v-for="item in swiperDateList" :key="item.id">
				<view class="swiper-item">
					{{item.content}}
				</view>
			</swiper-item>
		</swiper>
	</view>
</template>
<script>
	export default {
		data() {
			return {
				tabs: [{
						id: 1,
						name: '推荐'
					},
					{
						id: 2,
						name: '交通交通'
					},
					{
						id: 3,
						name: '住房'
					},
					{
						id: 4,
						name: '社会保障'
					},
					{
						id: 5,
						name: '民生热点'
					},
					{
						id: 6,
						name: '即日头条'
					},
					{
						id: 7,
						name: '新闻联播'
					},
				],
				currentTab: 0,
				tabCurrent: 'tabNum1',
				// Tab切换内容
				swiperDateList: [{
						id: 1,
						content: '推荐'
					},
					{
						id: 2,
						content: '交通交通'
					},
					{
						id: 3,
						content: '住房'
					},
					{
						id: 4,
						content: '社会保障'
					},
					{
						id: 5,
						content: '民生热点'
					},
					{
						id: 6,
						content: '即日头条'
					},
					{
						id: 7,
						content: '新闻联播'
					},
				],
			}
		},
		onLoad() {},
		methods: {
			swichMenu(id) {
				this.currentTab = id
				console.log(11, id)
				this.tabCurrent = 'tabNum' + id
			},
			swiperChange(e) {
				console.log(22, e.detail.current)
				let index = e.detail.current
				this.swichMenu(index)
			}
		}
	}
</script>
<style scoped lang="scss">
	.body-view {
		height: 100vh;
		width: 100%;
		display: flex;
		flex: 1;
		flex-direction: column;
		overflow: hidden;
		align-items: flex-start;
		justify-content: center;
	}

	.top-menu-view {
		display: flex;
		position: fixed;
		top: 0;
		left: 0;
		white-space: nowrap;
		width: 100%;
		background-color: #FFFFFF;
		height: 86rpx;
		line-height: 86rpx;
		border-top: 1rpx solid #d8dbe6;
		border-bottom: 1rpx solid #d8dbe6;

		.menu-topic-view {
			display: inline-block;
			white-space: nowrap;
			height: 86rpx;
			position: relative;

			.menu-topic-text {
				font-size: 30rpx;
				color: #303133;
				padding: 10rpx 40rpx;
			}

			// .menu-topic-act {
			// 	margin-left: 30upx;
			// 	margin-right: 10upx;
			// 	position: relative;
			// 	height: 100%;
			// 	display: flex;
			// 	align-items: center;
			// 	justify-content: center;
			// }
			.menu-topic-bottom {
				position: absolute;
				bottom: 0;
				width: 100%;

				.menu-topic-bottom-color {
					width: 40rpx;
					height: 4rpx;
				}
			}

			.menu-topic-act .menu-topic-text {
				font-weight: 700;
				color: #3d7eff;
			}

			.menu-topic-act .menu-topic-bottom {
				display: flex;
				justify-content: center;
			}

			.menu-topic-act .menu-topic-bottom-color {
				background: #3d7eff;
			}
		}
	}

	.swiper-box-list {
		width: 100%;
		padding-top: 100rpx;
		flex: 1;
		background-color: #FFFFFF;

		.swiper-topic-list {
			width: 100%;
		}
	}
</style>

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这个问题可能需要更详细的描述和代码示例才能够理解你的问题。不过,根据你的描述,我猜测你可能是想实现一个带有 `u-tabs-swiper` 和 `swiper` 组件的面,当用户左右滑动面时,`u-tabs-swiper` 组件会自动切换到对应的选项卡。 如果这是你的需求,你可以尝试在 `swiper` 组件上监听滑动事件,并在事件处理程序中手动调用 `u-tabs-swiper` 组件的 `setActiveTab` 方法来切换选项卡。具体代码示例如下: ```html <template> <view> <!-- u-tabs-swiper 组件 --> <u-tabs-swiper :tabs="tabs" ref="tabsSwiper" @tab-click="handleTabClick" /> <!-- swiper 组件 --> <swiper @change="handleSwiperChange"> <swiper-item v-for="(item, index) in tabs" :key="index"> <!-- 面内容 --> </swiper-item> </swiper> </view> </template> <script> export default { data() { return { tabs: ['选项卡1', '选项卡2', '选项卡3'], activeTab: 0, }; }, methods: { // u-tabs-swiper 组件的选项卡点击事件处理程序 handleTabClick(tab, index) { this.activeTab = index; // 切换到对应的 swiper 面 this.$refs.swiper.swiper.slideTo(index); }, // swiper 组件的面切换事件处理程序 handleSwiperChange() { const activeIndex = this.$refs.swiper.swiper.activeIndex; if (activeIndex !== this.activeTab) { // 切换到对应的选项卡 this.$refs.tabsSwiper.setActiveTab(activeIndex); this.activeTab = activeIndex; } }, }, }; </script> ``` 在上面的代码中,我们在 `swiper` 组件上监听了 `change` 事件,并在事件处理程序中手动调用 `u-tabs-swiper` 组件的 `setActiveTab` 方法来切换选项卡。同时,在 `u-tabs-swiper` 组件上监听了 `tab-click` 事件,当用户点击选项卡时,我们也需要手动调用 `swiper` 组件的 `slideTo` 方法来切换面。 希望这个代码示例能够解决你的问题。如果还有其他问题,欢迎继续提问!

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值