docs/api/page-scroll-to.md
::: sourceCode
GitCode: https://gitcode.com/dcloud/uni-api/tree/alpha/uni_modules/uni-pageScrollTo
GitHub: https://github.com/dcloudio/uni-api/tree/alpha/uni_modules/uni-pageScrollTo
:::
将页面滚动到目标位置
可以滚动到指定的scrollTop值处,也可以滚动到指定的目标元素处(通过css选择器selector), 仅支持一级 class。
本API滚动的是栈顶的页面。
app-uvue下,其实没有页面级滚动。但本API做了一定兼容,当页面的根元素为scroll-view时,本API也会滚动该scroll-view。详见
| Web | 微信小程序 | Android | iOS | HarmonyOS | HarmonyOS(Vapor) |
|---|---|---|---|---|---|
| 4.0 | 4.41 | 3.91 | 4.11 | 4.61 | 5.0 |
| 名称 | 类型 | 必填 | 默认值 | 兼容性 | 描述 |
|---|---|---|---|---|---|
| options | PageScrollToOptions | 是 | - | Web: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: - |
| 名称 | 类型 | 必备 | 默认值 | 兼容性 | 描述 |
|---|---|---|---|---|---|
| scrollTop | number | 否 | - | Web: -; 微信小程序: 4.41; Android: -; iOS: -; HarmonyOS: - | 滚动到页面的目标位置 |
| selector | string | 否 | - | Web: -; 微信小程序: 4.41; Android: -; iOS: -; HarmonyOS: - | 选择器 |
| offsetTop | number | 否 | - | Web: 4.0; 微信小程序: 4.41; Android: 3.91; iOS: 4.11; HarmonyOS: 4.61; HarmonyOS(Vapor): 5.0 | 偏移距离,可以滚动到 selector 加偏移距离的位置 |
| duration | number | 否 | - | Web: -; 微信小程序: 4.41; Android: -; iOS: -; HarmonyOS: - | 滚动动画的时长 |
| success | (result: PageScrollToSuccess) => void | 否 | - | Web: -; 微信小程序: 4.41; Android: -; iOS: -; HarmonyOS: - | 接口调用成功的回调函数 |
| fail | (result: PageScrollToFail) => void | 否 | - | Web: -; 微信小程序: 4.41; Android: -; iOS: -; HarmonyOS: - | 接口调用失败的回调函数 |
| complete | (result: PageScrollToComplete) => void | 否 | - | Web: -; 微信小程序: 4.41; Android: -; iOS: -; HarmonyOS: - | 接口调用结束的回调函数(调用成功、失败都会执行) |
| 名称 | 类型 | 必备 | 默认值 | 兼容性 | 描述 |
|---|---|---|---|---|---|
| errMsg | string | 是 | - | Web: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: - |
| 名称 | 类型 | 必备 | 默认值 | 兼容性 | 描述 |
|---|---|---|---|---|---|
| errCode | number | 是 | - | Web: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: - | 设置页面滚动错误码 |
| 名称 | 类型 | 必备 | 默认值 | 兼容性 | 描述 |
|---|---|---|---|---|---|
| errMsg | string | 是 | - | Web: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: - |
scrollTop 和 selector 必须指定其中一个属性,否者触发 fail 回调
| 类型 | 必备 |
|---|---|
| Promise<PageScrollToSuccess> | 否 |
| 名称 | 类型 | 必备 | 默认值 | 兼容性 | 描述 |
|---|---|---|---|---|---|
| errMsg | string | 是 | - | Web: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: - |
示例为hello uni-app x alpha分支,与最新HBuilderX Alpha版同步。与最新正式版同步的master分支示例另见 ::: preview https://hellouniappx.dcloud.net.cn/web/#/pages/API/page-scroll-to/page-scroll-to
appRedirect https://hellouniappx.dcloud.net.cn/appredirect.html?path=pages/API/page-scroll-to/page-scroll-to
示例
<template>
<!-- #ifdef APP -->
<scroll-view style="flex: 1" scroll-with-animation="true">
<!-- #endif -->
<view class="uni-padding-wrap">
<page-head :title="title"></page-head>
<button type="default" class="btn-scrollTo" @click="scrollTo">
scrollTo
</button>
<button type="default" class="btn-scrollToElement" @click="scrollToElement">
scrollToElement
</button>
<view class="uni-list" v-for="(_, index) in 10" :key="index">
<view class="uni-list-cell list-item">{{ index }}</view>
</view>
<view class="custom-element">scrollTo-custom-element</view>
<view class="uni-list" v-for="(_, index2) in 10" :key="index2">
<view class="uni-list-cell list-item">{{ index2 }}</view>
</view>
</view>
<!-- #ifdef APP -->
</scroll-view>
<!-- #endif -->
</template>
<script setup lang="uts">
const title = ref('pageScrollTo')
const scrollTo = () => {
uni.pageScrollTo({
scrollTop: 100,
duration: 300,
success: () => {
console.log('success')
},
})
}
const scrollToElement = () => {
uni.pageScrollTo({
selector: '.custom-element',
duration: 300,
success: () => {
console.log('success')
},
})
}
defineExpose({
scrollTo
})
</script>
<style>
.list-item {
height: 100px;
padding-left: 30px;
}
</style>
:::
selector 语法
selector类似于 CSS 的选择器,但仅支持下列语法。
.a-class.another-class.the-parent > .the-child.the-ancestor .the-descendant.the-ancestor >>> .the-descendant#a-node, .some-other-nodesscroll-with-animation="true",详见示例
uni.pageScrollTo({
scrollTop: 0,
duration: 300
});
| 名称 | 类型 | 必备 | 默认值 | 兼容性 | 描述 |
|---|---|---|---|---|---|
| errMsg | string | 是 | - | Web: -; 微信小程序: 4.41; Android: -; iOS: -; HarmonyOS: - | 错误信息 |