title: uniapp小程序的锚点定位(将页面滚动到目标位置)
date: ‘2024-12-14 11:29:03’
updated: ‘2024-12-14 11:30:27’
tags: tags:
categories:
- 编程
- Uniapp
- 锚点定位
permalink: >-
/post/the-anchor-position-positioning-of-the-uniapp-applet-scroll-the-page-to-the-target-location-20iwcc.html
comments: true
toc: true—
小程序中,a页面跳转到b页面,跳转后滚动定位到b页面的特定位置。
- 1.uni.pageScrollTo传递一个scrollTop参数可以滚动到特定位置。
- 2.可以通过 uni.createSelectorQuery()等获取定位元素的位置信息。
- 3.uni.getSystemInfoSync()获取设备的导航栏和状态栏高度。
- 4.注意:scrollTop参数的值是2的top的值减去3的导航栏的高度和状态栏的高度。
- 5.最后,需要注意使用定时器,等待页面渲染完毕,开始滚动。
直接上代码:下面是b页面的模板代码和js代码。
1 2 3 4 5 6 7 8 9
| <template> <div class="container"> <div class="section a"></div> <div class="section b"</div> <div class="section c"></div> <div class="section d"></div> </div> </template>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| onLoad(opt){ const pos = opt && opt.pos || ''; const res = uni.getSystemInfoSync(); uni.createSelectorQuery().select(`.${pos}`).boundingClientRect(data => { setTimeout(()=>{ uni.pageScrollTo({ scrollTop: data.top - res.navigationBarHeight - res.statusBarHeight }) }, 300) }).exec(); }
|
Author:
Shenhuanjie
Permalink:
https://shenhuanjie.github.io/2026/05/09/d4b2291fd1ed/
License:
Copyright (c) 2024 CC-BY-NC-4.0 LICENSE
Slogan:
Do you believe in DESTINY?
💬 互动讨论
欢迎留下你的见解、疑问或心得,精选评论有机会获得积分奖励哦!
使用 GitHub 账号登录评论 · 了解 Utterances
发现错误或有建议?提交反馈