uni-app 滚动到指定位置

方法1:使用标签,可以将页面横向(或纵向)滚动到指定位置

无法滚动 将代码放在setTimeout,nextTick里执行

javascript 复制代码
<!-- 左边 -->
			<scroll-view show-scrollbar="false" scroll-y="true" class="left-box"
				:scroll-top="scrollLeftTop" scroll-with-animation="true">
				<view class="scroll-view-item" v-for="(item,index) in servicesLeftList" :key="item.id"
					:id="'scroll' + activeLeftTab" :class="{'active':activeLeftTab==item.id}">
					{{item.name}}
				</view>
			</scroll-view>
javascript 复制代码
data(){
return {
servicesLeftList:[],
scrollLeftTop:0,,//滚动位置
activeLeftTab:"" //选中的样式
}
}

getData() {
	//接口
	getServicesTree().then(res => {
		this.servicesLeftList= res.data
		res.data.forEach((item, ind) => {
				setTimeout(()=>{
					uni.createSelectorQuery().in(this).select('#scroll' + item.id)
						.boundingClientRect(res => {
							scrollLeftTop.value = 65 * ind; // 设置滚动条距离左侧的距离    				        
						}).exec()
				},100)
		})
	})
}

方法二 使用uni.pageScrollTo 使页面纵向滚到到指定位置

建议设置height为auto :height:auto

javascript 复制代码
<view class="left-box" >
				<view class="scroll-view-item" v-for="(item,index) in servicesLeftList" :key="item.id"
					 :class="{'active':activeLeftTab==item.id}">
					{{item.name}}
				</view>
			</view>
//方法
uni.pageScrollTo({
	scrollTop: 0,
	duration: 500
});

方法三 用scroll-view描点

javascript 复制代码
 //左边 :scroll-top="scrollLeftTop"
<scroll-view scroll-y="true" class="left-box" scroll-with-animation="true"
	:scroll-into-view="tracingLeftPoint">
	<view class="scroll-view-item" v-for="(item,index) in servicesLeftList" :key="item.id"
		:id="'scroll' + item.id" :class="{'active':activeLeftTab==item.id}">
		{{item.name}}
	</view>
</scroll-view>

//方法

data(){
	retrun {
		tracingLeftPoint:"",//描点id
	}
}


	getData() {
	//接口
		getServicesTree().then(res => {
			this.servicesLeftList= res.data
			res.data.forEach((item, ind) => {
					setTimeout(()=>{
						this.tracingLeftPoint= 'scroll' + item.id
					},200)
			})
		})
	}
相关推荐
FAIRY_STARS6 小时前
记录uni-app好用的select组件
uni-app
FAIRY_STARS6 小时前
关于uni-app pages.json配置详解
uni-app
FliPPeDround7 小时前
🚀 定义即路由:definePage宏如何让uni-app路由配置原地起飞?
前端·vue.js·uni-app
iOS阿玮8 小时前
苹果审核被拒4.8.0条款,快速过审通关指南。
uni-app·app·apple
Ratten12 小时前
uniapp的H5 在 UC 浏览器中返回上一页失效的解决方案
uni-app
Ratten12 小时前
uniapp 的 H5和微信小程序上传 base64 图片
uni-app
Ratten12 小时前
使用 uniapp 扩展组件 uni-file-picker 实现视频和图片都可以预览展示
uni-app
Ratten12 小时前
在 HBuilderX 中使用 tailwindcss
uni-app
江湖行骗老中医1 天前
uniapp 引入使用u-view 完整步骤,u-view 样式不生效
uni-app
雪芽蓝域zzs1 天前
uniapp 页面favicon.ico文件不存在提示404问题解决
uni-app