---
name: "pageScrollTo"
mode: "api"
versionRequirements:
  - { name: "Base Library", version: "2.10.0" }
title: "ty.pageScrollTo - 将页面滚动到目标位置，支持选择器和滚动距离两种方式定位"
---

## pageScrollTo

> [VERSION] Base Library >= 2.10.0

### Description

Scroll the page to the target position, supporting location by selector or by scroll offset

### Parameters

`Params`

| Parameter | Type | Required | Description |
| --- | --- | --- | --- |
| `options` | `PageScrollToOption` | Yes | Scroll options |

### Return Value

None


### Referenced Types

##### `interface` PageScrollToOption

| Property | Type | Description |
| --- | --- | --- |
| `scrollTop` | `number` | Target scroll position on the page, in px |
| `duration` | `number` | Duration of the scroll animation, in ms |
| `selector` | `string` | Selector |
| `success` | `(result: PageScrollToResult) => void` | Success callback |
| `fail` | `(result: PageScrollToResult) => void` | Callback on failure |
| `complete` | `(result: PageScrollToResult) => void` | Callback on complete (success or failure) |

##### `type` PageScrollToResult

| Property | Type | Description |
| --- | --- | --- |
| `errMsg` | `"pageScrollTo:ok" \| "pageScrollTo:fail"` | Callback result |


### Examples

#### Scroll to a specified position

```ts
Page({
  handleScrollToTop() {
    ty.pageScrollTo({
      scrollTop: 0,
      duration: 300,
      success(res) {
        console.log('Scroll succeeded', res.errMsg);
      },
    });
  },
});
```

#### Scroll using a selector

```ts
Page({
  handleScrollToElement() {
    ty.pageScrollTo({
      scrollTop: 200,
      duration: 500,
      selector: '#my-scroll-view',
      success(res) {
        console.log('Scroll succeeded', res.errMsg);
      },
      fail(res) {
        console.error('Scroll failed', res.errMsg);
      },
    });
  },
});
```


## selector 语法

selector 类似于 CSS 的选择器，但仅支持下列语法：

- ID 选择器：`#the-id`
- class 选择器（可以连续指定多个）：`.a-class.another-class`
- 后代选择器：`.the-ancestor .the-descendant`
