前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >uni-app小程序开发-滚动到指定位置的实现方式

uni-app小程序开发-滚动到指定位置的实现方式

作者头像
码客说
发布2024-07-23 10:52:23
430
发布2024-07-23 10:52:23
举报
文章被收录于专栏:码客

使用scroll-view标签

scroll-top

页面

代码语言:javascript
复制
<scroll-view show-scrollbar="false" scroll-y="true" :scroll-top="scrollTop" scroll-with-animation="true">
    <view class="scroll-view-item" v-for="(item,index) in servicesLeftList" :key="item.id">
        {{item.name}}
    </view>
</scroll-view>

数据

代码语言:javascript
复制
data(){
    return {
        scrollTop:0,//滚动位置
    }
}

方法

代码语言:javascript
复制
this.scrollTop = 300;

scroll-into-view

页面

代码语言:javascript
复制
<scroll-view scroll-y="true" scroll-with-animation="true" :scroll-into-view="targetViewId">
	<view  v-for="(item,index) in servicesLeftList" :key="item.id" :id="'scroll' + item.id">
		{{item.name}}
	</view>
</scroll-view>

数据

代码语言:javascript
复制
data(){
	retrun {
		targetViewId:"",//描点id
	}
}

方法

代码语言:javascript
复制
this.targetViewId= 'scroll' + item.id;

使用uni.pageScrollTo

代码语言:javascript
复制
<view class="left-box" >
    <view class="scroll-view-item" v-for="(item,index) in servicesLeftList" :key="item.id"
         :class="{'active':activeLeftTab==item.id}" :id="textarea"+index>
        {{item.name}}
    </view>
</view>

JS

代码语言:javascript
复制
uni.pageScrollTo({
	scrollTop: 0,
	duration: 500,
	selector: '#textarea5' //指定位置
});

滚动到底部

页面

代码语言:javascript
复制
<scroll-view :style="{height:scrollViewHeight+'px'}" :scroll-y="true" :scroll-top="scrollTop" :scroll-with-animation="true">
    <view id="scroll-view-content">
        <block v-for="(item,index) in images" :key="index">
            <image class="item" :src="item.src" mode="aspectFill"></image>
        </block>
    </view>
</scroll-view>

JS

代码语言:javascript
复制
export default {
	data() {
		return {
			images:[],
			scrollTop:0,//滚动条位置
			scrollViewHeight:300,//滚动视图的高度
		};
	},
	methods:{
		scrollToBottom(){
			this.$nextTick(()=>{
				uni.createSelectorQuery().select('#scroll-view-content').boundingClientRect((rect)=>{
					let top = rect.height-this.scrollViewHeight;
					if(top>0){
						this.scrollTop=top;
					}
				}).exec()
			})
		}
	}
}

标签与滚动区联动

代码语言:javascript
复制
<template>
	<view>
		<scroll-view class="myscrollview" scroll-y @scroll="onScroll" :scroll-top="scrollTop" scroll-with-animation="true">
			<view v-for="(item, index) in items" :key="index" :id="'item-' + index" class="scroll_item">
				<text>{{ item }}</text>
			</view>
		</scroll-view>

		<view class="tag_outer">
			<view v-for="(item, index) in items" :key="index" class="tag_item">
				<text :style="{ color: selectedIndex === index ? 'blue' : 'black' }" @click="scrollTo(index)">
					{{ item }}
				</text>
			</view>
		</view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			items: ['标签1', '标签2', '标签3', '标签4', '标签5'],
			selectedIndex: 0,
			scrollTop: 0
		};
	},
	onReady() {},
	methods: {
		getRect(selector) {
			return new Promise((resolve) => {
				uni.createSelectorQuery()
					.select(selector)
					.boundingClientRect((rect) => {
						resolve(rect);
					})
					.exec();
			});
		},
		onScroll(event) {
			const { scrollTop, scrollHeight } = event.detail;
			this.updateSelectedIndex(scrollTop, scrollHeight);
		},

		async updateSelectedIndex(scrollTop, scrollHeight) {
			let items = this.items;
			let outerRect = await this.getRect('.myscrollview');
			if (scrollTop + outerRect.height >= scrollHeight - 10) {
				// 滚动到底部
				this.selectedIndex = items.length - 1;
			} else {
				// 未滚动到底部
				items.forEach(async (_, index) => {
					let rect = await this.getRect(`#item-${index}`);
					if (rect) {
						const top = rect.top + scrollTop;
						const bottom = rect.bottom + scrollTop;
						// 判断当前可见区域
						if (scrollTop >= top && scrollTop < bottom) {
							this.selectedIndex = index;
						}
						if (index == items.length - 1) {
							if (scrollTop + rect.height >= scrollHeight - 10) {
								this.selectedIndex = index;
							}
						}
					}
				});
			}
		},
		async scrollTo(index) {
			this.selectedIndex = index;
			let rect = await this.getRect(`#item-${index}`);
			if (rect) {
				this.scrollTop = rect.top + this.scrollTop;
			}
		}
	}
};
</script>

<style>
.myscrollview {
	height: 300px;
	background-color: #f0f0f0;
}

.scroll_item {
	height: 200px;
	background-color: aquamarine;
	margin-bottom: 20rpx;
}

.tag_outer {
	margin-top: 20rpx;
	display: flex;
	width: 100%;
}

.tag_item {
	margin-left: 10px;
	border: 1rpx solid #ddd;
	padding: 10rpx 10rpx;
}
</style>
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2024-07-22,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 使用scroll-view标签
    • scroll-top
      • scroll-into-view
      • 使用uni.pageScrollTo
      • 滚动到底部
      • 标签与滚动区联动
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档