uniapp scroll-view y轴点击item实现滚动效果

<view class="goods-box">
    <scroll-view scroll-y="true" class="classify" :scroll-top="scrollTop" scroll-with-animation>
	    <view class="classify-item" :class="categoryIndex===index?'active':''" v-for="(item,index) in categoryList" :key="item.id" @tap="categoryClick(item,index)">
            <image class="icon u-m-r-4" :src="item.image" mode="aspectFill" v-if="item.image"></image>{{item.name}}</view>
	</scroll-view>
</view>
data() {
	return {
		categoryList: [],
		categoryIndex: 0, // 当前选中的分类索引
		scrollTop: 0, // 用于控制 scroll-view 的滚动位置
		classifyRect: {},
	}
},
getGoodsCategoryList() {
				this.$http({
					url: 'api/web/getGoodsCategoryList',
				}).then(res => {
					this.categoryList = res.data.data; // 获取分类数据

					// 确保 DOM 已渲染完成,进行 DOM 查询
					this.$nextTick(() => {
						const query = uni.createSelectorQuery().in(this); // 在当前页面上下文中创建选择器查询
						query.select('.classify').boundingClientRect(); // 查询 scroll-view 的位置和尺寸
						query.selectAll('.classify-item').boundingClientRect(); // 获取分类项的位置
						query.exec(queryRes => {
							this.classifyRect = queryRes[0]
							// 将查询结果添加到 categoryList 中
							if (queryRes && queryRes[1]) {
								// 将 DOM 数据与 categoryList 合并
								this.categoryList.forEach((item, index) => {
									const rect = queryRes[1][index]; // 获取每个分类项的位置信息
									item.rect =
										rect; // 把分类项的位置数据添加到 categoryList 对应的 item 中
								});
							}
						});
					});
				});
			},
			categoryClick(item, index) {
				this.categoryIndex = index; // 更新当前选中的分类索引
				// 如果 item 不包含位置信息,动态查询该项的位置
				if (!item.rect.top || !item.rect.height) {
					const query = uni.createSelectorQuery().in(this);
					query.select('.classify-item').eq(index).boundingClientRect(); // 获取当前点击项的位置
					query.exec(res => {
						if (res && res[0]) {
							item.rect.top = res[0].top;
							item.rect.height = res[0].height;
							this.scrollToCategory(item); // 获取位置数据后,再调用滚动操作
						}
					});
				} else {
					// 如果 item 已经包含位置信息,直接调用滚动
					this.scrollToCategory(item);
				}
			},
			// 单独提取滚动操作逻辑
			scrollToCategory(item) {
					const scrollViewHeight = this.classifyRect.height;
					const scrollViewTop = this.classifyRect.top; // scroll-view 距离页面顶部的位置

					const itemTop = item.rect.top - scrollViewTop; // 分类项相对 scroll-view 的位置
					const itemHeight = item.rect.height; // 当前点击项的高度

					const targetScrollTop = itemTop - (scrollViewHeight / 2) + (itemHeight / 2);

					this.scrollTop = targetScrollTop; // 设置 scrollTop,滚动到目标位置
			}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

JOJORiny

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值