dlt.pageScrollTo
Introduction
Scrolls the page to a specified position.
Parameters
Object object
Property | Type | Default | Required | Description |
---|---|---|---|---|
scrollTop | number | No | The target scroll position of the page (in pixels). | |
selector | string | No | A selector. | |
duration | number | 300 | No | The duration of the scroll animation (in milliseconds). |
success | function | No | Callback function called when the interface is successfully invoked. | |
fail | function | No | Callback function called when the interface invocation fails. | |
complete | function | No | Callback function called when the interface invocation is complete (called whether the invocation is successful or not). |
object.success Callback Function
Parameters
Object res
Property | Type | Description |
---|---|---|
errMsg | string | "ok" - Success |
object.fail Callback Function
Parameters
Object res
Property | Type | Description |
---|---|---|
errMsg | string | -1 - Failure |
Selector Syntax
The selector is similar to CSS selectors but supports only the following syntax.
- ID selector: #the-id
- Class selector (multiple can be specified): .a-class.another-class
- Child element selector: .the-parent > .the-child
- Descendant selector: .the-ancestor .the-descendant
- Cross-component descendant selector: .the-ancestor >>> .the-descendant
- Union of multiple selectors: #a-node, .some-other-nodes
Note: When using a selector to specify the scroll target, do not set scrollTop; it will be based on the selector.
Sample Code
// pages/index/index.js
Page({
pageScrollTo() {
dlt.pageScrollTo({
scrollTop: 0,
duration: 1000,
success: function () {
console.log('success');
},
fail: function () {
console.log('fail');
},
complete: function () {
console.log('complete');
},
});
},
});
In the provided sample code, the pageScrollTo
function is called to scroll the page to the top (scrollTop: 0) with a duration of 1000 milliseconds. Callback functions are provided for success, failure, and completion. Adjust the scrollTop
value and duration as needed for your use case.