scorll view 实现点击元素,元素居中显示

这个方法是在百度借鉴的,

原文章地址:https://www.cnblogs.com/langxiyu/p/13159055.html

原文章中有三种方法(居左、居左留一、居中),我在这里只记录一个居中显示的方法

<template>
	<view class="home">
	    <scroll-view class="content-scroll" scroll-x @scroll="scroll" :scroll-left="scrollLeft" scroll-with-animation style="white-space: nowrap;height: 80rpx;">
			<view class="item scroll-item" style="height:80rpx;display: inline-block;" v-for="(item, index) in navList" :key="index">
				<view class="flex flex-column align-center " @click="changeItem(index)">
					<view class="thname" :class="currentIndex == index ? 'act-nav':''">{{ item.name }}</view>
				</view>
			</view>
		</scroll-view>
	</view>
</template>
<script>
	export default {
		data() {
			return {
				currentIndex: 0,
				navList: [{
						name: '精选'
					},
					{
						name: '语言课程'
					},
					{
						name: '名师录播课'
					},
					{
						name: '优选直播课'
					},
					{
						name: '定制化培训'
					},
					{
						name: '夏冬令营'
					},
					{
						name: '少儿多语种'
					},
					{
						name: '测试中心'
					},
					{
						name: '客户端下载'
					},
					{
						name: '关于我们'
					}
				],
				contentScrollW: 0, // 导航区宽度
				curIndex: 0, // 当前选中
				scrollLeft: 0, // 横向滚动条位置
			}
		},
        mounted() {
			// 获取标题区域宽度,和每个子元素节点的宽度
			this.getScrollW()
		},
		methods: {
			// 获取标题区域宽度,和每个子元素节点的宽度以及元素距离左边栏的距离
			getScrollW() {
				const query = uni.createSelectorQuery().in(this);

				query.select('.content-scroll').boundingClientRect(data => {
					// 拿到 scroll-view 组件宽度
					this.contentScrollW = data.width
				}).exec();

				query.selectAll('.scroll-item').boundingClientRect(data => {
					let dataLen = data.length;
					for (let i = 0; i < dataLen; i++) {
						//  scroll-view 子元素组件距离左边栏的距离
						this.navList[i].left = data[i].left;
						//  scroll-view 子元素组件宽度
						this.navList[i].width = data[i].width
					}
				}).exec()
			},
			changeItem(index) {
				this.currentIndex = index
				console.log(this.currentIndex)
				this.scrollLeft = this.navList[index].left - this.contentScrollW / 2 + this.navList[index].width / 2;
			},
		}
    }
</script>

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
实现 scroll-view 的无限滚动并且点击居中可以通过以下步骤: 1. 在 scroll-view 中创建一个宽度为所有子元素宽度之和的容器,在容器中使用 flex 布局来排列子元素。 2. 复制一份子元素列表,将其排列在原列表的后面。 3. 监听 scroll-view 的 scroll 事件,当滚动到最后一个子元素之后,将容器中的子元素列表重置为原列表,然后重新排列子元素。 4. 监听子元素点击事件,获取被点击的子元素的索引,计算出容器需要滚动的距离,然后使用 scroll-view 的 scrollIntoView 方法将被点击的子元素滚动居中位置。 以下是一个简单的示例代码: ```html <scroll-view scroll-x style="white-space: nowrap;" bindscroll="onScroll"> <view wx:for="{{items}}" wx:key="{{index}}" data-index="{{index}}" bindtap="onClick">{{item}}</view> </scroll-view> ``` ```javascript Page({ data: { items: ['A', 'B', 'C', 'D', 'E', 'F', 'G'], containerWidth: 0, itemWidth: 0, currentIndex: 0 }, onLoad: function () { wx.createSelectorQuery().in(this).select('.container').boundingClientRect(res => { this.setData({ containerWidth: res.width }) }).exec() wx.createSelectorQuery().in(this).select('.item').boundingClientRect(res => { this.setData({ itemWidth: res.width }) }).exec() }, onScroll: function (event) { const { scrollLeft } = event.detail const { containerWidth, itemWidth, items } = this.data const maxScrollLeft = (items.length * itemWidth) - containerWidth if (scrollLeft >= maxScrollLeft) { this.setData({ items: [...items, ...items] }) } }, onClick: function (event) { const { index } = event.currentTarget.dataset this.setData({ currentIndex: index }) wx.createSelectorQuery().in(this).select(`.item[data-index="${index}"]`).boundingClientRect(res => { const { containerWidth } = this.data const itemLeft = res.left const itemWidth = res.width const itemCenter = itemLeft + (itemWidth / 2) const scrollLeft = itemCenter - (containerWidth / 2) wx.pageScrollTo({ scrollLeft, duration: 300 }) }).exec() } }) ``` 在这个示例中,我们先获取容器和子元素的宽度,然后在 scroll 事件中判断是否需要重置并重新排列子元素。在点击事件中,我们获取被点击的子元素的位置,然后计算出需要滚动的距离,最后使用 pageScrollTo 方法将 scroll-view 滚动居中位置。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值