uniapp 锚点跳转

页面1:

跳转页面:

html 复制代码
<template>
	<view class="content">
		<view class="test test1">1111111111111</view>
        <view id="box2222" class="test test2">22222222222222</view>
	</view>
</template>

<script setup>
	import { ref, onMounted } from 'vue'
	
	onMounted(() => {
			// 获取路由参数
            //所有路由
			const pages = getCurrentPages()
            //当前跳转的路由(跳转页)
			const currentPage = pages[pages.length - 1]
            //需要跳转的位置
			const scrollTo = currentPage.options.scrollTo
			
			if (scrollTo) {
				// 延迟执行滚动,确保DOM已经渲染完成
				setTimeout(() => {
					// 使用uni-app的选择器API,兼容各平台
					const query = uni.createSelectorQuery().in(currentPage)
					query.select('#' + scrollTo).boundingClientRect()
					query.exec((res) => {
						if (res && res[0]) {
							// 获取元素位置并滚动
							const { top } = res[0]
							uni.pageScrollTo({
								scrollTop: top,
								duration: 300
							})
						}
					})
				}, 100)
			}
		})
</script>

<style lang="scss" scoped>
	.test{
        width: 100%;
        height:1200rpx ;
        background: green;
    }
</style>
相关推荐
奔跑的web.11 小时前
UniApp 路由导航守
前端·javascript·uni-app
特立独行的猫a11 小时前
主要跨端开发框架对比:Flutter、RN、KMP、Uniapp、Cordova,谁是未来主流?
flutter·uni-app·uniapp·rn·kmp·kuikly
万物得其道者成1 天前
UniApp 多端滑块验证码插件 zxj-slide-verify 实用指南
uni-app
蓝帆傲亦1 天前
支付宝小程序性能暴增秘籍:UniApp项目极限优化全攻略
小程序·uni-app
2501_916008891 天前
深入解析iOS机审4.3原理与混淆实战方法
android·java·开发语言·ios·小程序·uni-app·iphone
QT.qtqtqtqtqt1 天前
uni-app小程序前端开发笔记(更新中)
前端·笔记·小程序·uni-app
喵喵虫2 天前
uniapp修改封装组件失败 styleIsolation
uni-app
游戏开发爱好者82 天前
日常开发与测试的 App 测试方法、查看设备状态、实时日志、应用数据
android·ios·小程序·https·uni-app·iphone·webview
2501_915106322 天前
app 上架过程,安装包准备、证书与描述文件管理、安装测试、上传
android·ios·小程序·https·uni-app·iphone·webview
2501_915106322 天前
使用 Sniffmaster TCP 抓包和 Wireshark 网络分析
网络协议·tcp/ip·ios·小程序·uni-app·wireshark·iphone