Scroll

更新时间:
复制 MD 格式

my.pageScrollTo

This interface is used to scroll to the target position of the page.

Note

my.pageScrollTo is supported by mPaaS 10.1.32 and above.

  • The priority of scrollTop is higher than selector.

  • When using my.pageScrollTo to jump to the top of the mini program, the scrollTop value must be set to greater than 0 to achieve the jump.

Parameters description

Attribute

Type

Default value

Required

Description

Minimumu version

scrollTop

Number

-

No

Scroll to the target position of the page, in px. When using my.pageScrollTo to jump to the top of the mini program, the scrollTop value must be set to greater than 0 to achieve the jump.

-

duration

Number

0

No

The duration of the scroll animation, in ms.

1.20.0

selector

String

-

No

Selector.

1.20.0

success

Function

-

No

The callback function of successful interface call.

-

fail

Function

-

No

Callback function when the interface call fails.

-

complete

Function

-

No

The callback function for when the interface call ends (will be executed whether the call succeeds or fails).

-

Selector syntax

When the selector parameter is passed in, the framework will execute document.querySelector(selector) to select the target node.

Code example

<!-- API-DEMO page/API/page-scroll-to/page-scroll-to.axml-->
<view class="page">
  <view class="page-description">Page Scrolling API</view>
  <view class="page-section">
    <view class="page-section-title">
      my.pageScrollTo
    </view>
    <view class="page-section-demo">
      <input type="text" placeholder="key" name="key" value="{{scrollTop}}" onInput="scrollTopChange"></input>
    </view>
    <view class="page-section-btns">
      <view onTap="scrollTo">Page Scrolling</view>
    </view>
  </view>
  <view style="height:1000px"/>
</view>
// API-DEMO page/API/page-scroll-to/page-scroll-to.js
Page({
  data: {
    scrollTop: 0,
  },
  scrollTopChange(e) {
    this.setData({
      scrollTop: e.detail.value,
    });
  },
  onPageScroll({ scrollTop }) {
    console.log('onPageScroll', scrollTop);
  },
  scrollTo() {
    my.pageScrollTo({
      scrollTop: parseInt(this.data.scrollTop),
            duration: 300,
    });
  },
});