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>
相关推荐
codingWhat2 小时前
小程序里「嵌」H5:一套完整可落地的 WebView 集成方案
前端·uni-app·webview
小时前端1 天前
微信小程序选不了本地文件?用 web-view + H5 一招搞定
前端·微信小程序·uni-app
Mr_li2 天前
给 Vue 开发者的 uni-app 快速指南
vue.js·uni-app
anyup2 天前
🔥2026最推荐的跨平台方案:H5/小程序/App/鸿蒙,一套代码搞定
前端·uni-app·harmonyos
Mintopia3 天前
Vue3 项目如何迁移到 uni-app x:从纯 Web 到多端应用的系统指南
uni-app
Mintopia3 天前
uni-app x 发展前景技术分析:跨端统一的新阶段?
uni-app
不爱说话郭德纲4 天前
告别漫长的HbuilderX云打包排队!uni-app x 安卓本地打包保姆级教程(附白屏、包体积过大排坑指南)
android·前端·uni-app
HashTang5 天前
【AI 编程实战】第 12 篇:从 0 到 1 的回顾 - 项目总结与 AI 协作心得
前端·uni-app·ai编程
JunjunZ5 天前
uniapp 文件预览:从文件流到多格式预览的完整实现
前端·uni-app
郑州光合科技余经理6 天前
代码展示:PHP搭建海外版外卖系统源码解析
java·开发语言·前端·后端·系统架构·uni-app·php