微信小程序,点击tab,使当前tab出现的视野中 或者 滑动下面的swiper,使当前的tab出现在视野中(用的uni-app)(滑动的tab)

主要是 scroll-view 这个组件的使用,以及每一个tabItem一些属性的计算

<template>
	<view id="estimate-answerV2" class="estimate-answerV2">
		<view class="tab-nav">
			<scroll-view :enable-flex="true" scroll-with-animation :throttle="false" :scroll-left="scrollToLeft" scroll-x
			 @scroll="handleScroll">
				<view class="tab-list">
					<block v-for="(tab,index) in tabList" :key="tab.value">
						<view class="tab-item" @click.stop="tabClick(tab)" :class="currentTab.value == tab.value?'active':''">
							{{tab.name}}
						</view>
					</block>
				</view>
			</scroll-view>
		</view>
		<view class="tab-content">
			<swiper class="swiper-box" :current="currentSwiper" :duration="duration" @change="change">
				<block v-for="(swiper) in tabList" :key="swiper.value">
					<swiper-item>
						<view class="swiper-item">
							{{swiper.name}}
						</view>
					</swiper-item>
				</block>
			</swiper>
		</view>
	</view>
</template>
<script>
	export default {
		components: {
		},
		data() {
			return {
				tabList: [],
				tabItemInfo: [],
				currentSwiper:0,
				duration:500,
				currentTab: {
					name:'',
					value:'',
					index:''
				},
				scrollToLeft: 0,
				parentLeft: 0,
				parentWidth: 0,
				
			}
		},
		mounted() {
			this.getTabList()
		},
		methods: {
			//获取tabList
			getTabList(){
				this.tabList = [
					{
						name:"Ⅰ Use of English",
						value:0,
						index:0
					},
					{
						name:"Ⅱ Reading Comprehension",
						value:1,
						index:1
					},
					{
						name:"Ⅲ Writing",
						value:2,
						index:2
					},
				]
				this.$nextTick(() => {
					this.initScroll()
				})
			},
			change(e){
				this.currentSwiper = e.detail.current;
				this.currentTab = this.tabList[e.detail.current]
				this.scrollDom(e.detail.current);
			},
			/*获取辅助数据*/
			initScroll() {
				const query = uni.createSelectorQuery().in(this);
				query.select('.tab-list').fields({
					rect: true,
					size: true
				}, res => {
					this.parentLeft = res.left;
					this.parentWidth = res.width;
				});
				//获取每个tabitem的宽度以及距离左边的距离
				query.selectAll('.tab-item').fields({
					rect: true,
					size: true
				}, data => {
					data.forEach((item, index) => {
						this.tabItemInfo.push({
							width: item.width,
							left: item.left
						});
					});
				});
				query.exec();
			},
			handleScroll(e) {
				//this.scrollDom()
			},
			/*tab的点击事件*/
			tabClick(tab, position) {
				this.currentTab = tab
				this.currentSwiper = tab.value;
				this.scrollDom(tab.value);
			},
			/*
			使scroll滚动,显示当前tab
				@param {*} index 当前tab的下标
			*/
			scrollDom(index) {
				let info = this.tabItemInfo[index];
				let offsetLeft = info.left - this.parentLeft;
				let scrollLeft = offsetLeft - (this.parentWidth - info.width) / 2;
				this.scrollToLeft = scrollLeft < 0 ? 0 : scrollLeft;
			}
		}
	}
</script>
<style lang="scss">
	page {
		height: 100%;
	}

	#estimate-answerV2 {
		height: 100%;

		.tab-nav {
			width: 100%;
			height: 72rpx;
			background: #fff;

			.tab-list {
				height: 100%;
				white-space: nowrap;

				.tab-item {
					display: inline-block;
					position: relative;
					padding: 0 18rpx;
					box-sizing: border-box;
					height: 100%;
					line-height: 72rpx;
					color: #bbb;
					transition: all linear .1s;

					&:after {
						content: "";
						position: absolute;
						bottom: rpx;
						left: 50%;
						bottom: 2rpx;
						width: 65rpx;
						height: 8rpx;
						transform: translateX(-50%);
						background: transparent;
						border-radius: 5rpx;
					}

					&.active {
						color: #222;
						font-weight: bold;

						&:after {
							background: linear-gradient(-23deg, #EC3D2F, #FC675B)
						}

					}
				}

			}
		}

		.tab-content {
			height: calc(100% - 72rpx);
			.swiper-box,.swiper-item{
				height: 100%;
			}
			.swiper-item{
				display: flex;
				justify-content: center;
				align-items: center;
			}
		}
	}
</style>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值